silpo-tech/trace-bundle

Trace bundle for Symfony

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Type:symfony-bundle

v1.0.0 2025-09-25 10:32 UTC

This package is auto-updated.

Last update: 2025-09-25 11:03:11 UTC


README

CI codecov License: MIT

A Symfony bundle for distributed tracing and request correlation across microservices.

Features

  • Request ID Generation: Automatically generates unique trace IDs for incoming requests
  • Request ID Propagation: Forwards trace IDs to outgoing HTTP requests via Guzzle middleware
  • Logging Integration: Adds trace IDs to Monolog log entries
  • Console Support: Generates trace IDs for console commands
  • Sentry Integration: Enriches Sentry transactions with trace information
  • Message Bus Support: Correlates messages with trace IDs (requires silpo-tech/message-bus-bundle)

Installation

composer require silpo-tech/trace-bundle

Configuration

Add the bundle to your config/bundles.php:

return [
    // ...
    TraceBundle\TraceBundle::class => ['all' => true],
];

Bundle Configuration

Create config/packages/trace.yaml:

trace:
    id_header_name: 'X-Request-Id'        # Header name for trace ID (default)
    id_log_extra_name: 'requestId'        # Log field name for trace ID (default)
    autoconfigure_handlers: true          # Auto-configure Guzzle handlers (default)

Usage

HTTP Requests

The bundle automatically:

  1. Extracts trace ID from incoming request headers (X-Request-Id by default)
  2. Generates a new UUID if no trace ID is present
  3. Adds the trace ID to response headers
  4. Propagates trace ID to outgoing HTTP requests via Guzzle middleware

Logging

Trace IDs are automatically added to all log entries:

$logger->info('Processing user request'); 
// Log will include: {"message": "Processing user request", "extra": {"requestId": "uuid-here"}}

Console Commands

For console commands, a trace ID is automatically generated and available throughout the command execution.

Manual Access

You can access the current trace ID programmatically:

use TraceBundle\Storage\TraceIdStorageInterface;

class YourService
{
    public function __construct(
        private TraceIdStorageInterface $traceIdStorage
    ) {}
    
    public function someMethod(): void
    {
        $traceId = $this->traceIdStorage->get();
        // Use trace ID as needed
    }
}

Integration

Sentry

When sentry/sentry is installed, the bundle automatically enriches Sentry transactions with trace information.

Message Bus

When silpo-tech/message-bus-bundle is installed, trace IDs are automatically propagated through message bus operations.

Requirements

  • PHP ≥8.3
  • Symfony ≥6.4|^7.0

Dependencies

Required

  • symfony/http-kernel
  • symfony/dependency-injection
  • symfony/config
  • ramsey/uuid
  • monolog/monolog
  • guzzlehttp/guzzle
  • symfony/yaml

Optional

  • sentry/sentry - For Sentry integration
  • silpo-tech/message-bus-bundle - For message bus correlation

Testing

composer install
vendor/bin/phpunit

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

This is a proprietary package. Please contact the maintainers for contribution guidelines.