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 ;
@@ -1337,7 +1337,6 @@ public CoreSpanBuilder ignoreActiveSpan() {
13371337 }
13381338
13391339 private DDSpan buildSpan () {
1340- addTerminatedContextAsLinks ();
13411340 DDSpan span = DDSpan .create (instrumentationName , timestampMicro , buildSpanContext (), links );
13421341 if (span .isLocalRootSpan ()) {
13431342 EndpointTracker tracker = tracer .onRootSpanStarted (span );
@@ -1348,6 +1347,22 @@ private DDSpan buildSpan() {
13481347 return span ;
13491348 }
13501349
1350+ private void addParentContextAsLinks (AgentSpanContext parentContext ) {
1351+ SpanLink link ;
1352+ if (parentContext instanceof ExtractedContext ) {
1353+ String headers = ((ExtractedContext ) parentContext ).getPropagationStyle ().toString ();
1354+ SpanAttributes attributes =
1355+ SpanAttributes .builder ()
1356+ .put ("reason" , "propagation_behavior_extract" )
1357+ .put ("context_headers" , headers )
1358+ .build ();
1359+ link = DDSpanLink .from ((ExtractedContext ) parentContext , attributes );
1360+ } else {
1361+ link = SpanLink .from (parentContext );
1362+ }
1363+ withLink (link );
1364+ }
1365+
13511366 private void addTerminatedContextAsLinks () {
13521367 if (this .parent instanceof TagContext ) {
13531368 List <AgentSpanLink > terminatedContextLinks =
@@ -1515,6 +1530,7 @@ private DDSpanContext buildSpanContext() {
15151530 spanId = this .spanId ;
15161531 }
15171532
1533+ // Find the parent context
15181534 AgentSpanContext parentContext = parent ;
15191535 if (parentContext == null && !ignoreScope ) {
15201536 // use the Scope as parent unless overridden or ignored.
@@ -1523,35 +1539,23 @@ private DDSpanContext buildSpanContext() {
15231539 parentContext = activeSpan .context ();
15241540 }
15251541 }
1526-
1527- String parentServiceName = null ;
1528- boolean isRemote = false ;
1529-
1530- TracePropagationBehaviorExtract behaviorExtract =
1531- Config .get ().getTracePropagationBehaviorExtract ();
1542+ // Handle remote terminated context as span links
15321543 if (parentContext != null && parentContext .isRemote ()) {
1533- if (behaviorExtract == TracePropagationBehaviorExtract .IGNORE ) {
1534- // reset links that may have come terminated span links
1535- links = new ArrayList <>();
1536- parentContext = null ;
1537- } else if (behaviorExtract == TracePropagationBehaviorExtract .RESTART ) {
1538- links = new ArrayList <>();
1539- SpanLink link =
1540- (parentContext instanceof ExtractedContext )
1541- ? DDSpanLink .from (
1542- (ExtractedContext ) parentContext ,
1543- SpanAttributes .builder ()
1544- .put ("reason" , "propagation_behavior_extract" )
1545- .put (
1546- "context_headers" ,
1547- ((ExtractedContext ) parentContext ).getPropagationStyle ().toString ())
1548- .build ())
1549- : SpanLink .from (parentContext );
1550- links .add (link );
1551- parentContext = null ;
1544+ switch (Config .get ().getTracePropagationBehaviorExtract ()) {
1545+ case RESTART :
1546+ addParentContextAsLinks (parentContext );
1547+ parentContext = null ;
1548+ break ;
1549+ case IGNORE :
1550+ parentContext = null ;
1551+ break ;
1552+ case CONTINUE :
1553+ default :
1554+ addTerminatedContextAsLinks ();
15521555 }
15531556 }
15541557
1558+ String parentServiceName = null ;
15551559 // Propagate internal trace.
15561560 // Note: if we are not in the context of distributed tracing and we are starting the first
15571561 // root span, parentContext will be null at this point.
@@ -1585,7 +1589,6 @@ private DDSpanContext buildSpanContext() {
15851589
15861590 if (parentContext instanceof ExtractedContext ) {
15871591 // Propagate external trace
1588- isRemote = true ;
15891592 final ExtractedContext extractedContext = (ExtractedContext ) parentContext ;
15901593 traceId = extractedContext .getTraceId ();
15911594 parentSpanId = extractedContext .getSpanId ();
@@ -1726,8 +1729,7 @@ private DDSpanContext buildSpanContext() {
17261729 disableSamplingMechanismValidation ,
17271730 propagationTags ,
17281731 profilingContextIntegration ,
1729- injectBaggageAsTags ,
1730- isRemote );
1732+ injectBaggageAsTags );
17311733
17321734 // By setting the tags on the context we apply decorators to any tags that have been set via
17331735 // the builder. This is the order that the tags were added previously, but maybe the `tags`
0 commit comments