digitalrevolution/symfony-trace-bundle
Installation:
composer require digitalrevolution/symfony-trace-bundle
Add to config/bundles.php:
DR\SymfonyTraceBundle\SymfonyTraceBundle::class => ['all' => true],
First Use Case:
Inject the TraceContext service into a controller or service:
use DR\SymfonyTraceBundle\Trace\TraceContext;
public function __construct(private TraceContext $traceContext) {}
public function someAction(): Response
{
$traceId = $this->traceContext->getTraceId();
// Log or use the traceId (e.g., in Monolog)
return new Response("Trace ID: {$traceId}");
}
Verify in Logs:
Ensure Monolog logs now include the trace_id metadata. Check your log handler configuration (e.g., monolog.yaml):
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
channels: ["!event"]
process_psr_3_messages: true # Required for trace_id inclusion
Request-Response Propagation:
trace_id into HTTP headers (traceparent for W3C compliance).RequestStack or TraceContext:
$request = $this->requestStack->getCurrentRequest();
$traceId = $request->headers->get('X-Trace-Id');
Console Commands:
use DR\SymfonyTraceBundle\Trace\TraceContext;
public function __construct(private TraceContext $traceContext) {}
public function execute(Command $command): int
{
$traceId = $this->traceContext->getTraceId();
$this->logger->info("Command executed with trace_id: {$traceId}");
return Command::SUCCESS;
}
Twig Integration:
{{ app.trace_id }}
services.yaml:
DR\SymfonyTraceBundle\Twig\TraceExtension:
tags: ['twig.extension']
Messenger Integration:
# config/packages/messenger.yaml
framework:
messenger:
transports:
async: '%env(MESSENGER_TRANSPORT_DSN)%'
routing:
'App\Message\YourMessage': async
defaults:
trace_id: true # Auto-propagate trace_id
HttpClient:
$client = \Symfony\Contracts\HttpClient\HttpClient::create([
'trace_id' => true, // Auto-inject trace_id
]);
Sentry handler:
# config/packages/monolog.yaml
handlers:
sentry:
type: sentry
level: error
trace_id: true # Include trace_id in reports
trace_id in query logging (e.g., Doctrine):
$this->logger->info("Query executed", ['trace_id' => $traceId, 'query' => $sql]);
Header Conflicts:
traceparent headers if using W3C compliance, as the bundle auto-generates them.Request::headers for duplicates.Monolog Configuration:
process_psr_3_messages: Logs won’t include trace_id unless enabled in handlers.stream with process_psr_3_messages: true).Console Trace IDs:
TraceContext::generateTraceId() for isolated CLI workflows:
$traceId = $this->traceContext->generateTraceId();
HttpClient Tagging:
$client = \Symfony\Contracts\HttpClient\HttpClient::create([
'trace_id' => true,
]);
Messenger Transport:
trace_id is enabled in messenger.yaml for async buses:
defaults:
trace_id: true
TraceContext is injected correctly (use dump($traceContext->getTraceId())).RequestIdBundle).# config/packages/dev/monolog.yaml
handlers:
main:
level: debug
Custom Trace ID Generation:
// config/services.yaml
DR\SymfonyTraceBundle\Trace\TraceContext:
arguments:
$traceIdGenerator: '@app.custom_trace_generator'
DR\SymfonyTraceBundle\Trace\TraceIdGeneratorInterface.Header Customization:
kernel.request):
public function onKernelRequest(RequestEvent $event): void
{
$event->getRequest()->headers->set('X-Custom-Trace', $this->traceContext->getTraceId());
}
Log Formatter:
Processor to include trace_id in custom formats:
use Monolog\Processor\ProcessorInterface;
class TraceIdProcessor implements ProcessorInterface
{
public function __invoke(array $record): array
{
$record['extra']['trace_id'] = $this->traceContext->getTraceId();
return $record;
}
}
Register in monolog.yaml:
processors:
trace_id: ['@app.trace_id_processor']
How can I help you explore Laravel packages today?