|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.sdk.extension.incubator.entities; |
| 7 | + |
| 8 | +import io.opentelemetry.api.internal.GuardedBy; |
| 9 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 10 | +import io.opentelemetry.sdk.internal.ThrottlingLogger; |
| 11 | +import io.opentelemetry.sdk.resources.Resource; |
| 12 | +import io.opentelemetry.sdk.resources.internal.Entity; |
| 13 | +import io.opentelemetry.sdk.resources.internal.EntityUtil; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 17 | +import java.util.concurrent.ExecutorService; |
| 18 | +import java.util.concurrent.atomic.AtomicReference; |
| 19 | +import java.util.logging.Level; |
| 20 | +import java.util.logging.Logger; |
| 21 | +import javax.annotation.Nullable; |
| 22 | + |
| 23 | +/** |
| 24 | + * This class does all state and listener management for a {@link Resource} constructed of {@link |
| 25 | + * Entity}s. |
| 26 | + */ |
| 27 | +final class SdkResourceSharedState { |
| 28 | + |
| 29 | + // The currently advertised Resource to other SDK providers. |
| 30 | + private final AtomicReference<Resource> resource = new AtomicReference<>(Resource.empty()); |
| 31 | + private final Object writeLock = new Object(); |
| 32 | + private final List<EntityListener> listeners = new CopyOnWriteArrayList<>(); |
| 33 | + private final ExecutorService listenerExecutor; |
| 34 | + |
| 35 | + // Our internal storage of registered entities. |
| 36 | + @GuardedBy("writeLock") |
| 37 | + private final ArrayList<Entity> entities = new ArrayList<>(); |
| 38 | + |
| 39 | + private static final ThrottlingLogger logger = |
| 40 | + new ThrottlingLogger(Logger.getLogger(SdkResourceSharedState.class.getName())); |
| 41 | + |
| 42 | + SdkResourceSharedState(ExecutorService listenerExecutor) { |
| 43 | + this.listenerExecutor = listenerExecutor; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Shutdown the provider. The resulting {@link CompletableResultCode} completes when all complete. |
| 48 | + */ |
| 49 | + CompletableResultCode shutdown() { |
| 50 | + // TODO - Actually figure out how to wait for shutdown and deal with pending tasks. |
| 51 | + listenerExecutor.shutdown(); |
| 52 | + return CompletableResultCode.ofSuccess(); |
| 53 | + } |
| 54 | + |
| 55 | + /** Returns the currently active resource. */ |
| 56 | + public Resource getResource() { |
| 57 | + Resource result = resource.get(); |
| 58 | + // We do this to make NullAway happy. |
| 59 | + if (result == null) { |
| 60 | + throw new IllegalStateException("SdkResource should never have null resource"); |
| 61 | + } |
| 62 | + return result; |
| 63 | + } |
| 64 | + |
| 65 | + private static boolean hasSameSchemaUrl(Entity lhs, Entity rhs) { |
| 66 | + if (lhs.getSchemaUrl() != null) { |
| 67 | + return lhs.getSchemaUrl().equals(rhs.getSchemaUrl()); |
| 68 | + } |
| 69 | + return rhs.getSchemaUrl() == null; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Removes an entity by type and notifies listeners. |
| 74 | + * |
| 75 | + * @param entityType The entity type to remove. |
| 76 | + */ |
| 77 | + boolean removeEntity(String entityType) { |
| 78 | + synchronized (writeLock) { |
| 79 | + @Nullable Entity removed = null; |
| 80 | + for (Entity existing : entities) { |
| 81 | + if (existing.getType().equals(entityType)) { |
| 82 | + removed = existing; |
| 83 | + } |
| 84 | + } |
| 85 | + if (removed == null) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + entities.remove(removed); |
| 89 | + Resource result = EntityUtil.createResource(entities); |
| 90 | + resource.lazySet(result); |
| 91 | + publishEntityDelete(new SdkEntityState(removed), result); |
| 92 | + return true; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Adds an entity and notifies listeners. |
| 98 | + * |
| 99 | + * <p>Note: This will not add an entity on conflict. This will update the description if the |
| 100 | + * entity already exists. |
| 101 | + * |
| 102 | + * @param e The entity type to add. |
| 103 | + */ |
| 104 | + void addOrUpdateEntity(Entity e) { |
| 105 | + synchronized (writeLock) { |
| 106 | + @Nullable Entity conflict = null; |
| 107 | + for (Entity existing : entities) { |
| 108 | + if (existing.getType().equals(e.getType())) { |
| 109 | + conflict = existing; |
| 110 | + } |
| 111 | + } |
| 112 | + Entity newState = e; |
| 113 | + if (conflict != null) { |
| 114 | + if (hasSameSchemaUrl(conflict, e) && conflict.getId().equals(e.getId())) { |
| 115 | + // We can merge descriptive attributes. |
| 116 | + entities.remove(conflict); |
| 117 | + io.opentelemetry.sdk.resources.internal.EntityBuilder newEntity = |
| 118 | + Entity.builder(conflict.getType()) |
| 119 | + .withId(conflict.getId()) |
| 120 | + .withDescription( |
| 121 | + conflict.getDescription().toBuilder().putAll(e.getDescription()).build()); |
| 122 | + if (conflict.getSchemaUrl() != null) { |
| 123 | + newEntity.setSchemaUrl(conflict.getSchemaUrl()); |
| 124 | + } |
| 125 | + newState = newEntity.build(); |
| 126 | + entities.add(newState); |
| 127 | + } else { |
| 128 | + logger.log(Level.INFO, "Ignoring new entity, conflicts with existing: " + e); |
| 129 | + return; |
| 130 | + } |
| 131 | + } else { |
| 132 | + entities.add(e); |
| 133 | + } |
| 134 | + Resource result = EntityUtil.createResource(entities); |
| 135 | + resource.lazySet(result); |
| 136 | + publishEntityStateChange(new SdkEntityState(newState), result); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @SuppressWarnings("FutureReturnValueIgnored") |
| 141 | + private void publishEntityStateChange(EntityState state, Resource resource) { |
| 142 | + for (EntityListener listener : listeners) { |
| 143 | + // We isolate listener execution via executor, if configured. |
| 144 | + // We ignore failures on futures to avoid having one listener block others. |
| 145 | + listenerExecutor.submit(() -> listener.onEntityState(state, resource)); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @SuppressWarnings("FutureReturnValueIgnored") |
| 150 | + private void publishEntityDelete(EntityState deleted, Resource resource) { |
| 151 | + for (EntityListener listener : listeners) { |
| 152 | + // We isolate listener execution via executor, if configured. |
| 153 | + // We ignore failures on futures to avoid having one listener block others. |
| 154 | + listenerExecutor.submit(() -> listener.onEntityDelete(deleted, resource)); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public void addListener(EntityListener listener) { |
| 159 | + listeners.add(listener); |
| 160 | + } |
| 161 | +} |
0 commit comments