Skip to content

Commit

Permalink
add conetnt type to additional data spans
Browse files Browse the repository at this point in the history
  • Loading branch information
shashank11p committed Sep 25, 2023
1 parent c9cb1fe commit 50874ce
Showing 1 changed file with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
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;
import java.io.ByteArrayOutputStream;
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;
Expand All @@ -50,12 +53,34 @@ public static void addAttribute(Span span, AttributeKey<String> 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();
}
}

Expand Down

0 comments on commit 50874ce

Please sign in to comment.