Skip to content

Commit ddf096d

Browse files
committed
Remove all publicly accessible references to Entity from stable packages
1 parent b119561 commit ddf096d

File tree

5 files changed

+95
-14
lines changed

5 files changed

+95
-14
lines changed
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
Comparing source compatibility of opentelemetry-sdk-common-1.52.0-SNAPSHOT.jar against opentelemetry-sdk-common-1.51.0.jar
2-
**** MODIFIED CLASS: PUBLIC ABSTRACT io.opentelemetry.sdk.resources.Resource (not serializable)
2+
*** MODIFIED CLASS: PUBLIC ABSTRACT io.opentelemetry.sdk.resources.Resource (not serializable)
33
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
44
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.resources.Resource create(io.opentelemetry.api.common.Attributes, java.lang.String, java.util.Collection<io.opentelemetry.sdk.resources.internal.Entity>)
55
*** MODIFIED METHOD: PUBLIC NON_ABSTRACT (<- ABSTRACT) io.opentelemetry.api.common.Attributes getAttributes()
6-
+++* NEW METHOD: PUBLIC(+) ABSTRACT(+) java.util.Collection<io.opentelemetry.sdk.resources.internal.Entity> getEntities()
7-
+++* NEW METHOD: PUBLIC(+) ABSTRACT(+) io.opentelemetry.api.common.Attributes getRawAttributes()
8-
*** MODIFIED CLASS: PUBLIC io.opentelemetry.sdk.resources.ResourceBuilder (not serializable)
9-
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
10-
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.resources.ResourceBuilder add(io.opentelemetry.sdk.resources.internal.Entity)
11-
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.resources.ResourceBuilder addAll(java.util.Collection<io.opentelemetry.sdk.resources.internal.Entity>)

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/ResourceMarshaler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
1111
import io.opentelemetry.exporter.internal.marshal.Serializer;
1212
import io.opentelemetry.proto.resource.v1.internal.Resource;
13+
import io.opentelemetry.sdk.resources.internal.EntityUtil;
1314
import java.io.ByteArrayOutputStream;
1415
import java.io.IOException;
1516
import java.io.UncheckedIOException;
@@ -38,7 +39,7 @@ public static ResourceMarshaler create(io.opentelemetry.sdk.resources.Resource r
3839
RealResourceMarshaler realMarshaler =
3940
new RealResourceMarshaler(
4041
KeyValueMarshaler.createForAttributes(resource.getAttributes()),
41-
resource.getEntities().stream()
42+
EntityUtil.getEntities(resource).stream()
4243
.map(EntityRefMarshaler::createForEntity)
4344
.toArray(MarshalerWithSize[]::new));
4445

sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ public static Resource create(
135135
*
136136
* @return a map of attributes.
137137
*/
138-
public abstract Attributes getRawAttributes();
138+
abstract Attributes getRawAttributes();
139139

140140
/**
141141
* Returns a collectoion of associated entities.
142142
*
143143
* @return a collection of entities.
144144
*/
145-
public abstract Collection<Entity> getEntities();
145+
abstract Collection<Entity> getEntities();
146146

147147
/**
148148
* Returns a map of attributes that describe the resource.

sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,13 @@ public Resource build() {
220220
}
221221

222222
/** Appends a new entity on to the end of the list of entities. */
223-
public ResourceBuilder add(Entity e) {
223+
ResourceBuilder add(Entity e) {
224224
this.entities.add(e);
225225
return this;
226226
}
227227

228228
/** Appends a new collection of entities on to the end of the list of entities. */
229-
public ResourceBuilder addAll(Collection<Entity> entities) {
229+
ResourceBuilder addAll(Collection<Entity> entities) {
230230
this.entities.addAll(entities);
231231
return this;
232232
}

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

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
import io.opentelemetry.api.common.Attributes;
1010
import io.opentelemetry.api.common.AttributesBuilder;
1111
import io.opentelemetry.sdk.resources.Resource;
12+
import io.opentelemetry.sdk.resources.ResourceBuilder;
13+
import java.lang.reflect.InvocationTargetException;
14+
import java.lang.reflect.Method;
1215
import java.util.ArrayList;
1316
import java.util.Collection;
17+
import java.util.Collections;
1418
import java.util.HashMap;
1519
import java.util.Map;
1620
import java.util.Set;
21+
import java.util.logging.Level;
1722
import java.util.logging.Logger;
1823
import java.util.stream.Collectors;
1924
import javax.annotation.Nullable;
@@ -29,6 +34,87 @@ public final class EntityUtil {
2934

3035
private EntityUtil() {}
3136

37+
/** Appends a new entity on to the end of the list of entities. */
38+
public static final ResourceBuilder addEntity(ResourceBuilder rb, Entity e) {
39+
try {
40+
Method method = Resource.class.getDeclaredMethod("add", Entity.class);
41+
if (method != null) {
42+
method.setAccessible(true);
43+
method.invoke(rb, e);
44+
}
45+
} catch (NoSuchMethodException nme) {
46+
logger.log(Level.WARNING, "Attempting to use entities with unsupported resource", nme);
47+
} catch (IllegalAccessException iae) {
48+
logger.log(Level.WARNING, "Attempting to use entities with unsupported resource", iae);
49+
} catch (InvocationTargetException ite) {
50+
logger.log(Level.WARNING, "Attempting to use entities with unsupported resource", ite);
51+
}
52+
return rb;
53+
}
54+
55+
/** Appends a new collection of entities on to the end of the list of entities. */
56+
public static final ResourceBuilder addAllEntity(ResourceBuilder rb, Collection<Entity> e) {
57+
try {
58+
Method method = Resource.class.getDeclaredMethod("addAll", Collection.class);
59+
if (method != null) {
60+
method.setAccessible(true);
61+
method.invoke(rb, e);
62+
}
63+
} catch (NoSuchMethodException nme) {
64+
logger.log(Level.WARNING, "Attempting to use entities with unsupported resource", nme);
65+
} catch (IllegalAccessException iae) {
66+
logger.log(Level.WARNING, "Attempting to use entities with unsupported resource", iae);
67+
} catch (InvocationTargetException ite) {
68+
logger.log(Level.WARNING, "Attempting to use entities with unsupported resource", ite);
69+
}
70+
return rb;
71+
}
72+
73+
/**
74+
* Returns a collectoion of associated entities.
75+
*
76+
* @return a collection of entities.
77+
*/
78+
@SuppressWarnings("unchecked")
79+
public static final Collection<Entity> getEntities(Resource r) {
80+
try {
81+
Method method = Resource.class.getDeclaredMethod("getEntities");
82+
if (method != null) {
83+
method.setAccessible(true);
84+
return (Collection<Entity>) method.invoke(r);
85+
}
86+
} catch (NoSuchMethodException nme) {
87+
logger.log(Level.WARNING, "Attempting to use entities with unsupported resource", nme);
88+
} catch (IllegalAccessException iae) {
89+
logger.log(Level.WARNING, "Attempting to use entities with unsupported resource", iae);
90+
} catch (InvocationTargetException ite) {
91+
logger.log(Level.WARNING, "Attempting to use entities with unsupported resource", ite);
92+
}
93+
return Collections.emptyList();
94+
}
95+
96+
/**
97+
* Returns a map of attributes that describe the resource, not associated with entites.
98+
*
99+
* @return a map of attributes.
100+
*/
101+
public static final Attributes getRawAttributes(Resource r) {
102+
try {
103+
Method method = Resource.class.getDeclaredMethod("getRawAttributes");
104+
if (method != null) {
105+
method.setAccessible(true);
106+
return (Attributes) method.invoke(r);
107+
}
108+
} catch (NoSuchMethodException nme) {
109+
logger.log(Level.WARNING, "Attempting to use entities with unsupported resource", nme);
110+
} catch (IllegalAccessException iae) {
111+
logger.log(Level.WARNING, "Attempting to use entities with unsupported resource", iae);
112+
} catch (InvocationTargetException ite) {
113+
logger.log(Level.WARNING, "Attempting to use entities with unsupported resource", ite);
114+
}
115+
return Attributes.empty();
116+
}
117+
32118
/** Returns true if any entity in the collection has the attribute key, in id or description. */
33119
public static final <T> boolean hasAttributeKey(
34120
Collection<Entity> entities, AttributeKey<T> key) {
@@ -196,9 +282,9 @@ public static Resource merge(Resource base, @Nullable Resource next) {
196282
}
197283
// Merge Algorithm from
198284
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/oteps/entities/0264-resource-and-entities.md#entity-merging-and-resource
199-
Collection<Entity> entities = EntityUtil.mergeEntities(base.getEntities(), next.getEntities());
285+
Collection<Entity> entities = EntityUtil.mergeEntities(getEntities(base), getEntities(next));
200286
RawAttributeMergeResult attributeResult =
201-
EntityUtil.mergeRawAttributes(base.getRawAttributes(), next.getRawAttributes(), entities);
287+
EntityUtil.mergeRawAttributes(getRawAttributes(base), getRawAttributes(next), entities);
202288
// Remove entiites that are conflicting with raw attributes, and therefore in an unknown state.
203289
entities.removeAll(attributeResult.getConflicts());
204290
// Now figure out schema url for overall resource.

0 commit comments

Comments
 (0)