Skip to content

Commit 8fcf551

Browse files
committed
Take the proper span context into account for OTel tracecontext generation
Signed-off-by: Bob Weinand <[email protected]>
1 parent 5799170 commit 8fcf551

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

src/DDTrace/OpenTelemetry/SpanContext.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ public function getSpanIdBinary(): string
7474

7575
public function getTraceState(): ?TraceStateInterface
7676
{
77-
$traceContext = generate_distributed_tracing_headers(['tracecontext']);
77+
$current = \DDTrace\active_stack();
78+
if ($current !== $this->span->stack) {
79+
\DDTrace\switch_stack($this->span);
80+
$traceContext = generate_distributed_tracing_headers(['tracecontext']);
81+
\DDTrace\switch_stack($current);
82+
} else {
83+
$traceContext = generate_distributed_tracing_headers(['tracecontext']);
84+
}
7885
return new TraceState($traceContext['tracestate'] ?? null);
7986
}
8087

tests/OpenTelemetry/Integration/API/TracerTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,9 @@ public function testGetSpanContextWithMultipleTraceStates()
543543
'_dd.p.congo' => 't61rcWkgMzE',
544544
'_dd.p.some_val' => 'tehehe'
545545
]);
546-
$this->assertRegularExpression('/^dd=p:[0-9a-f]{16};t.dm:-0;t.congo:t61rcWkgMzE;t.some_val:tehehe$/', (string)$span->getContext()->getTraceState());
546+
$this->assertRegularExpression('/^dd=p:[0-9a-f]{16};t.congo:t61rcWkgMzE;t.some_val:tehehe;t.dm:-0$/', (string)$span->getContext()->getTraceState());
547547
$span->end();
548-
$this->assertRegularExpression('/^dd=p:[0-9a-f]{16};t.dm:-0;t.congo:t61rcWkgMzE;t.some_val:tehehe$/', (string)$span->getContext()->getTraceState());
548+
$this->assertRegularExpression('/^dd=p:[0-9a-f]{16};t.congo:t61rcWkgMzE;t.some_val:tehehe;t.dm:-0$/', (string)$span->getContext()->getTraceState());
549549
});
550550

551551
$span = $traces[0][0];
@@ -589,7 +589,7 @@ public function testGetSpanContextWithRemoteParent(int $traceFlags, ?TraceState
589589
$this->assertSame($remoteContext->getSpanId(), $child->getParentContext()->getSpanId());
590590
$this->assertFalse($childContext->isRemote()); // "When creating children from remote spans, their IsRemote flag MUST be set to false."
591591
$this->assertEquals(1, $childContext->getTraceFlags()); // RECORD_AND_SAMPLED ==> 01 (AlwaysOn sampler)
592-
$this->assertSame(sprintf("dd=p:%016X;t.dm:-0", $child->getContext()->getSpanID()) . ($traceState ? ",$traceState" : ""), (string)$childContext->getTraceState());
592+
$this->assertSame("dd=p:" . $child->getContext()->getSpanID() . ";t.dm:-0" . ($traceState ? ",$traceState" : ""), (string)$childContext->getTraceState());
593593
});
594594

595595
$span = $traces[0][0];
@@ -678,7 +678,7 @@ public function getDescription(): string
678678
$this->assertFalse($childContext->isRemote()); // "When creating children from remote spans, their IsRemote flag MUST be set to false."
679679
$this->assertEquals(1, $childContext->getTraceFlags()); // RECORD_AND_SAMPLED ==> 01 (AlwaysOn sampler)
680680
$this->assertRegularExpression('/^dd=p:[0-9a-f]{16};t.dm:-0,localparent=yes,rojo=00f067aa0ba902b7,congo=t61rcWkgMzE$/', (string)$childContext->getTraceState());
681-
$this->assertRegularExpression('/^dd=p:[0-9a-f]{16};t.dm:-0,localparent=yes,rojo=00f067aa0ba902b7,congo=t61rcWkgMzE$/', (string)$parent->getContext()->getTraceState());
681+
$this->assertRegularExpression('/^dd=p:[0-9a-f]{16};t.dm:-0;t.some_val:tehehe,root=yes,rojo=00f067aa0ba902b7,congo=t61rcWkgMzE$/', (string)$parent->getContext()->getTraceState());
682682

