@@ -46,21 +46,7 @@ private InputStreamUtils() {}
4646 GlobalOpenTelemetry .get ().getTracer ("org.hypertrace.java.inputstream" );
4747
4848 private static Method getAttribute = null ;
49-
50- static {
51- try {
52- getAttribute =
53- Class .forName ("io.opentelemetry.sdk.trace.SdkSpan" )
54- .getDeclaredMethod ("getAttribute" , AttributeKey .class );
55- } catch (NoSuchMethodException e ) {
56- log .error ("getAttribute method not found in SdkSpan class" , e );
57- } catch (ClassNotFoundException e ) {
58- log .error ("SdkSpan class not found" , e );
59- }
60- if (getAttribute != null ) {
61- getAttribute .setAccessible (true );
62- }
63- }
49+ private static boolean hasGetAttributeMethod = true ;
6450
6551 /**
6652 * Adds an attribute to span. If the span is ended it adds the attributed to a newly created
@@ -77,24 +63,36 @@ public static void addAttribute(Span span, AttributeKey<String> attributeKey, St
7763 .setAttribute (attributeKey , value );
7864
7965 // Also add content type if present
80- if (getAttribute != null
81- && span .getClass ().getName ().equals ("io.opentelemetry.sdk.trace.SdkSpan" )) {
66+ if (span .getClass ().getName ().equals ("io.opentelemetry.sdk.trace.SdkSpan" )) {
8267 try {
83- Object reqContentType =
84- getAttribute . invoke (
85- span , HypertraceSemanticAttributes . HTTP_REQUEST_HEADER_CONTENT_TYPE );
86- if ( reqContentType != null ) {
87- spanBuilder . setAttribute ( "http.request.header.content-type" , ( String ) reqContentType );
68+ if ( getAttribute == null ) {
69+ if ( hasGetAttributeMethod ) {
70+ getAttribute = span . getClass (). getDeclaredMethod ( "getAttribute" , AttributeKey . class );
71+ getAttribute . setAccessible ( true );
72+ }
8873 }
89- Object resContentType =
90- getAttribute .invoke (
91- span , HypertraceSemanticAttributes .HTTP_RESPONSE_HEADER_CONTENT_TYPE );
92- if (resContentType != null ) {
93- spanBuilder .setAttribute ("http.response.header.content-type" , (String ) resContentType );
74+ if (getAttribute != null ) {
75+ Object reqContentType =
76+ getAttribute .invoke (
77+ span , HypertraceSemanticAttributes .HTTP_REQUEST_HEADER_CONTENT_TYPE );
78+ if (reqContentType != null ) {
79+ spanBuilder .setAttribute ("http.request.header.content-type" , (String ) reqContentType );
80+ }
81+ Object resContentType =
82+ getAttribute .invoke (
83+ span , HypertraceSemanticAttributes .HTTP_RESPONSE_HEADER_CONTENT_TYPE );
84+ if (resContentType != null ) {
85+ spanBuilder .setAttribute (
86+ "http.response.header.content-type" , (String ) resContentType );
87+ }
9488 }
9589 } catch (IllegalAccessException | InvocationTargetException e ) {
9690 // ignore and continue
9791 log .debug ("Could not invoke getAttribute on SdkSpan" , e );
92+ } catch (NoSuchMethodException e ) {
93+ log .debug ("getAttribute method not found in SdkSpan class" , e );
94+ // do not attempt to get this method again
95+ hasGetAttributeMethod = false ;
9896 }
9997 }
10098 spanBuilder .startSpan ().end ();
0 commit comments