Skip to content

Commit

Permalink
Undertow: restore attached context only when it is for different trace (
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored and steverao committed Feb 16, 2024
1 parent e432cf7 commit 8cab456
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static void onEnter(
@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,4 +86,12 @@ private static void attachServerContext(Context context, HttpServerExchange exch
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());
}
}

0 comments on commit 8cab456

Please sign in to comment.