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 0b05f1df..7581bab6 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 @@ -45,6 +45,21 @@ private InputStreamUtils() {} private static final Tracer TRACER = 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); + } + } + /** * Adds an attribute to span. If the span is ended it adds the attributed to a newly created * child. @@ -60,24 +75,22 @@ public static void addAttribute(Span span, AttributeKey attributeKey, St .setAttribute(attributeKey, value); // Also add content type if present - if (span.getClass().getName().equals("io.opentelemetry.sdk.trace.SdkSpan")) { + if (getAttribute != null && span.getClass().getName().equals("io.opentelemetry.sdk.trace.SdkSpan")) { try { - Method getAttribute = - span.getClass().getDeclaredMethod("getAttribute", AttributeKey.class); - getAttribute.setAccessible(true); Object reqContentType = - getAttribute.invoke(span, AttributeKey.stringKey("http.request.header.content-type")); + 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, AttributeKey.stringKey("http.response.header.content-type")); + span, HypertraceSemanticAttributes.HTTP_RESPONSE_HEADER_CONTENT_TYPE); if (resContentType != null) { spanBuilder.setAttribute("http.response.header.content-type", (String) resContentType); } - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + } catch (IllegalAccessException | InvocationTargetException e) { // ignore and continue + log.debug("Could not invoke getAttribute on SdkSpan", e); } } spanBuilder.startSpan().end(); diff --git a/instrumentation/vertx/vertx-web-3.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/vertx/ResponseBodyWrappingHandler.java b/instrumentation/vertx/vertx-web-3.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/vertx/ResponseBodyWrappingHandler.java index 1cdd1758..c00a4565 100644 --- a/instrumentation/vertx/vertx-web-3.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/vertx/ResponseBodyWrappingHandler.java +++ b/instrumentation/vertx/vertx-web-3.0/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/vertx/ResponseBodyWrappingHandler.java @@ -27,12 +27,31 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ResponseBodyWrappingHandler implements Handler { private static final Tracer tracer = GlobalOpenTelemetry.getTracer("io.opentelemetry.javaagent.vertx-core-3.0"); + private static final Logger log = LoggerFactory.getLogger(ResponseBodyWrappingHandler.class); + + 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 final Handler wrapped; private final Span span; @@ -54,19 +73,17 @@ public void handle(Buffer event) { .setAttribute(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY, responseBody); // Also add content type if present - if (span.getClass().getName().equals("io.opentelemetry.sdk.trace.SdkSpan")) { + if (getAttribute != null && span.getClass().getName().equals("io.opentelemetry.sdk.trace.SdkSpan")) { try { - Method getAttribute = - span.getClass().getDeclaredMethod("getAttribute", AttributeKey.class); - getAttribute.setAccessible(true); Object resContentType = getAttribute.invoke( - span, AttributeKey.stringKey("http.response.header.content-type")); + span, HypertraceSemanticAttributes.HTTP_RESPONSE_HEADER_CONTENT_TYPE); if (resContentType != null) { spanBuilder.setAttribute("http.response.header.content-type", (String) resContentType); } - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + } catch (IllegalAccessException | InvocationTargetException e) { // ignore and continue + log.debug("Could not invoke getAttribute on SdkSpan", e); } } diff --git a/javaagent-core/src/main/java/org/hypertrace/agent/core/instrumentation/HypertraceSemanticAttributes.java b/javaagent-core/src/main/java/org/hypertrace/agent/core/instrumentation/HypertraceSemanticAttributes.java index 5e94c8cd..20b5fd17 100644 --- a/javaagent-core/src/main/java/org/hypertrace/agent/core/instrumentation/HypertraceSemanticAttributes.java +++ b/javaagent-core/src/main/java/org/hypertrace/agent/core/instrumentation/HypertraceSemanticAttributes.java @@ -45,6 +45,12 @@ public static AttributeKey httpResponseHeader(String header) { public static final AttributeKey HTTP_REQUEST_SESSION_ID = AttributeKey.stringKey("http.request.session_id"); + public static final AttributeKey HTTP_REQUEST_HEADER_CONTENT_TYPE = + AttributeKey.stringKey("http.request.header.content-type"); + + public static final AttributeKey HTTP_RESPONSE_HEADER_CONTENT_TYPE = + AttributeKey.stringKey("http.response.header.content-type"); + public static final AttributeKey RPC_REQUEST_BODY = AttributeKey.stringKey("rpc.request.body"); public static final AttributeKey RPC_RESPONSE_BODY =