Skip to content

Commit cba0606

Browse files
committed
Simplify prototype of SdkResource.
1 parent d5219d1 commit cba0606

File tree

4 files changed

+59
-23
lines changed

4 files changed

+59
-23
lines changed

sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/entities/SdkResource.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,28 @@
66
package io.opentelemetry.sdk.extension.incubator.entities;
77

88
import io.opentelemetry.api.incubator.entities.EntityBuilder;
9+
import io.opentelemetry.api.internal.GuardedBy;
910
import io.opentelemetry.sdk.resources.Resource;
1011
import io.opentelemetry.sdk.resources.internal.Entity;
1112
import io.opentelemetry.sdk.resources.internal.EntityUtil;
13+
import java.util.ArrayList;
1214
import java.util.concurrent.atomic.AtomicReference;
15+
import java.util.logging.Level;
16+
import java.util.logging.Logger;
17+
import javax.annotation.Nullable;
1318

1419
final class SdkResource implements io.opentelemetry.api.incubator.entities.Resource {
1520

21+
// The currently advertised Resource to other SDK providers.
1622
private final AtomicReference<Resource> resource = new AtomicReference<>(Resource.empty());
1723
private final Object writeLock = new Object();
1824

25+
// Our internal storage of registered entities.
26+
@GuardedBy("writeLock")
27+
private final ArrayList<Entity> entities = new ArrayList<>();
28+
29+
private static final Logger logger = Logger.getLogger(SdkResource.class.getName());
30+
1931
/** Returns the currently active resource. */
2032
public Resource getResource() {
2133
Resource result = resource.get();
@@ -32,11 +44,44 @@ public boolean removeEntity(String entityType) {
3244
throw new UnsupportedOperationException("Unimplemented method 'removeEntity'");
3345
}
3446

47+
private static boolean hasSameSchemaUrl(Entity lhs, Entity rhs) {
48+
if (lhs.getSchemaUrl() != null) {
49+
return lhs.getSchemaUrl().equals(rhs.getSchemaUrl());
50+
}
51+
return rhs.getSchemaUrl() == null;
52+
}
53+
3554
void attachEntityOnEmit(Entity e) {
3655
synchronized (writeLock) {
37-
Resource current = getResource();
38-
Resource next = EntityUtil.addEntity(Resource.builder(), e).build();
39-
resource.lazySet(current.merge(next));
56+
@Nullable Entity conflict = null;
57+
for (Entity existing : entities) {
58+
if (existing.getType().equals(e.getType())) {
59+
conflict = existing;
60+
}
61+
}
62+
63+
if (conflict != null) {
64+
if (hasSameSchemaUrl(conflict, e) && conflict.getId().equals(e.getId())) {
65+
// We can merge descriptive attributes.
66+
entities.remove(conflict);
67+
io.opentelemetry.sdk.resources.internal.EntityBuilder newEntity =
68+
Entity.builder(conflict.getType())
69+
.withId(conflict.getId())
70+
.withDescription(
71+
conflict.getDescription().toBuilder().putAll(e.getDescription()).build());
72+
if (conflict.getSchemaUrl() != null) {
73+
newEntity.setSchemaUrl(conflict.getSchemaUrl());
74+
}
75+
entities.add(newEntity.build());
76+
} else {
77+
// TODO - use ThrottlingLogger?
78+
logger.log(Level.WARNING, "Ignoring new entity, conflicts with existing: ", e);
79+
}
80+
} else {
81+
entities.add(e);
82+
}
83+
84+
resource.lazySet(EntityUtil.createResource(entities));
4085
}
4186
}
4287

sdk/common/src/main/java/io/opentelemetry/sdk/resources/internal/EntityUtil.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private EntityUtil() {}
4040
* @param entities The set of entities the resource needs.
4141
* @return A constructed resource.
4242
*/
43-
public static final Resource createResource(Collection<Entity> entities) {
43+
public static Resource createResource(Collection<Entity> entities) {
4444
return createResourceRaw(
4545
Attributes.empty(), EntityUtil.mergeResourceSchemaUrl(entities, null, null), entities);
4646
}
@@ -53,7 +53,7 @@ public static final Resource createResource(Collection<Entity> entities) {
5353
* @param entities The set of entities the resource needs.
5454
* @return A constructed resource.
5555
*/
56-
static final Resource createResourceRaw(
56+
static Resource createResourceRaw(
5757
Attributes attributes, @Nullable String schemaUrl, Collection<Entity> entities) {
5858
try {
5959
Method method =
@@ -79,7 +79,7 @@ static final Resource createResourceRaw(
7979
}
8080

8181
/** Appends a new entity on to the end of the list of entities. */
82-
public static final ResourceBuilder addEntity(ResourceBuilder rb, Entity e) {
82+
public static ResourceBuilder addEntity(ResourceBuilder rb, Entity e) {
8383
try {
8484
Method method = ResourceBuilder.class.getDeclaredMethod("add", Entity.class);
8585
if (method != null) {
@@ -97,7 +97,7 @@ public static final ResourceBuilder addEntity(ResourceBuilder rb, Entity e) {
9797
}
9898

9999
/** Appends a new collection of entities on to the end of the list of entities. */
100-
public static final ResourceBuilder addAllEntity(ResourceBuilder rb, Collection<Entity> e) {
100+
public static ResourceBuilder addAllEntity(ResourceBuilder rb, Collection<Entity> e) {
101101
try {
102102
Method method = ResourceBuilder.class.getDeclaredMethod("addAll", Collection.class);
103103
if (method != null) {
@@ -120,7 +120,7 @@ public static final ResourceBuilder addAllEntity(ResourceBuilder rb, Collection<
120120
* @return a collection of entities.
121121
*/
122122
@SuppressWarnings("unchecked")
123-
public static final Collection<Entity> getEntities(Resource r) {
123+
public static Collection<Entity> getEntities(Resource r) {
124124
try {
125125
Method method = Resource.class.getDeclaredMethod("getEntities");
126126
if (method != null) {
@@ -142,7 +142,7 @@ public static final Collection<Entity> getEntities(Resource r) {
142142
*
143143
* @return a map of attributes.
144144
*/
145-
public static final Attributes getRawAttributes(Resource r) {
145+
public static Attributes getRawAttributes(Resource r) {
146146
try {
147147
Method method = Resource.class.getDeclaredMethod("getRawAttributes");
148148
if (method != null) {
@@ -160,16 +160,15 @@ public static final Attributes getRawAttributes(Resource r) {
160160
}
161161

162162
/** Returns true if any entity in the collection has the attribute key, in id or description. */
163-
public static final <T> boolean hasAttributeKey(
164-
Collection<Entity> entities, AttributeKey<T> key) {
163+
public static <T> boolean hasAttributeKey(Collection<Entity> entities, AttributeKey<T> key) {
165164
return entities.stream()
166165
.anyMatch(
167166
e -> e.getId().asMap().containsKey(key) || e.getDescription().asMap().containsKey(key));
168167
}
169168

170169
/** Decides on a final SchemaURL for OTLP Resource based on entities chosen. */
171170
@Nullable
172-
static final String mergeResourceSchemaUrl(
171+
static String mergeResourceSchemaUrl(
173172
Collection<Entity> entities, @Nullable String baseUrl, @Nullable String nextUrl) {
174173
// Check if entities all share the same URL.
175174
Set<String> entitySchemas =
@@ -228,8 +227,7 @@ static final RawAttributeMergeResult mergeRawAttributes(
228227
additional.forEach(
229228
(key, value) -> {
230229
for (Entity e : entities) {
231-
if (e.getId().asMap().keySet().contains(key)
232-
|| e.getDescription().asMap().keySet().contains(key)) {
230+
if (e.getId().get(key) != null || e.getDescription().get(key) != null) {
233231
// Remove the entity and push all attributes as raw,
234232
// we have an override.
235233
conflicts.add(e);
@@ -249,7 +247,7 @@ static final RawAttributeMergeResult mergeRawAttributes(
249247
* @param additional Additional entities to merge with base set.
250248
* @return A new set of entities with no duplicate types.
251249
*/
252-
public static final Collection<Entity> mergeEntities(
250+
public static Collection<Entity> mergeEntities(
253251
Collection<Entity> base, Collection<Entity> additional) {
254252
if (base.isEmpty()) {
255253
return additional;

sdk/common/src/main/java/io/opentelemetry/sdk/resources/internal/SdkEntity.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,9 @@ abstract class SdkEntity implements Entity {
2626
* @param id a map of attributes that identify the entity.
2727
* @param description a map of attributes that describe the entity.
2828
* @return a {@code Entity}.
29-
* @throws NullPointerException if {@code id} or {@code description} is null.
30-
* @throws IllegalArgumentException if entityType string, attribute key or attribute value is not
31-
* a valid printable ASCII string or exceed {@link AttributeCheckUtil#MAX_LENGTH} characters.
3229
*/
3330
static final Entity create(
3431
String entityType, Attributes id, Attributes description, @Nullable String schemaUrl) {
35-
AttributeCheckUtil.isValid(entityType);
36-
AttributeCheckUtil.checkAttributes(id);
37-
AttributeCheckUtil.checkAttributes(description);
3832
return new AutoValue_SdkEntity(entityType, id, description, schemaUrl);
3933
}
4034

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/LoggerSharedState.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ final class LoggerSharedState {
3838
this.exceptionAttributeResolver = exceptionAttributeResolver;
3939
}
4040

41-
// This is used in a test, and must be public for it.
42-
public Resource getResource() {
41+
Resource getResource() {
4342
return resourceSupplier.get();
4443
}
4544

0 commit comments

Comments
 (0)