@@ -46,21 +46,7 @@ private InputStreamUtils() {}
46
46
GlobalOpenTelemetry .get ().getTracer ("org.hypertrace.java.inputstream" );
47
47
48
48
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 ;
64
50
65
51
/**
66
52
* 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
77
63
.setAttribute (attributeKey , value );
78
64
79
65
// 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" )) {
82
67
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
+ }
88
73
}
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
+ }
94
88
}
95
89
} catch (IllegalAccessException | InvocationTargetException e ) {
96
90
// ignore and continue
97
91
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 ;
98
96
}
99
97
}
100
98
spanBuilder .startSpan ().end ();
0 commit comments