DataDog / dd-trace-php

Datadog PHP Clients
https://docs.datadoghq.com/tracing/setup/php
Other
487 stars 152 forks source link

[Bug]: \DDTrace\current_context trace_id does not match \DDTrace\logs_correlation_trace_id() #2734

Open Justinas-Jurciukonis opened 3 months ago

Justinas-Jurciukonis commented 3 months ago

Bug report

As stated inside Manual Injection documentation \DDTrace\current_context() should return trace identifiers and we always have used following code:

$context = \DDTrace\current_context();

$record->with(extra: [
    'dd' => [
        'span_id' => $context['span_id'],
        'trace_id' => $context['trace_id'],
    ],
])

and it worked fine for some time. But now (as of 0.98.1 and 1.1.0) adding this trace_id loses Traces information and even sometimes matches accross multiple requests.

I have logged following code and see that \DDTrace\current_context() and \DDTrace\logs_correlation_trace_id() returns different results:

var_dump(\DDTrace\current_context() + ['span_id2' => \dd_trace_peek_span_id(), 'trace_id2' => \DDTrace\logs_correlation_trace_id()]);

// Outputs
{
    "trace_id": "136226447259282480168556905957809988804", // <---
    "span_id": "6603237610001450838",
    "version": "1516-test",
    "env": "infra_test",
    "distributed_tracing_propagated_tags": [],
    "trace_id2": "12691714017235838148", // <---
    "span_id2": "6603237610001450838"
}

If using \DDTrace\logs_correlation_trace_id(), than Traces are matched against log records

PHP version

8.3

Tracer or profiler version

0.98.1; 1.1.0

Installed extensions

No response

Output of phpinfo()

No response

Upgrading from

No response

PROFeNoM commented 3 months ago

Hi @Justinas-Jurciukonis :wave:

Currently, many external tracing systems (e.g., OpenTelemetry) utilize 128-bit Trace IDs, so when Datadog interoperates with them, we can end up with broken traces and logs correlation. This is why, alongside the OTel API support, 128-bit trace ID generation has been enabled by default (#2358) since 0.94.0 to match these open standards.

The behavior you are experiencing is expected. The results of these two functions are the same when DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED is disabled. \DDTrace\logs_correlation_trace_id should be used to ensure the right trace identifier format is used.

Justinas-Jurciukonis commented 3 months ago

@PROFeNoM Maybe you can add this as warning into documentation page?

bwoebi commented 3 months ago

@Justinas-Jurciukonis We're planning to change the default setting in the next few months, given that our backend now also supports 128 bit trace ids :-)