4
4
import static datadog .trace .api .cache .RadixTreeCache .UNSET_STATUS ;
5
5
import static datadog .trace .api .datastreams .DataStreamsContext .forHttpServer ;
6
6
import static datadog .trace .api .gateway .Events .EVENTS ;
7
+ import static datadog .trace .bootstrap .ActiveSubsystems .APPSEC_ACTIVE ;
7
8
import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .traceConfig ;
8
9
import static datadog .trace .bootstrap .instrumentation .decorator .http .HttpResourceDecorator .HTTP_RESOURCE_DECORATOR ;
9
10
17
18
import datadog .trace .api .gateway .BlockResponseFunction ;
18
19
import datadog .trace .api .gateway .CallbackProvider ;
19
20
import datadog .trace .api .gateway .Flow ;
21
+ import datadog .trace .api .gateway .Flow .Action .RequestBlockingAction ;
20
22
import datadog .trace .api .gateway .IGSpanInfo ;
21
23
import datadog .trace .api .gateway .RequestContext ;
22
24
import datadog .trace .api .gateway .RequestContextSlot ;
23
25
import datadog .trace .api .naming .SpanNaming ;
24
- import datadog .trace .bootstrap .ActiveSubsystems ;
25
26
import datadog .trace .bootstrap .instrumentation .api .AgentPropagation ;
26
27
import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
27
28
import datadog .trace .bootstrap .instrumentation .api .AgentSpanContext ;
@@ -146,12 +147,12 @@ public Context startSpan(REQUEST_CARRIER carrier, Context context) {
146
147
instrumentationNames != null && instrumentationNames .length > 0
147
148
? instrumentationNames [0 ]
148
149
: DEFAULT_INSTRUMENTATION_NAME ;
149
- AgentSpanContext .Extracted extracted = callIGCallbackStart (context );
150
+ AgentSpanContext .Extracted extracted = callIGCallbackStart (getExtractedSpanContext ( context ) );
150
151
AgentSpan span =
151
152
tracer ().startSpan (instrumentationName , spanName (), extracted ).setMeasured (true );
152
153
Flow <Void > flow = callIGCallbackRequestHeaders (span , carrier );
153
- if (flow .getAction () instanceof Flow . Action . RequestBlockingAction ) {
154
- span .setRequestBlockingAction ((Flow . Action . RequestBlockingAction ) flow .getAction ());
154
+ if (flow .getAction () instanceof RequestBlockingAction ) {
155
+ span .setRequestBlockingAction ((RequestBlockingAction ) flow .getAction ());
155
156
}
156
157
AgentPropagation .ContextVisitor <REQUEST_CARRIER > getter = getter ();
157
158
if (null != carrier && null != getter ) {
@@ -165,25 +166,9 @@ public AgentSpan onRequest(
165
166
final CONNECTION connection ,
166
167
final REQUEST request ,
167
168
final Context context ) {
168
- return onRequest (span , connection , request , getExtractedSpanContext (context ));
169
- }
170
-
171
- public AgentSpanContext .Extracted getExtractedSpanContext (Context context ) {
172
- AgentSpan extractedSpan = AgentSpan .fromContext (context );
173
- return extractedSpan == null ? null : (AgentSpanContext .Extracted ) extractedSpan .context ();
174
- }
175
-
176
- public AgentSpan onRequest (
177
- final AgentSpan span ,
178
- final CONNECTION connection ,
179
- final REQUEST request ,
180
- @ Nullable final AgentSpanContext .Extracted context ) {
181
169
Config config = Config .get ();
182
- boolean clientIpResolverEnabled =
183
- config .isClientIpEnabled ()
184
- || traceClientIpResolverEnabled && ActiveSubsystems .APPSEC_ACTIVE ;
185
170
186
- if (ActiveSubsystems . APPSEC_ACTIVE ) {
171
+ if (APPSEC_ACTIVE ) {
187
172
RequestContext requestContext = span .getRequestContext ();
188
173
if (requestContext != null ) {
189
174
BlockResponseFunction brf = createBlockResponseFunction (request , connection );
@@ -193,35 +178,38 @@ public AgentSpan onRequest(
193
178
}
194
179
Flow <Void > flow = callIGCallbackRequestSessionId (span , request );
195
180
Flow .Action action = flow .getAction ();
196
- if (action instanceof Flow . Action . RequestBlockingAction ) {
197
- span .setRequestBlockingAction ((Flow . Action . RequestBlockingAction ) flow .getAction ());
181
+ if (action instanceof RequestBlockingAction ) {
182
+ span .setRequestBlockingAction ((RequestBlockingAction ) flow .getAction ());
198
183
}
199
184
}
200
185
201
- if (context != null ) {
186
+ AgentSpanContext .Extracted extracted = getExtractedSpanContext (context );
187
+ boolean clientIpResolverEnabled =
188
+ config .isClientIpEnabled () || traceClientIpResolverEnabled && APPSEC_ACTIVE ;
189
+ if (extracted != null ) {
202
190
if (clientIpResolverEnabled ) {
203
- String forwarded = context .getForwarded ();
191
+ String forwarded = extracted .getForwarded ();
204
192
if (forwarded != null ) {
205
193
span .setTag (Tags .HTTP_FORWARDED , forwarded );
206
194
}
207
- String forwardedProto = context .getXForwardedProto ();
195
+ String forwardedProto = extracted .getXForwardedProto ();
208
196
if (forwardedProto != null ) {
209
197
span .setTag (Tags .HTTP_FORWARDED_PROTO , forwardedProto );
210
198
}
211
- String forwardedHost = context .getXForwardedHost ();
199
+ String forwardedHost = extracted .getXForwardedHost ();
212
200
if (forwardedHost != null ) {
213
201
span .setTag (Tags .HTTP_FORWARDED_HOST , forwardedHost );
214
202
}
215
- String forwardedIp = context .getXForwardedFor ();
203
+ String forwardedIp = extracted .getXForwardedFor ();
216
204
if (forwardedIp != null ) {
217
205
span .setTag (Tags .HTTP_FORWARDED_IP , forwardedIp );
218
206
}
219
- String forwardedPort = context .getXForwardedPort ();
207
+ String forwardedPort = extracted .getXForwardedPort ();
220
208
if (forwardedPort != null ) {
221
209
span .setTag (Tags .HTTP_FORWARDED_PORT , forwardedPort );
222
210
}
223
211
}
224
- String userAgent = context .getUserAgent ();
212
+ String userAgent = extracted .getUserAgent ();
225
213
if (userAgent != null ) {
226
214
span .setTag (Tags .HTTP_USER_AGENT , userAgent );
227
215
}
@@ -245,8 +233,8 @@ public AgentSpan onRequest(
245
233
} else if (supportsRaw ) {
246
234
span .setTag (Tags .HTTP_URL , URIUtils .lazyInvalidUrl (url .raw ()));
247
235
}
248
- if (context != null && context .getXForwardedHost () != null ) {
249
- span .setTag (Tags .HTTP_HOSTNAME , context .getXForwardedHost ());
236
+ if (extracted != null && extracted .getXForwardedHost () != null ) {
237
+ span .setTag (Tags .HTTP_HOSTNAME , extracted .getXForwardedHost ());
250
238
} else if (url .host () != null ) {
251
239
span .setTag (Tags .HTTP_HOSTNAME , url .host ());
252
240
}
@@ -258,8 +246,8 @@ public AgentSpan onRequest(
258
246
span .setTag (DDTags .HTTP_FRAGMENT , url .fragment ());
259
247
}
260
248
Flow <Void > flow = callIGCallbackURI (span , url , method );
261
- if (flow .getAction () instanceof Flow . Action . RequestBlockingAction ) {
262
- span .setRequestBlockingAction ((Flow . Action . RequestBlockingAction ) flow .getAction ());
249
+ if (flow .getAction () instanceof RequestBlockingAction ) {
250
+ span .setRequestBlockingAction ((RequestBlockingAction ) flow .getAction ());
263
251
}
264
252
if (valid && SHOULD_SET_URL_RESOURCE_NAME ) {
265
253
HTTP_RESOURCE_DECORATOR .withServerPath (span , method , path , encoded );
@@ -280,8 +268,8 @@ public AgentSpan onRequest(
280
268
}
281
269
282
270
String inferredAddressStr = null ;
283
- if (clientIpResolverEnabled && context != null ) {
284
- InetAddress inferredAddress = ClientIpAddressResolver .resolve (context , span );
271
+ if (clientIpResolverEnabled && extracted != null ) {
272
+ InetAddress inferredAddress = ClientIpAddressResolver .resolve (extracted , span );
285
273
// the peer address should be used if:
286
274
// 1. the headers yield nothing, regardless of whether it is public or not
287
275
// 2. it is public and the headers yield a private address
@@ -300,9 +288,9 @@ public AgentSpan onRequest(
300
288
span .setTag (Tags .HTTP_CLIENT_IP , inferredAddressStr );
301
289
}
302
290
} else if (clientIpResolverEnabled && span .getLocalRootSpan () != span ) {
303
- // in this case context == null
304
- // If there is no context we can't do anything but use the peer addr.
305
- // Additionally, context == null arises on subspans for which the resolution
291
+ // in this case extracted == null
292
+ // If there is no extracted we can't do anything but use the peer addr.
293
+ // Additionally, extracted == null arises on subspans for which the resolution
306
294
// likely already happened on the top span, so we don't need to do the resolution
307
295
// again. Instead, copy from the top span, should it exist
308
296
AgentSpan localRootSpan = span .getLocalRootSpan ();
@@ -321,13 +309,18 @@ public AgentSpan onRequest(
321
309
}
322
310
setPeerPort (span , peerPort );
323
311
Flow <Void > flow = callIGCallbackAddressAndPort (span , peerIp , peerPort , inferredAddressStr );
324
- if (flow .getAction () instanceof Flow . Action . RequestBlockingAction ) {
325
- span .setRequestBlockingAction ((Flow . Action . RequestBlockingAction ) flow .getAction ());
312
+ if (flow .getAction () instanceof RequestBlockingAction ) {
313
+ span .setRequestBlockingAction ((RequestBlockingAction ) flow .getAction ());
326
314
}
327
315
328
316
return span ;
329
317
}
330
318
319
+ protected static AgentSpanContext .Extracted getExtractedSpanContext (Context context ) {
320
+ AgentSpan extractedSpan = AgentSpan .fromContext (context );
321
+ return extractedSpan == null ? null : (AgentSpanContext .Extracted ) extractedSpan .context ();
322
+ }
323
+
331
324
protected BlockResponseFunction createBlockResponseFunction (
332
325
REQUEST request , CONNECTION connection ) {
333
326
return null ;
@@ -388,8 +381,8 @@ public AgentSpan onResponse(final AgentSpan span, final RESPONSE response) {
388
381
return span ;
389
382
}
390
383
391
- private AgentSpanContext .Extracted callIGCallbackStart (final Context context ) {
392
- AgentSpanContext .Extracted extracted = getExtractedSpanContext ( context );
384
+ private AgentSpanContext .Extracted callIGCallbackStart (
385
+ @ Nullable final AgentSpanContext .Extracted extracted ) {
393
386
AgentTracer .TracerAPI tracer = tracer ();
394
387
Supplier <Flow <Object >> startedCbAppSec =
395
388
tracer .getCallbackProvider (RequestContextSlot .APPSEC ).getCallback (EVENTS .requestStarted ());
0 commit comments