44import static datadog .trace .api .DDTags .DJM_ENABLED ;
55import static datadog .trace .api .DDTags .DSM_ENABLED ;
66import static datadog .trace .api .DDTags .PROFILING_CONTEXT_ENGINE ;
7+ import static datadog .trace .api .TracePropagationBehaviorExtract .RESTART ;
78import static datadog .trace .bootstrap .instrumentation .api .AgentPropagation .BAGGAGE_CONCERN ;
89import static datadog .trace .bootstrap .instrumentation .api .AgentPropagation .DSM_CONCERN ;
910import static datadog .trace .bootstrap .instrumentation .api .AgentPropagation .TRACING_CONCERN ;
3132import datadog .trace .api .IdGenerationStrategy ;
3233import datadog .trace .api .StatsDClient ;
3334import datadog .trace .api .TraceConfig ;
34- import datadog .trace .api .TracePropagationBehaviorExtract ;
3535import datadog .trace .api .config .GeneralConfig ;
3636import datadog .trace .api .datastreams .AgentDataStreamsMonitoring ;
3737import datadog .trace .api .datastreams .PathwayContext ;
@@ -1326,7 +1326,6 @@ public CoreSpanBuilder ignoreActiveSpan() {
13261326 }
13271327
13281328 private DDSpan buildSpan () {
1329- addTerminatedContextAsLinks ();
13301329 DDSpan span = DDSpan .create (instrumentationName , timestampMicro , buildSpanContext (), links );
13311330 if (span .isLocalRootSpan ()) {
13321331 EndpointTracker tracker = tracer .onRootSpanStarted (span );
@@ -1337,6 +1336,22 @@ private DDSpan buildSpan() {
13371336 return span ;
13381337 }
13391338
1339+ private void addParentContextAsLinks (AgentSpanContext parentContext ) {
1340+ SpanLink link ;
1341+ if (parentContext instanceof ExtractedContext ) {
1342+ String headers = ((ExtractedContext ) parentContext ).getPropagationStyle ().toString ();
1343+ SpanAttributes attributes =
1344+ SpanAttributes .builder ()
1345+ .put ("reason" , "propagation_behavior_extract" )
1346+ .put ("context_headers" , headers )
1347+ .build ();
1348+ link = DDSpanLink .from ((ExtractedContext ) parentContext , attributes );
1349+ } else {
1350+ link = SpanLink .from (parentContext );
1351+ }
1352+ withLink (link );
1353+ }
1354+
13401355 private void addTerminatedContextAsLinks () {
13411356 if (this .parent instanceof TagContext ) {
13421357 List <AgentSpanLink > terminatedContextLinks =
@@ -1504,6 +1519,7 @@ private DDSpanContext buildSpanContext() {
15041519 spanId = this .spanId ;
15051520 }
15061521
1522+ // Find the parent context
15071523 AgentSpanContext parentContext = parent ;
15081524 if (parentContext == null && !ignoreScope ) {
15091525 // use the Scope as parent unless overridden or ignored.
@@ -1512,35 +1528,23 @@ private DDSpanContext buildSpanContext() {
15121528 parentContext = activeSpan .context ();
15131529 }
15141530 }
1515-
1516- String parentServiceName = null ;
1517- boolean isRemote = false ;
1518-
1519- TracePropagationBehaviorExtract behaviorExtract =
1520- Config .get ().getTracePropagationBehaviorExtract ();
1531+ // Handle remote terminated context as span links
15211532 if (parentContext != null && parentContext .isRemote ()) {
1522- if (behaviorExtract == TracePropagationBehaviorExtract .IGNORE ) {
1523- // reset links that may have come terminated span links
1524- links = new ArrayList <>();
1525- parentContext = null ;
1526- } else if (behaviorExtract == TracePropagationBehaviorExtract .RESTART ) {
1527- links = new ArrayList <>();
1528- SpanLink link =
1529- (parentContext instanceof ExtractedContext )
1530- ? DDSpanLink .from (
1531- (ExtractedContext ) parentContext ,
1532- SpanAttributes .builder ()
1533- .put ("reason" , "propagation_behavior_extract" )
1534- .put (
1535- "context_headers" ,
1536- ((ExtractedContext ) parentContext ).getPropagationStyle ().toString ())
1537- .build ())
1538- : SpanLink .from (parentContext );
1539- links .add (link );
1540- parentContext = null ;
1533+ switch (Config .get ().getTracePropagationBehaviorExtract ()) {
1534+ case RESTART :
1535+ addParentContextAsLinks (parentContext );
1536+ parentContext = null ;
1537+ break ;
1538+ case IGNORE :
1539+ parentContext = null ;
1540+ break ;
1541+ case CONTINUE :
1542+ default :
1543+ addTerminatedContextAsLinks ();
15411544 }
15421545 }
15431546
1547+ String parentServiceName = null ;
15441548 // Propagate internal trace.
15451549 // Note: if we are not in the context of distributed tracing and we are starting the first
15461550 // root span, parentContext will be null at this point.
@@ -1574,7 +1578,6 @@ private DDSpanContext buildSpanContext() {
15741578
15751579 if (parentContext instanceof ExtractedContext ) {
15761580 // Propagate external trace
1577- isRemote = true ;
15781581 final ExtractedContext extractedContext = (ExtractedContext ) parentContext ;
15791582 traceId = extractedContext .getTraceId ();
15801583 parentSpanId = extractedContext .getSpanId ();
@@ -1715,8 +1718,7 @@ private DDSpanContext buildSpanContext() {
17151718 disableSamplingMechanismValidation ,
17161719 propagationTags ,
17171720 profilingContextIntegration ,
1718- injectBaggageAsTags ,
1719- isRemote );
1721+ injectBaggageAsTags );
17201722
17211723 // By setting the tags on the context we apply decorators to any tags that have been set via
17221724 // the builder. This is the order that the tags were added previously, but maybe the `tags`
0 commit comments