Skip to content

Commit

Permalink
fix additional data content type (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
shashank11p committed Dec 7, 2023
1 parent 728d665 commit bf443ae
Showing 1 changed file with 25 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -77,24 +63,36 @@ public static void addAttribute(Span span, AttributeKey<String> 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();
Expand Down

0 comments on commit bf443ae

Please sign in to comment.