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

This commit is contained in:
Lauri Tulmin 2024-01-26 17:59:04 +02:00 committed by GitHub
parent 4695b57d73
commit 20e3cd6ab8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 1 deletions

View File

@ -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;

View File

@ -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());
}
}