dd-trace-php icon indicating copy to clipboard operation
dd-trace-php copied to clipboard

TraceId not being injected in the logs

Open sdepablos opened this issue 6 years ago • 4 comments

As per explained in Connect Logs and Traces we've added a a Monolog processor to our logs so they receive the trace and span id, but so far we've had no luck.

What we've found is that while the trace id is generated correctly at any point of our code - we just did a echo GlobalTracer::get()->getActiveSpan()->getTraceId(); at it worked - once we arrive to the Monolog processor the GlobalTracer::get()->getActiveSpan(); returns null for the same request.

We're using agent v6.15.1 wit the PHP agent 0.35.0, in a Symfony 3.4 project, with the following config:

monolog:
  handlers:
    datadog:
      type: stream
      path: "%kernel.logs_dir%/datadog-%kernel.environment%.log"
      level: info
      formatter: monolog.formatter.json
services:
  datadog_processor:
    class: App\Core\Infrastructure\Monolog\DatadogProcessor
    tags:
      - { name: monolog.processor, handler: datadog }
namespace App\Core\Infrastructure\Monolog;

use DDTrace\GlobalTracer;

class DatadogProcessor
{
    public function __invoke(array $record)
    {
        $span = GlobalTracer::get()->getActiveSpan();
        if (null === $span) {
            return $record;
        }
        $record['message'] .= sprintf(
            ' [dd.trace_id=%d dd.span_id=%d]',
            $span->getTraceId(),
            $span->getSpanId()
        );

        return $record;
    }
}

sdepablos avatar Dec 11 '19 17:12 sdepablos

Hi @sdepablos I need to ask a few questions to be able to help:

  1. What is your env (SAPI)? web request or symfony command?
  2. How do you have access to GlobalTracer? I.e. do you have a dependency in composer for datadog/dd-trace or you rely on the classes made available by our extension?
  3. If you are declaring a dependency in composer, is the version declared the same as the version of the installed extension?

If the answer to 1) is cli and the answer to 2) is 'composer' then you need to run the command as DD_TRACE_CLI_ENABLED=true.

labbati avatar Dec 12 '19 10:12 labbati

Hi @labbati, sorry for the late reply but I didn't see your message until today. This is happening with

  1. Web request
  2. Using the classes made available by the extension
  3. Not applicable

sdepablos avatar Jan 08 '20 17:01 sdepablos

Hi @labbati, attached a screenshot on how the trace_id and span_id are arriving to Datadog. Is it correct they arrive inside the string of the route? Shouldn't they be some kind of attributes of the log? Selection_002

sdepablos avatar Feb 08 '20 10:02 sdepablos

Hi @sdepablos, finding time to look into this issue is taking more than it should have. My apologies. Just dropping a message here to let you know that this is still in our radar and we will look into this asap.

labbati avatar Feb 13 '20 13:02 labbati

Hello, FYI, we plan to work on logs-tracing correlation this quarter.

pierotibou avatar Mar 28 '23 19:03 pierotibou

Hi @sdepablos,

At long last, Logs Correlation is now available with the tracer starting version 0.89.0. This documentation may also help you get started with it :smiley:

I'll be closing this issue for now, but feel free to open an other one in case you encounter an issue while connecting your logs and traces or if you have any feedback!

PROFeNoM avatar Oct 20 '23 14:10 PROFeNoM