99import io .opentelemetry .api .common .Attributes ;
1010import io .opentelemetry .api .common .AttributesBuilder ;
1111import io .opentelemetry .sdk .resources .Resource ;
12+ import io .opentelemetry .sdk .resources .ResourceBuilder ;
13+ import java .lang .reflect .InvocationTargetException ;
14+ import java .lang .reflect .Method ;
1215import java .util .ArrayList ;
1316import java .util .Collection ;
17+ import java .util .Collections ;
1418import java .util .HashMap ;
1519import java .util .Map ;
1620import java .util .Set ;
21+ import java .util .logging .Level ;
1722import java .util .logging .Logger ;
1823import java .util .stream .Collectors ;
1924import 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