88import static datadog .trace .bootstrap .instrumentation .decorator .http .HttpResourceDecorator .HTTP_RESOURCE_DECORATOR ;
99
1010import datadog .appsec .api .blocking .BlockingException ;
11- import datadog .context .Context ;
1211import datadog .context .InferredProxyContext ;
1312import datadog .context .propagation .Propagators ;
1413import datadog .trace .api .Config ;
@@ -529,8 +528,7 @@ public AgentSpanContext.Extracted extract(REQUEST_CARRIER carrier) {
529528 if (null == carrier || null == getter ) {
530529 return null ;
531530 }
532- Context c = Propagators .defaultPropagator ().extract (Context .root (), carrier , getter );
533- c .attach ();
531+
534532 return extractContextAndGetSpanContext (carrier , getter );
535533 }
536534
@@ -541,40 +539,67 @@ public AgentSpan startSpan(REQUEST_CARRIER carrier, AgentSpanContext.Extracted c
541539 }
542540
543541 public AgentSpan startSpan (
544- String instrumentationName , REQUEST_CARRIER carrier , AgentSpanContext .Extracted context ) {
542+ String instrumentationName ,
543+ REQUEST_CARRIER carrier ,
544+ AgentSpanContext .Extracted standardExtractedContext ) {
545545 boolean addInferredProxy = Config .get ().isInferredProxyPropagationEnabled ();
546546 AgentSpan apiGtwSpan = null ;
547+
547548 if (addInferredProxy ) {
548- apiGtwSpan = startSpanWithInferredProxy (instrumentationName , carrier , context );
549+ // Locally extract the full datadog.context.Context for inferred proxy information
550+ AgentPropagation .ContextVisitor <REQUEST_CARRIER > getter =
551+ getter (); // Ensure getter is available
552+ datadog .context .Context fullContextForInferredProxy = datadog .context .Context .root ();
553+ if (carrier != null && getter != null ) {
554+ fullContextForInferredProxy =
555+ Propagators .defaultPropagator ()
556+ .extract (datadog .context .Context .root (), carrier , getter );
557+ }
558+ // Pass the locally extracted fullContextForInferredProxy and the standardExtractedContext
559+ apiGtwSpan =
560+ startSpanWithInferredProxy (
561+ instrumentationName , fullContextForInferredProxy , standardExtractedContext );
549562 }
550563
551- AgentSpan span =
564+ AgentSpan serverSpan =
552565 tracer ()
553566 .startSpan (
554567 instrumentationName ,
555568 spanName (),
556- apiGtwSpan != null ? apiGtwSpan .context () : callIGCallbackStart (context ))
569+ // Parent serverSpan to apiGtwSpan if it exists, otherwise to
570+ // standardExtractedContext
571+ apiGtwSpan != null
572+ ? apiGtwSpan .context ()
573+ : callIGCallbackStart (standardExtractedContext ))
557574 .setMeasured (true );
558- Flow <Void > flow = callIGCallbackRequestHeaders (span , carrier );
575+ Flow <Void > flow = callIGCallbackRequestHeaders (serverSpan , carrier );
559576 if (flow .getAction () instanceof Flow .Action .RequestBlockingAction ) {
560- span .setRequestBlockingAction ((Flow .Action .RequestBlockingAction ) flow .getAction ());
577+ serverSpan .setRequestBlockingAction ((Flow .Action .RequestBlockingAction ) flow .getAction ());
561578 }
562- AgentPropagation .ContextVisitor <REQUEST_CARRIER > getter = getter ();
563- if (null != carrier && null != getter ) {
564- tracer ().getDataStreamsMonitoring ().setCheckpoint (span , fromTags (SERVER_PATHWAY_EDGE_TAGS ));
579+ // Ensure getter() is available for DSM checkpoint; it was obtained above if addInferredProxy
580+ // was true.
581+ // If not, get it again. This logic might need refinement if getter() is expensive, but for now,
582+ // direct call.
583+ if (null != carrier && null != getter ()) {
584+ tracer ()
585+ .getDataStreamsMonitoring ()
586+ .setCheckpoint (serverSpan , fromTags (SERVER_PATHWAY_EDGE_TAGS ));
565587 }
566588
567589 if (addInferredProxy && apiGtwSpan != null ) {
568- return new InferredProxySpanGroup (apiGtwSpan , span );
590+ return new InferredProxySpanGroup (apiGtwSpan , serverSpan );
569591 } else {
570- return span ;
592+ return serverSpan ;
571593 }
572594 }
573595
574596 private AgentSpan startSpanWithInferredProxy (
575- String instrumentationName , REQUEST_CARRIER carrier , AgentSpanContext .Extracted context ) {
597+ String instrumentationName ,
598+ datadog .context .Context fullContextForInferredProxy ,
599+ AgentSpanContext .Extracted standardExtractedContext ) {
576600
577- InferredProxyContext inferredProxy = InferredProxyContext .fromContext (Context .current ());
601+ InferredProxyContext inferredProxy =
602+ InferredProxyContext .fromContext (fullContextForInferredProxy );
578603
579604 if (inferredProxy == null ) {
580605 return null ;
@@ -604,7 +629,7 @@ private AgentSpan startSpanWithInferredProxy(
604629 .startSpan (
605630 "inferred_proxy" ,
606631 SUPPORTED_PROXIES .get (proxySystem ),
607- callIGCallbackStart (context ),
632+ callIGCallbackStart (standardExtractedContext ),
608633 startTime );
609634
610635 apiGtwSpan .setTag (Tags .COMPONENT , proxySystem );
0 commit comments