Install the Bundle
composer require amashukov/tracing-bundle
Add to config/bundles.php:
return [
// ...
AndreyMashukov\TracingBundle\TracingBundle::class => ['all' => true],
];
Verify Request ID Generation
Trigger an HTTP request (e.g., via browser or curl):
curl -v http://your-app.test
Check the X-Request-Id header in the response and Monolog logs (var/log/dev.log).
First Use Case: Debugging a Request Flow
request_id in logs (e.g., extra.request_id).HTTP Layer
X-Request-Id for incoming requests.RequestId service into controllers/services:
use AndreyMashukov\TracingBundle\Service\RequestId;
public function __construct(private RequestId $requestId) {}
public function index(): Response
{
$requestId = $this->requestId->get(); // e.g., "018a0b..."
return new Response("Request ID: {$requestId}");
}
Messenger Bridge
request_id from the HTTP context.RequestIdMiddleware to propagate the ID to queue workers:
use AndreyMashukov\TracingBundle\Middleware\RequestIdMiddleware;
$bus->addMiddleware(
new RequestIdMiddleware($requestIdService)
);
$this->bus->dispatch(new YourMessage());
Monolog Integration
extra.request_id automatically. Customize via monolog.yaml:
handlers:
main:
processor: AndreyMashukov\TracingBundle\Processor\RequestIdProcessor
Custom ID Generation
Override the default UUIDv7 generator by binding your own service to AndreyMashukov\TracingBundle\Generator\RequestIdGeneratorInterface.
Contextual Logging
Attach additional context to logs (e.g., user ID) while preserving the request_id:
$this->logger->info('Event triggered', [
'request_id' => $this->requestId->get(),
'user_id' => $user->id,
]);
Testing
Mock the RequestId service in tests:
$this->requestId->shouldReceive('get')->andReturn('test-123');
Missing Headers in Async Workers
request_id if RequestIdMiddleware isn’t configured.$bus->addMiddleware(new RequestIdMiddleware($requestIdService), 'before' => 'your_handler');
UUIDv7 Collisions
class CustomRequestIdGenerator implements RequestIdGeneratorInterface
{
public function generate(): string
{
return sprintf(
'%s-%d',
(new \Ramsey\Uuid\Uuid())->uuid7()->toString(),
getmypid()
);
}
}
Log Processor Conflicts
extra, it may overwrite request_id.RequestIdProcessor runs last in the processor chain.Symfony Messenger Version Mismatch
MessageBusInterface. Older versions may need adapter shims.composer.json or wrap the middleware in a version check.Validate Request ID Propagation Add a debug endpoint to verify the ID flows through all layers:
public function debug(RequestId $requestId): Response
{
return $this->json([
'request_id' => $requestId->get(),
'monolog_extra' => $this->logger->getHandlers()[0]->getProcessor()?->__invoke([])['extra'] ?? [],
]);
}
Log Correlation
Use a log viewer (e.g., ELK, Papertrail) with a query for extra.request_id to trace a request’s lifecycle.
Custom Headers
Extend the RequestId service to support additional headers (e.g., X-Correlation-Id):
$this->requestId->setHeaderName('X-Correlation-Id');
Database Tracing
Bind the request_id to database queries via Doctrine listeners:
$conn->getConfiguration()->addListener(new RequestIdListener($requestIdService));
OpenTelemetry Integration
Export the request_id to OpenTelemetry traces for distributed tracing:
$traceContext = new TraceContext([
'request_id' => $this->requestId->get(),
]);
How can I help you explore Laravel packages today?