Skip to content

Commit 2349c6e

Browse files
authored
HttpServerDecorator.onRequest expects to be passed the parent context containing the extracted trace data (#9513)
1 parent 0007547 commit 2349c6e

File tree

12 files changed

+35
-26
lines changed

12 files changed

+35
-26
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/decorator/HttpServerDecorator.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,17 @@ public Context extract(REQUEST_CARRIER carrier) {
138138
* Starts a span.
139139
*
140140
* @param carrier The request carrier.
141-
* @param context The parent context of the span to create.
141+
* @param parentContext The parent context of the span to create.
142142
* @return A new context bundling the span, child of the given parent context.
143143
*/
144-
public Context startSpan(REQUEST_CARRIER carrier, Context context) {
144+
public Context startSpan(REQUEST_CARRIER carrier, Context parentContext) {
145145
String[] instrumentationNames = instrumentationNames();
146146
String instrumentationName =
147147
instrumentationNames != null && instrumentationNames.length > 0
148148
? instrumentationNames[0]
149149
: DEFAULT_INSTRUMENTATION_NAME;
150-
AgentSpanContext.Extracted extracted = callIGCallbackStart(getExtractedSpanContext(context));
150+
AgentSpanContext.Extracted extracted =
151+
callIGCallbackStart(getExtractedSpanContext(parentContext));
151152
AgentSpan span =
152153
tracer().startSpan(instrumentationName, spanName(), extracted).setMeasured(true);
153154
Flow<Void> flow = callIGCallbackRequestHeaders(span, carrier);
@@ -158,14 +159,14 @@ public Context startSpan(REQUEST_CARRIER carrier, Context context) {
158159
if (null != carrier && null != getter) {
159160
tracer().getDataStreamsMonitoring().setCheckpoint(span, forHttpServer());
160161
}
161-
return context.with(span);
162+
return parentContext.with(span);
162163
}
163164

164165
public AgentSpan onRequest(
165166
final AgentSpan span,
166167
final CONNECTION connection,
167168
final REQUEST request,
168-
final Context context) {
169+
final Context parentContext) {
169170
Config config = Config.get();
170171

171172
if (APPSEC_ACTIVE) {
@@ -183,7 +184,7 @@ public AgentSpan onRequest(
183184
}
184185
}
185186

186-
AgentSpanContext.Extracted extracted = getExtractedSpanContext(context);
187+
AgentSpanContext.Extracted extracted = getExtractedSpanContext(parentContext);
187188
boolean clientIpResolverEnabled =
188189
config.isClientIpEnabled() || traceClientIpResolverEnabled && APPSEC_ACTIVE;
189190
if (extracted != null) {
@@ -316,9 +317,17 @@ public AgentSpan onRequest(
316317
return span;
317318
}
318319

319-
protected static AgentSpanContext.Extracted getExtractedSpanContext(Context context) {
320-
AgentSpan extractedSpan = AgentSpan.fromContext(context);
321-
return extractedSpan == null ? null : (AgentSpanContext.Extracted) extractedSpan.context();
320+
protected static AgentSpanContext.Extracted getExtractedSpanContext(Context parentContext) {
321+
AgentSpan extractedSpan = AgentSpan.fromContext(parentContext);
322+
if (extractedSpan != null) {
323+
AgentSpanContext extractedSpanContext = extractedSpan.context();
324+
if (extractedSpanContext instanceof AgentSpanContext.Extracted) {
325+
return (AgentSpanContext.Extracted) extractedSpanContext;
326+
} else {
327+
log.warn("Expected AgentSpanContext.Extracted but found {}", extractedSpanContext);
328+
}
329+
}
330+
return null;
322331
}
323332

324333
protected BlockResponseFunction createBlockResponseFunction(

dd-java-agent/instrumentation/play-2.3/src/main/java/datadog/trace/instrumentation/play23/PlayHttpServerDecorator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ public AgentSpan onRequest(
8787
final AgentSpan span,
8888
final Request<?> connection,
8989
final Request<?> request,
90-
final Context context) {
91-
super.onRequest(span, connection, request, context);
90+
final Context parentContext) {
91+
super.onRequest(span, connection, request, parentContext);
9292
if (request != null) {
9393
// more about routes here:
9494
// https://github.com/playframework/playframework/blob/master/documentation/manual/releases/release26/migration26/Migration26.md#router-tags-are-now-attributes

dd-java-agent/instrumentation/play-2.4/src/main/java/datadog/trace/instrumentation/play24/PlayHttpServerDecorator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ public AgentSpan onRequest(
8787
final AgentSpan span,
8888
final Request<?> connection,
8989
final Request<?> request,
90-
final Context context) {
91-
super.onRequest(span, connection, request, context);
90+
final Context parentContext) {
91+
super.onRequest(span, connection, request, parentContext);
9292
if (request != null) {
9393
// more about routes here:
9494
// https://github.com/playframework/playframework/blob/master/documentation/manual/releases/release26/migration26/Migration26.md#router-tags-are-now-attributes

dd-java-agent/instrumentation/play-2.6/src/main/java/datadog/trace/instrumentation/play26/PlayHttpServerDecorator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ public AgentSpan onRequest(
130130
final AgentSpan span,
131131
final Request<?> connection,
132132
final Request<?> request,
133-
final Context context) {
134-
super.onRequest(span, connection, request, context);
133+
final Context parentContext) {
134+
super.onRequest(span, connection, request, parentContext);
135135
if (request != null) {
136136
// more about routes here:
137137
// https://github.com/playframework/playframework/blob/master/documentation/manual/releases/release26/migration26/Migration26.md

dd-java-agent/instrumentation/servlet/request-2/src/main/java/datadog/trace/instrumentation/servlet2/Servlet2Decorator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ public AgentSpan onRequest(
7979
final AgentSpan span,
8080
final HttpServletRequest connection,
8181
final HttpServletRequest request,
82-
final Context context) {
82+
final Context parentContext) {
8383
assert span != null;
8484
if (request != null) {
8585
span.setTag("servlet.context", request.getContextPath());
8686
span.setTag("servlet.path", request.getServletPath());
8787
}
88-
return super.onRequest(span, connection, request, context);
88+
return super.onRequest(span, connection, request, parentContext);
8989
}
9090
}

dd-java-agent/instrumentation/servlet/request-3/src/main/java/datadog/trace/instrumentation/servlet3/Servlet3Decorator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public AgentSpan onRequest(
8282
final AgentSpan span,
8383
final HttpServletRequest connection,
8484
final HttpServletRequest request,
85-
final Context context) {
85+
final Context parentContext) {
8686
assert span != null;
8787
ClassloaderConfigurationOverrides.maybeEnrichSpan(span);
8888
if (request != null) {
@@ -97,7 +97,7 @@ public AgentSpan onRequest(
9797
request.setAttribute(DD_CONTEXT_PATH_ATTRIBUTE, contextPath);
9898
request.setAttribute(DD_SERVLET_PATH_ATTRIBUTE, servletPath);
9999
}
100-
return super.onRequest(span, connection, request, context);
100+
return super.onRequest(span, connection, request, parentContext);
101101
}
102102

103103
@Override

dd-java-agent/instrumentation/spring/spring-webmvc/spring-webmvc-3.1/src/main/java/datadog/trace/instrumentation/springweb/SpringWebHttpServerDecorator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public AgentSpan onRequest(
9797
final AgentSpan span,
9898
final HttpServletRequest connection,
9999
final HttpServletRequest request,
100-
final Context context) {
100+
final Context parentContext) {
101101
if (request != null) {
102102
final String method = request.getMethod();
103103
final Object bestMatchingPattern =

dd-java-agent/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/src/main/java17/datadog/trace/instrumentation/springweb6/SpringWebHttpServerDecorator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public AgentSpan onRequest(
101101
final AgentSpan span,
102102
final HttpServletRequest connection,
103103
final HttpServletRequest request,
104-
final Context context) {
104+
final Context parentContext) {
105105
// FIXME: adding a filter to avoid resource name to be overridden on redirect and forwards.
106106
// Remove myself when jakarta.servlet will be available
107107
if (request != null && request.getAttribute(DD_FILTERED_SPRING_ROUTE_ALREADY_APPLIED) == null) {

dd-java-agent/instrumentation/synapse-3/src/main/java/datadog/trace/instrumentation/synapse3/SynapseServerInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static ContextScope beginRequest(
7272
ContextScope scope = context.attach();
7373
AgentSpan span = spanFromContext(context);
7474
DECORATE.afterStart(span);
75-
DECORATE.onRequest(span, connection, request, context);
75+
DECORATE.onRequest(span, connection, request, parentContext);
7676

7777
// capture span to be finished by one of the various server response advices
7878
connection.getContext().setAttribute(SYNAPSE_SPAN_KEY, span);

dd-java-agent/instrumentation/tomcat-5.5-common/src/main/java/datadog/trace/instrumentation/tomcat/TomcatDecorator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public AgentSpan onRequest(
9494
final AgentSpan span,
9595
final Request connection,
9696
final Request request,
97-
final Context context) {
97+
final Context parentContext) {
9898
if (request != null) {
9999
String contextPath = request.getContextPath();
100100
String servletPath = request.getServletPath();
@@ -112,7 +112,7 @@ public AgentSpan onRequest(
112112
request.setAttribute(DD_CONTEXT_PATH_ATTRIBUTE, contextPath);
113113
request.setAttribute(DD_SERVLET_PATH_ATTRIBUTE, servletPath);
114114
}
115-
return super.onRequest(span, connection, request, context);
115+
return super.onRequest(span, connection, request, parentContext);
116116
}
117117

118118
@Override

0 commit comments

Comments
 (0)