Skip to content

Commit ee0878f

Browse files
MilanGarnierclaude
andcommitted
test: add phpt tests for DD_TRACE_PROPAGATION_BEHAVIOR_EXTRACT
Covers the three behaviors (continue, restart, ignore) and config parsing (case-insensitive values, invalid value falls back to default). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1071d94 commit ee0878f

4 files changed

Lines changed: 176 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--TEST--
2+
DD_TRACE_PROPAGATION_BEHAVIOR_EXTRACT config parsing: case-insensitive, invalid falls back to continue
3+
--ENV--
4+
DD_TRACE_GENERATE_ROOT_SPAN=0
5+
--FILE--
6+
<?php
7+
8+
// Helper: return trace_id after consuming upstream context with a given config value
9+
function check_behavior(string $config_value): string {
10+
ini_set('datadog.trace.propagation_behavior_extract', $config_value);
11+
12+
DDTrace\consume_distributed_tracing_headers([
13+
"x-datadog-trace-id" => 42,
14+
"x-datadog-parent-id" => 10,
15+
]);
16+
17+
$span = DDTrace\start_span();
18+
$result = DDTrace\root_span()->traceId === "0000000000000000000000000000002a" ? "continue" : "restart_or_ignore";
19+
DDTrace\close_span();
20+
21+
return $result;
22+
}
23+
24+
// Lowercase values
25+
echo "continue: " . check_behavior("continue") . "\n";
26+
echo "restart: " . check_behavior("restart") . "\n";
27+
echo "ignore: " . check_behavior("ignore") . "\n";
28+
29+
// Case-insensitive
30+
echo "CONTINUE: " . check_behavior("CONTINUE") . "\n";
31+
echo "RESTART: " . check_behavior("RESTART") . "\n";
32+
echo "Ignore: " . check_behavior("Ignore") . "\n";
33+
34+
// Invalid value falls back to default (continue)
35+
echo "invalid: " . check_behavior("invalid_value") . "\n";
36+
37+
?>
38+
--EXPECT--
39+
continue: continue
40+
restart: restart_or_ignore
41+
ignore: restart_or_ignore
42+
CONTINUE: continue
43+
RESTART: restart_or_ignore
44+
Ignore: restart_or_ignore
45+
invalid: continue
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
DD_TRACE_PROPAGATION_BEHAVIOR_EXTRACT=continue inherits upstream context
3+
--ENV--
4+
DD_TRACE_GENERATE_ROOT_SPAN=0
5+
DD_TRACE_PROPAGATION_BEHAVIOR_EXTRACT=continue
6+
--FILE--
7+
<?php
8+
9+
DDTrace\consume_distributed_tracing_headers([
10+
"x-datadog-trace-id" => 42,
11+
"x-datadog-parent-id" => 10,
12+
"x-datadog-sampling-priority" => 1,
13+
"baggage" => "user.id=123",
14+
]);
15+
16+
$span = DDTrace\start_span();
17+
$root = DDTrace\root_span();
18+
19+
echo "trace_id: " . $root->traceId . "\n";
20+
echo "parent_id: " . $root->parentId . "\n";
21+
echo "links_count: " . count($root->links) . "\n";
22+
23+
$headers = DDTrace\generate_distributed_tracing_headers(['baggage']);
24+
echo "baggage: " . ($headers['baggage'] ?? 'none') . "\n";
25+
26+
DDTrace\close_span();
27+
?>
28+
--EXPECT--
29+
trace_id: 0000000000000000000000000000002a
30+
parent_id: 10
31+
links_count: 0
32+
baggage: user.id=123
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
DD_TRACE_PROPAGATION_BEHAVIOR_EXTRACT=ignore drops all incoming context including baggage
3+
--ENV--
4+
DD_TRACE_GENERATE_ROOT_SPAN=0
5+
DD_TRACE_PROPAGATION_BEHAVIOR_EXTRACT=ignore
6+
--FILE--
7+
<?php
8+
9+
DDTrace\consume_distributed_tracing_headers([
10+
"x-datadog-trace-id" => 42,
11+
"x-datadog-parent-id" => 10,
12+
"x-datadog-sampling-priority" => 2,
13+
"x-datadog-tags" => "_dd.p.dm=-4",
14+
"baggage" => "user.id=123",
15+
]);
16+
17+
$span = DDTrace\start_span();
18+
$root = DDTrace\root_span();
19+
20+
// fresh trace: not from upstream
21+
echo "same_as_upstream: " . ($root->traceId === "0000000000000000000000000000002a" ? "yes" : "no") . "\n";
22+
echo "parent_id: " . $root->parentId . "\n";
23+
24+
// no span link (context discarded entirely)
25+
echo "links_count: " . count($root->links) . "\n";
26+
27+
// baggage dropped
28+
$headers = DDTrace\generate_distributed_tracing_headers(['baggage']);
29+
echo "baggage: " . ($headers['baggage'] ?? 'none') . "\n";
30+
31+
// upstream sampling priority not carried over
32+
$dd_headers = DDTrace\generate_distributed_tracing_headers(['datadog']);
33+
echo "sampling_priority: " . ($dd_headers['x-datadog-sampling-priority'] ?? 'none') . "\n";
34+
35+
DDTrace\close_span();
36+
?>
37+
--EXPECT--
38+
same_as_upstream: no
39+
parent_id: 0
40+
links_count: 0
41+
baggage: none
42+
sampling_priority: none
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--TEST--
2+
DD_TRACE_PROPAGATION_BEHAVIOR_EXTRACT=restart starts fresh trace with span link
3+
--ENV--
4+
DD_TRACE_GENERATE_ROOT_SPAN=0
5+
DD_TRACE_PROPAGATION_BEHAVIOR_EXTRACT=restart
6+
DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED=0
7+
DD_TRACE_DEBUG_PRNG_SEED=42
8+
--FILE--
9+
<?php
10+
11+
DDTrace\consume_distributed_tracing_headers([
12+
"x-datadog-trace-id" => 42,
13+
"x-datadog-parent-id" => 10,
14+
"x-datadog-sampling-priority" => 1,
15+
"x-datadog-tags" => "_dd.p.foo=bar",
16+
"baggage" => "user.id=123",
17+
]);
18+
19+
$span = DDTrace\start_span();
20+
$root = DDTrace\root_span();
21+
22+
// fresh trace: different from upstream trace_id 42
23+
echo "same_as_upstream: " . ($root->traceId === "0000000000000000000000000000002a" ? "yes" : "no") . "\n";
24+
25+
// span link attached to root span
26+
echo "links_count: " . count($root->links) . "\n";
27+
28+
$link = $root->links[0] ?? null;
29+
if ($link !== null) {
30+
// link captures upstream trace/span ids
31+
echo "link_trace_id: " . $link->traceId . "\n";
32+
echo "link_span_id: " . $link->spanId . "\n";
33+
echo "link_reason: " . ($link->attributes['reason'] ?? 'missing') . "\n";
34+
// _dd.p.foo not included (was in upstream _dd.p.* tags, now dropped)
35+
echo "link_has_foo: " . (isset($link->attributes['_dd.p.foo']) ? "yes" : "no") . "\n";
36+
}
37+
38+
// baggage preserved
39+
$headers = DDTrace\generate_distributed_tracing_headers(['baggage']);
40+
echo "baggage: " . ($headers['baggage'] ?? 'none') . "\n";
41+
42+
// upstream _dd.p.foo not in outbound tags
43+
$dd_headers = DDTrace\generate_distributed_tracing_headers(['datadog']);
44+
$tags = $dd_headers['x-datadog-tags'] ?? '';
45+
echo "foo_in_tags: " . (str_contains($tags, '_dd.p.foo') ? "yes" : "no") . "\n";
46+
47+
DDTrace\close_span();
48+
?>
49+
--EXPECT--
50+
same_as_upstream: no
51+
links_count: 1
52+
link_trace_id: 0000000000000000000000000000002a
53+
link_span_id: 000000000000000a
54+
link_reason: propagation_behavior_extract
55+
link_has_foo: no
56+
baggage: user.id=123
57+
foo_in_tags: no

0 commit comments

Comments
 (0)