Skip to content

Commit

Permalink
Fix trace_id reset in DDTrace\set_distributed_tracing_context() (#1863)
Browse files Browse the repository at this point in the history
Fixes #1860.

Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>
  • Loading branch information
bwoebi committed Jan 17, 2023
1 parent fcbcc42 commit 73e32f0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ext/ddtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,7 @@ static void dd_apply_propagated_values_to_existing_spans(void) {
ddtrace_span_data *span = ddtrace_active_span();
while (span) {
zend_array *meta = ddtrace_spandata_property_meta(span);
span->trace_id = DDTRACE_G(distributed_trace_id);
span->trace_id = DDTRACE_G(distributed_trace_id) == 0 ? span->root->span_id : DDTRACE_G(distributed_trace_id);

if (DDTRACE_G(dd_origin)) {
zval value;
Expand Down
5 changes: 5 additions & 0 deletions tests/Common/TracerTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ private function parseRawDumpedTraces($rawTraces)
return;
}

if ($rawSpan['trace_id'] == "0") {
TestCase::fail(sprintf("Span '%s' has zero trace_id", $rawSpan['name']));
return;
}

$rawSpan["duration"] = (int)($rawSpan["duration"] / 1000);
$rawSpan["start"] = (int)($rawSpan["start"] / 1000);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,40 @@ DD_TRACE_GENERATE_ROOT_SPAN=0
--FILE--
<?php

function dump_spans() {
foreach (dd_trace_serialize_closed_spans() as $span) {
unset($span["meta"]["process_id"], $span["meta"]["_dd.p.dm"]);
echo "parent: ", $span["parent_id"] ?? 0, ", trace: {$span["trace_id"]}, meta: " . json_encode($span["meta"] ?? []) . "\n";
}
return $span;
}

DDTrace\start_span();
DDTrace\start_span();

var_dump(DDTrace\set_distributed_tracing_context("123", "321", "foo", ["a" => "b"]));
var_dump(DDtrace\current_context());
var_dump(DDtrace\current_context()["span_id"] != "123" && DDtrace\current_context()["span_id"] != "321");
var_dump(DDTrace\current_context());
var_dump(DDTrace\current_context()["span_id"] != "123" && DDtrace\current_context()["span_id"] != "321");

DDTrace\close_span();
DDTrace\close_span();

foreach (dd_trace_serialize_closed_spans() as $span) {
unset($span["meta"]["process_id"], $span["meta"]["_dd.p.dm"]);
echo "parent: {$span["parent_id"]}, trace: {$span["trace_id"]}, meta: " . json_encode($span["meta"]) . "\n";
}
dump_spans();

DDTrace\start_span();
DDTrace\start_span();

var_dump(DDTrace\set_distributed_tracing_context("0", "0", "", []));
var_dump(DDTrace\current_context());
$trace_id = DDTrace\trace_id();
var_dump($trace_id != 123);
var_dump($trace_id == DDTrace\root_span()->id);

DDTrace\close_span();
DDTrace\close_span();

$span = dump_spans();
echo "all spans trace_id updated: "; var_dump($span["trace_id"] == $trace_id);

?>
--EXPECTF--
Expand All @@ -47,3 +67,22 @@ array(7) {
bool(true)
parent: 321, trace: 123, meta: {"_dd.origin":"foo","a":"b"}
parent: %d, trace: 123, meta: {"_dd.origin":"foo"}
bool(true)
array(5) {
["trace_id"]=>
string(%d) "%d"
["span_id"]=>
string(%d) "%d"
["version"]=>
NULL
["env"]=>
NULL
["distributed_tracing_propagated_tags"]=>
array(0) {
}
}
bool(true)
bool(true)
parent: 0, trace: %d, meta: []
parent: %d, trace: %d, meta: []
all spans trace_id updated: bool(true)

0 comments on commit 73e32f0

Please sign in to comment.