Skip to content

Commit 54b3d90

Browse files
committed
If there's an existing servlet3 context create a new context with dispatch span, otherwise just attach the dispatch span.
1 parent d10055d commit 54b3d90

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

  • dd-java-agent/instrumentation/servlet/javax-servlet/javax-servlet-3.0/src/main/java/datadog/trace/instrumentation/servlet3

dd-java-agent/instrumentation/servlet/javax-servlet/javax-servlet-3.0/src/main/java/datadog/trace/instrumentation/servlet3/Servlet3Advice.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public static boolean onEnter(
6161
}
6262
}
6363

64+
Object contextAttr = request.getAttribute(DD_CONTEXT_ATTRIBUTE);
65+
6466
Object dispatchSpan = request.getAttribute(DD_DISPATCH_SPAN_ATTRIBUTE);
6567
if (dispatchSpan instanceof AgentSpan) {
6668
request.removeAttribute(DD_DISPATCH_SPAN_ATTRIBUTE);
@@ -71,13 +73,19 @@ public static boolean onEnter(
7173
// the dispatch span was already activated in Jetty's HandleAdvice. We let it finish the span
7274
// to avoid trying to finish twice
7375
finishSpan = activeSpan() != dispatchSpan;
74-
scope = castDispatchSpan.attach();
76+
// If we have an existing context, create a new context with the dispatch span
77+
// Otherwise just attach the dispatch span
78+
if (contextAttr instanceof Context) {
79+
Context contextWithDispatchSpan = ((Context) contextAttr).with(castDispatchSpan);
80+
scope = contextWithDispatchSpan.attach();
81+
} else {
82+
scope = castDispatchSpan.attach();
83+
}
7584
return false;
7685
}
7786

7887
finishSpan = true;
7988

80-
Object contextAttr = request.getAttribute(DD_CONTEXT_ATTRIBUTE);
8189
if (contextAttr instanceof Context) {
8290
final Context existingContext = (Context) contextAttr;
8391
final AgentSpan span = spanFromContext(existingContext);

0 commit comments

Comments
 (0)