Connecting PHP Logs and Traces
Rapport de recherche Datadog : Bilan sur l'adoption de l'informatique sans serveur Rapport : Bilan sur l'adoption de l'informatique sans serveur

Connecting PHP Logs and Traces

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Automatic Trace ID injection

Given the many different ways to implement logging in PHP, with some completely circumventing PHP’s built-in error-logging API, the Datadog PHP tracing library cannot reliably inject trace and span IDs into logs automatically. See the section below to learn how to connect your PHP Logs and traces manually.

Manual Trace ID injection

To connect your logs and traces together, your logs must contain the dd.trace_id and dd.span_id attributes that respectively contain your trace ID and your span ID.

If you are not using a Datadog Log Integration to parse your logs, custom log parsing rules need to ensure that dd.trace_id and dd.span_id are being parsed as strings and remapped thanks to the Trace Remapper. More information can be found in the Why can’t I see my correlated logs in the Trace ID panel? FAQ.

For instance, you would append those two attributes to your logs with:

  <?php
  $span = \DDTrace\GlobalTracer::get()->getActiveSpan();
  $append = sprintf(
      ' [dd.trace_id=%d dd.span_id=%d]',
      $span->getTraceId(),
      $span->getSpanId()
  );
  my_error_logger('Error message.' . $append);
?>

If the logger implements the monolog/monolog library, use Logger::pushProcessor() to automatically append the identifiers to all log messages:

<?php
  $logger->pushProcessor(function ($record) {
      $span = \DDTrace\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;
  });
?>

Further Reading