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 ;
@@ -1333,7 +1333,6 @@ public CoreSpanBuilder ignoreActiveSpan() {
13331333 }
13341334
13351335 private DDSpan buildSpan () {
1336- addTerminatedContextAsLinks ();
13371336 DDSpan span = DDSpan .create (instrumentationName , timestampMicro , buildSpanContext (), links );
13381337 if (span .isLocalRootSpan ()) {
13391338 EndpointTracker tracker = tracer .onRootSpanStarted (span );
@@ -1344,6 +1343,22 @@ private DDSpan buildSpan() {
13441343 return span ;
13451344 }
13461345
1346+ private void addParentContextAsLinks (AgentSpanContext parentContext ) {
1347+ SpanLink link ;
1348+ if (parentContext instanceof ExtractedContext ) {
1349+ String headers = ((ExtractedContext ) parentContext ).getPropagationStyle ().toString ();
1350+ SpanAttributes attributes =
1351+ SpanAttributes .builder ()
1352+ .put ("reason" , "propagation_behavior_extract" )
1353+ .put ("context_headers" , headers )
1354+ .build ();
1355+ link = DDSpanLink .from ((ExtractedContext ) parentContext , attributes );
1356+ } else {
1357+ link = SpanLink .from (parentContext );
1358+ }
1359+ withLink (link );
1360+ }
1361+
13471362 private void addTerminatedContextAsLinks () {
13481363 if (this .parent instanceof TagContext ) {
13491364 List <AgentSpanLink > terminatedContextLinks =
@@ -1511,6 +1526,7 @@ private DDSpanContext buildSpanContext() {
15111526 spanId = this .spanId ;
15121527 }
15131528
1529+ // Find the parent context
15141530 AgentSpanContext parentContext = parent ;
15151531 if (parentContext == null && !ignoreScope ) {
15161532 // use the Scope as parent unless overridden or ignored.
@@ -1519,35 +1535,23 @@ private DDSpanContext buildSpanContext() {
15191535 parentContext = activeSpan .context ();
15201536 }
15211537 }
1522-
1523- String parentServiceName = null ;
1524- boolean isRemote = false ;
1525-
1526- TracePropagationBehaviorExtract behaviorExtract =
1527- Config .get ().getTracePropagationBehaviorExtract ();
1538+ // Handle remote terminated context as span links
15281539 if (parentContext != null && parentContext .isRemote ()) {
1529- if (behaviorExtract == TracePropagationBehaviorExtract .IGNORE ) {
1530- // reset links that may have come terminated span links
1531- links = new ArrayList <>();
1532- parentContext = null ;
1533- } else if (behaviorExtract == TracePropagationBehaviorExtract .RESTART ) {
1534- links = new ArrayList <>();
1535- SpanLink link =
1536- (parentContext instanceof ExtractedContext )
1537- ? DDSpanLink .from (
1538- (ExtractedContext ) parentContext ,
1539- SpanAttributes .builder ()
1540- .put ("reason" , "propagation_behavior_extract" )
1541- .put (
1542- "context_headers" ,
1543- ((ExtractedContext ) parentContext ).getPropagationStyle ().toString ())
1544- .build ())
1545- : SpanLink .from (parentContext );
1546- links .add (link );
1547- parentContext = null ;
1540+ switch (Config .get ().getTracePropagationBehaviorExtract ()) {
1541+ case RESTART :
1542+ addParentContextAsLinks (parentContext );
1543+ parentContext = null ;
1544+ break ;
1545+ case IGNORE :
1546+ parentContext = null ;
1547+ break ;
1548+ case CONTINUE :
1549+ default :
1550+ addTerminatedContextAsLinks ();
15481551 }
15491552 }
15501553
1554+ String parentServiceName = null ;
15511555 // Propagate internal trace.
15521556 // Note: if we are not in the context of distributed tracing and we are starting the first
15531557 // root span, parentContext will be null at this point.
@@ -1581,7 +1585,6 @@ private DDSpanContext buildSpanContext() {
15811585
15821586 if (parentContext instanceof ExtractedContext ) {
15831587 // Propagate external trace
1584- isRemote = true ;
15851588 final ExtractedContext extractedContext = (ExtractedContext ) parentContext ;
15861589 traceId = extractedContext .getTraceId ();
15871590 parentSpanId = extractedContext .getSpanId ();
@@ -1722,8 +1725,7 @@ private DDSpanContext buildSpanContext() {
17221725 disableSamplingMechanismValidation ,
17231726 propagationTags ,
17241727 profilingContextIntegration ,
1725- injectBaggageAsTags ,
1726- isRemote );
1728+ injectBaggageAsTags );
17271729
17281730 // By setting the tags on the context we apply decorators to any tags that have been set via
17291731 // the builder. This is the order that the tags were added previously, but maybe the `tags`
0 commit comments