4
4
import static datadog .trace .api .DDTags .DJM_ENABLED ;
5
5
import static datadog .trace .api .DDTags .DSM_ENABLED ;
6
6
import static datadog .trace .api .DDTags .PROFILING_CONTEXT_ENGINE ;
7
+ import static datadog .trace .api .TracePropagationBehaviorExtract .RESTART ;
7
8
import static datadog .trace .bootstrap .instrumentation .api .AgentPropagation .BAGGAGE_CONCERN ;
8
9
import static datadog .trace .bootstrap .instrumentation .api .AgentPropagation .DSM_CONCERN ;
9
10
import static datadog .trace .bootstrap .instrumentation .api .AgentPropagation .TRACING_CONCERN ;
31
32
import datadog .trace .api .IdGenerationStrategy ;
32
33
import datadog .trace .api .StatsDClient ;
33
34
import datadog .trace .api .TraceConfig ;
34
- import datadog .trace .api .TracePropagationBehaviorExtract ;
35
35
import datadog .trace .api .config .GeneralConfig ;
36
36
import datadog .trace .api .datastreams .AgentDataStreamsMonitoring ;
37
37
import datadog .trace .api .datastreams .PathwayContext ;
@@ -1333,7 +1333,6 @@ public CoreSpanBuilder ignoreActiveSpan() {
1333
1333
}
1334
1334
1335
1335
private DDSpan buildSpan () {
1336
- addTerminatedContextAsLinks ();
1337
1336
DDSpan span = DDSpan .create (instrumentationName , timestampMicro , buildSpanContext (), links );
1338
1337
if (span .isLocalRootSpan ()) {
1339
1338
EndpointTracker tracker = tracer .onRootSpanStarted (span );
@@ -1344,6 +1343,22 @@ private DDSpan buildSpan() {
1344
1343
return span ;
1345
1344
}
1346
1345
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
+
1347
1362
private void addTerminatedContextAsLinks () {
1348
1363
if (this .parent instanceof TagContext ) {
1349
1364
List <AgentSpanLink > terminatedContextLinks =
@@ -1511,6 +1526,7 @@ private DDSpanContext buildSpanContext() {
1511
1526
spanId = this .spanId ;
1512
1527
}
1513
1528
1529
+ // Find the parent context
1514
1530
AgentSpanContext parentContext = parent ;
1515
1531
if (parentContext == null && !ignoreScope ) {
1516
1532
// use the Scope as parent unless overridden or ignored.
@@ -1519,35 +1535,18 @@ private DDSpanContext buildSpanContext() {
1519
1535
parentContext = activeSpan .context ();
1520
1536
}
1521
1537
}
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
1528
1539
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 );
1540
+ if (Config .get ().getTracePropagationBehaviorExtract () == RESTART ) {
1541
+ addParentContextAsLinks (parentContext );
1547
1542
parentContext = null ;
1543
+ } else {
1544
+ addTerminatedContextAsLinks ();
1548
1545
}
1549
1546
}
1550
1547
1548
+ String parentServiceName = null ;
1549
+ boolean isRemote = false ; // TODO Simplify?
1551
1550
// Propagate internal trace.
1552
1551
// Note: if we are not in the context of distributed tracing and we are starting the first
1553
1552
// root span, parentContext will be null at this point.
0 commit comments