683683
$grandChild = $tracer->spanBuilder("grandChild")
684684
->setParent(Context::getCurrent()->withContextValue($child))
@@ -812,7 +812,7 @@ public function getDescription(): string
812812
$grandChild = $tracer->spanBuilder("grandChild")
813813
->setParent(Context::getCurrent()->withContextValue($child))
814814
->startSpan();
815-
$expected_tracestate = sprintf("dd=p:%016X;t.dm:-0,remoteparent=yes,rojo=00f067aa0ba902b7,congo=t61rcWkgMzE", $child->getContext()->getSpanId());
815+
$expected_tracestate = "dd=p:" . $childContext->getSpanId() . ";t.dm:-0,remoteparent=yes,rojo=00f067aa0ba902b7,congo=t61rcWkgMzE";
816816
$this->assertSame($expected_tracestate, (string)$child->getContext()->getTraceState());
817817
$grandChildScope = $grandChild->activate();
818818

@@ -821,9 +821,9 @@ public function getDescription(): string
821821
$this->assertSame($child->getContext()->getSpanId(), $grandChild->getParentContext()->getSpanId());
822822
$this->assertFalse($grandChildContext->isRemote()); // "When creating children from remote spans, their IsRemote flag MUST be set to false."
823823
$this->assertEquals(1, $grandChildContext->getTraceFlags()); // RECORD_AND_SAMPLED ==> 01 (AlwaysOn sampler)
824-
$expected_tracestate =sprintf("dd=p:%016X;t.dm:-0,remoteparent=yes,rojo=00f067aa0ba902b7,congo=t61rcWkgMzE", $child->getSpanId());
824+
$expected_tracestate = "dd=p:" . $childContext->getSpanId() . ";t.dm:-0,remoteparent=yes,rojo=00f067aa0ba902b7,congo=t61rcWkgMzE";
825825
$this->assertSame($expected_tracestate, (string)$child->getContext()->getTraceState());
826-
$expected_tracestate =sprintf("dd=p:%016X;t.dm:-0,remoteparent=yes,rojo=00f067aa0ba902b7,congo=t61rcWkgMzE", $grandChildContext->getSpanId());
826+
$expected_tracestate = "dd=p:" . $grandChildContext->getSpanId(). ";t.dm:-0,remoteparent=yes,rojo=00f067aa0ba902b7,congo=t61rcWkgMzE";
827827
$this->assertSame($expected_tracestate, (string)$grandChildContext->getTraceState());
828828

829829
$grandChildScope->detach();
@@ -856,7 +856,7 @@ public function testAddItemToTracestate()
856856
'_dd.p.congo' => 't61rcWkgMzE',
857857
]);
858858

859-
$this->assertRegularExpression('/^dd=p:[0-9a-f]{16};t.dm:-0;t.congo:t61rcWkgMzE$/', (string)$span->getContext()->getTraceState());
859+
$this->assertRegularExpression('/^dd=p:[0-9a-f]{16};t.congo:t61rcWkgMzE;t.dm:-0$/', (string)$span->getContext()->getTraceState());
860860

861861
$traceState = $span->getContext()->getTraceState()->with('rojo', '00f067aa0ba902b7');
862862
$context = SpanContext::create(
@@ -870,7 +870,7 @@ public function testAddItemToTracestate()
870870
->setParent(Context::getCurrent()->withContextValue(Span::wrap($context)))
871871
->startSpan();
872872

873-
$this->assertRegularExpression('/^dd=p:[0-9a-f]{16};t.dm:-0;t.congo:t61rcWkgMzE,rojo=00f067aa0ba902b7$/', (string)$child->getContext()->getTraceState());
873+
$this->assertRegularExpression('/^dd=p:[0-9a-f]{16};t.congo:t61rcWkgMzE;t.dm:-0,rojo=00f067aa0ba902b7$/', (string)$child->getContext()->getTraceState());
874874

875875
$child->end();
876876
$span->end();

tests/OpenTelemetry/Integration/SDK/TracerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function test_sampler_may_override_parents_trace_state(): void
7979
$span = $tracer->spanBuilder('test.span')->setParent($parentContext)->startSpan();
8080

8181
$this->assertNotEquals($parentTraceState, $span->getContext()->getTraceState());
82-
$this->assertEquals(sprintf("dd=p:%016X;t.dm:-0,new-key=new_value", $span->getContext()->getSpanID()), (string)$span->getContext()->getTraceState());
82+
$this->assertEquals("dd=p:" . $span->getContext()->getSpanID() . ";t.dm:-0,new-key=new_value", (string)$span->getContext()->getTraceState());
8383
});
8484
}
8585

0 commit comments

Comments
 (0)