Browse Source

Undertow: restore attached context only when it is for different trace (#10336)

Lauri Tulmin 1 year ago
parent
commit
20e3cd6ab8

+ 1 - 1
instrumentation/undertow-1.4/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/undertow/HandlerInstrumentation.java

@@ -53,7 +53,7 @@ public class HandlerInstrumentation implements TypeInstrumentation {
         @Advice.Local("otelScope") Scope scope) {
       Context attachedContext = helper().getServerContext(exchange);
       if (attachedContext != null) {
-        if (!Java8BytecodeBridge.currentContext().equals(attachedContext)) {
+        if (!helper().sameTrace(Java8BytecodeBridge.currentContext(), attachedContext)) {
           // request processing is dispatched to another thread
           scope = attachedContext.makeCurrent();
           context = attachedContext;

+ 9 - 0
instrumentation/undertow-1.4/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/undertow/UndertowHelper.java

@@ -5,6 +5,7 @@
 
 package io.opentelemetry.javaagent.instrumentation.undertow;
 
+import io.opentelemetry.api.trace.Span;
 import io.opentelemetry.context.Context;
 import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
 import io.opentelemetry.javaagent.bootstrap.servlet.AppServerBridge;
@@ -85,4 +86,12 @@ public class UndertowHelper {
                 AttachmentKey.class, key -> AttachmentKey.create(Context.class));
     exchange.putAttachment(contextKey, context);
   }
+
+  public boolean sameTrace(Context currentContext, Context attachedContext) {
+    return sameTrace(Span.fromContext(currentContext), Span.fromContext(attachedContext));
+  }
+
+  private static boolean sameTrace(Span oneSpan, Span otherSpan) {
+    return oneSpan.getSpanContext().getTraceId().equals(otherSpan.getSpanContext().getTraceId());
+  }
 }