From 50874ce076845820b6bdd63f3fb7382452bf3e77 Mon Sep 17 00:00:00 2001 From: Shashank Patidar Date: Mon, 25 Sep 2023 18:42:14 +0530 Subject: [PATCH] add conetnt type to additional data spans --- .../java/inputstream/InputStreamUtils.java | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) 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 67fb84ee..0b05f1df 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 @@ -19,6 +19,7 @@ import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.api.trace.Tracer; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.util.VirtualField; @@ -26,6 +27,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.nio.charset.Charset; import org.hypertrace.agent.core.instrumentation.HypertraceCallDepthThreadLocalMap; import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes; @@ -50,12 +53,34 @@ public static void addAttribute(Span span, AttributeKey attributeKey, St if (span.isRecording()) { span.setAttribute(attributeKey, value); } else { - TRACER - .spanBuilder(HypertraceSemanticAttributes.ADDITIONAL_DATA_SPAN_NAME) - .setParent(Context.root().with(span)) - .setAttribute(attributeKey, value) - .startSpan() - .end(); + SpanBuilder spanBuilder = + TRACER + .spanBuilder(HypertraceSemanticAttributes.ADDITIONAL_DATA_SPAN_NAME) + .setParent(Context.root().with(span)) + .setAttribute(attributeKey, value); + + // Also add content type if present + if (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")); + if (reqContentType != null) { + spanBuilder.setAttribute("http.request.header.content-type", (String) reqContentType); + } + Object resContentType = + getAttribute.invoke( + span, AttributeKey.stringKey("http.response.header.content-type")); + if (resContentType != null) { + spanBuilder.setAttribute("http.response.header.content-type", (String) resContentType); + } + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + // ignore and continue + } + } + spanBuilder.startSpan().end(); } }