Skip to content

Commit 20e3cd6

Browse files
authored
Undertow: restore attached context only when it is for different trace (#10336)
1 parent 4695b57 commit 20e3cd6

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void onEnter(
5353
@Advice.Local("otelScope") Scope scope) {
5454
Context attachedContext = helper().getServerContext(exchange);
5555
if (attachedContext != null) {
56-
if (!Java8BytecodeBridge.currentContext().equals(attachedContext)) {
56+
if (!helper().sameTrace(Java8BytecodeBridge.currentContext(), attachedContext)) {
5757
// request processing is dispatched to another thread
5858
scope = attachedContext.makeCurrent();
5959
context = attachedContext;

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

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.javaagent.instrumentation.undertow;
77

8+
import io.opentelemetry.api.trace.Span;
89
import io.opentelemetry.context.Context;
910
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
1011
import io.opentelemetry.javaagent.bootstrap.servlet.AppServerBridge;
@@ -85,4 +86,12 @@ private static void attachServerContext(Context context, HttpServerExchange exch
8586
AttachmentKey.class, key -> AttachmentKey.create(Context.class));
8687
exchange.putAttachment(contextKey, context);
8788
}
89+
90+
public boolean sameTrace(Context currentContext, Context attachedContext) {
91+
return sameTrace(Span.fromContext(currentContext), Span.fromContext(attachedContext));
92+
}
93+
94+
private static boolean sameTrace(Span oneSpan, Span otherSpan) {
95+
return oneSpan.getSpanContext().getTraceId().equals(otherSpan.getSpanContext().getTraceId());
96+
}
8897
}

0 commit comments

Comments
 (0)