diff --git a/instrumentation/java-streams/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/java/inputstream/InputStreamUtils.java b/instrumentation/java-streams/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/java/inputstream/InputStreamUtils.java index 9fda5290..d3436067 100644 --- a/instrumentation/java-streams/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/java/inputstream/InputStreamUtils.java +++ b/instrumentation/java-streams/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/java/inputstream/InputStreamUtils.java @@ -46,21 +46,7 @@ private InputStreamUtils() {} GlobalOpenTelemetry.get().getTracer("org.hypertrace.java.inputstream"); private static Method getAttribute = null; - - static { - try { - getAttribute = - Class.forName("io.opentelemetry.sdk.trace.SdkSpan") - .getDeclaredMethod("getAttribute", AttributeKey.class); - } catch (NoSuchMethodException e) { - log.error("getAttribute method not found in SdkSpan class", e); - } catch (ClassNotFoundException e) { - log.error("SdkSpan class not found", e); - } - if (getAttribute != null) { - getAttribute.setAccessible(true); - } - } + private static boolean hasGetAttributeMethod = true; /** * 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 attributeKey, St .setAttribute(attributeKey, value); // Also add content type if present - if (getAttribute != null - && span.getClass().getName().equals("io.opentelemetry.sdk.trace.SdkSpan")) { + if (span.getClass().getName().equals("io.opentelemetry.sdk.trace.SdkSpan")) { try { - Object reqContentType = - getAttribute.invoke( - span, HypertraceSemanticAttributes.HTTP_REQUEST_HEADER_CONTENT_TYPE); - if (reqContentType != null) { - spanBuilder.setAttribute("http.request.header.content-type", (String) reqContentType); + if (getAttribute == null) { + if (hasGetAttributeMethod) { + getAttribute = span.getClass().getDeclaredMethod("getAttribute", AttributeKey.class); + getAttribute.setAccessible(true); + } } - Object resContentType = - getAttribute.invoke( - span, HypertraceSemanticAttributes.HTTP_RESPONSE_HEADER_CONTENT_TYPE); - if (resContentType != null) { - spanBuilder.setAttribute("http.response.header.content-type", (String) resContentType); + if (getAttribute != null) { + Object reqContentType = + getAttribute.invoke( + span, HypertraceSemanticAttributes.HTTP_REQUEST_HEADER_CONTENT_TYPE); + if (reqContentType != null) { + spanBuilder.setAttribute("http.request.header.content-type", (String) reqContentType); + } + Object resContentType = + getAttribute.invoke( + span, HypertraceSemanticAttributes.HTTP_RESPONSE_HEADER_CONTENT_TYPE); + if (resContentType != null) { + spanBuilder.setAttribute( + "http.response.header.content-type", (String) resContentType); + } } } catch (IllegalAccessException | InvocationTargetException e) { // ignore and continue log.debug("Could not invoke getAttribute on SdkSpan", e); + } catch (NoSuchMethodException e) { + log.debug("getAttribute method not found in SdkSpan class", e); + // do not attempt to get this method again + hasGetAttributeMethod = false; } } spanBuilder.startSpan().end();