Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Open Telemetry Bundle Laravel Package

danilovl/open-telemetry-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require danilovl/open-telemetry-bundle
    

    Ensure OTEL_PHP_AUTOLOAD_ENABLED=false in your .env.

  2. Configure Environment Variables:

    OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
    OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
    OTEL_SERVICE_NAME=my-app
    
  3. Basic Configuration: Create config/packages/open_telemetry.yaml:

    danilovl_open_telemetry:
        service:
            name: 'my-app'
        instrumentation:
            http_server:
                enabled: true
                tracing:
                    enabled: true
    
  4. First Use Case: Trigger an HTTP request. The bundle automatically instruments the request with OpenTelemetry tracing.


Where to Look First

  • Configuration: Start with config/packages/open_telemetry.yaml to enable/disable instrumentations.
  • Documentation: Review the README for full configuration examples.
  • Logs: Check Symfony logs for initialization errors (e.g., missing dependencies or misconfigured exporters).

Implementation Patterns

Usage Patterns

  1. Selective Instrumentation: Enable only the instrumentations you need (e.g., http_server, doctrine, messenger) to avoid overhead:

    danilovl_open_telemetry:
        instrumentation:
            http_server:
                enabled: true
            doctrine:
                enabled: true
            redis:
                enabled: false  # Disable if unused
    
  2. Custom Metrics/Traces: Implement interfaces to override default behavior:

    // src/Tracing/CustomDoctrineMetrics.php
    namespace App\Tracing;
    
    use Danilovl\OpenTelemetryBundle\Instrumentation\Doctrine\DoctrineMetricsInterface;
    
    class CustomDoctrineMetrics implements DoctrineMetricsInterface
    {
        public function recordQueryExecution(string $query, float $duration): void
        {
            // Custom logic
        }
    }
    

    Register the service in services.yaml:

    services:
        App\Tracing\CustomDoctrineMetrics:
            tags: ['danilovl.open_telemetry.metrics.doctrine']
    
  3. Traceable Attributes: Annotate controllers/commands/methods with #[Traceable] for automatic span creation:

    use Danilovl\OpenTelemetryBundle\Attribute\Traceable;
    
    #[Traceable]
    public function processOrder(Order $order): void
    {
        // Automatically wrapped in a span
    }
    
  4. Middleware/Decorators: Leverage built-in decorators for HTTP clients, caches, and Redis:

    # Automatically decorates http_client.client services
    danilovl_open_telemetry:
        instrumentation:
            http_client:
                enabled: true
    

Workflows

  1. Debugging Traces: Use the TracingSpanService to manually create spans:

    use Danilovl\OpenTelemetryBundle\Service\TracingSpanService;
    
    public function __construct(private TracingSpanService $tracingSpanService) {}
    
    public function someMethod()
    {
        $span = $this->tracingSpanService->startSpan('custom-span');
        try {
            // Business logic
        } finally {
            $span->end();
        }
    }
    
  2. Metrics Collection: Inject metrics interfaces into services:

    use Danilovl\OpenTelemetryBundle\Instrumentation\HttpServer\HttpServerMetricsInterface;
    
    public function __construct(private HttpServerMetricsInterface $metrics) {}
    
    public function onKernelRequest(GetResponseEvent $event): void
    {
        $this->metrics->recordRequestStart();
    }
    
  3. Custom Exporters/Processors: Tag services to override default SDK behavior:

    services:
        App\Tracing\CustomSpanProcessor:
            tags:
                - { name: 'otel.span_processor', priority: 1000 }
    

Integration Tips

  1. Symfony Messenger: Enable long_running_command_enabled for messenger:consume:

    danilovl_open_telemetry:
        instrumentation:
            messenger:
                enabled: true
                long_running_command_enabled: true
    
  2. Doctrine: Use default_trace_ignore_enabled to skip vendor queries:

    danilovl_open_telemetry:
        instrumentation:
            doctrine:
                enabled: true
                default_trace_ignore_enabled: true
    
  3. HTTP Client: Decorate specific clients by tagging them:

    services:
        http_client.my_client:
            decorates: http_client
            arguments:
                $decorated: '@http_client.my_client.inner'
            tags: ['http_client.client']
    
  4. Async Operations: Use #[Traceable] on async methods or enable async instrumentation:

    danilovl_open_telemetry:
        instrumentation:
            async:
                enabled: true
    

Gotchas and Tips

Pitfalls

  1. PHP Extension Conflict: Error: OTEL_PHP_AUTOLOAD_ENABLED must be false; otherwise, the bundle and extension will conflict. Fix: Set OTEL_PHP_AUTOLOAD_ENABLED=false in .env.

  2. Missing Dependencies: Error: Class 'OpenTelemetry\API\Trace\TracerProviderInterface' not found. Fix: Ensure open-telemetry/api and open-telemetry/sdk are installed.

  3. Duplicate Implementations: Error: LogicException: Multiple implementations found for interface X. Declare an alias explicitly. Fix: Configure the alias in services.yaml:

    services:
        _defaults:
            bind:
                Danilovl\OpenTelemetryBundle\Instrumentation\Doctrine\DoctrineMetricsInterface: '@App\Tracing\CustomDoctrineMetrics'
    
  4. Late Initialization: Issue: Traces/metrics appear only after the first request. Fix: The SDK initializes lazily. For immediate startup, trigger a dummy request or use OpenTelemetryInitializer::initializeSdk() manually.

  5. Context Propagation: Issue: Spans not linked across services (e.g., HTTP → Messenger). Fix: Ensure OTEL_TRACES_SAMPLER is configured (e.g., always_on for testing):

    OTEL_TRACES_SAMPLER=always_on
    

Debugging

  1. Check Initialization: Enable debug mode and inspect logs for:

    [OpenTelemetry] Initializing SDK with resource attributes: {...}
    
  2. Validate Configuration: Use Symfony’s config validator:

    php bin/console debug:config danilovl_open_telemetry
    
  3. Test Locally: Run a local OTel Collector with Docker:

    docker run -p 4318:4318 otel/opentelemetry-collector
    

    Then configure:

    OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
    
  4. Span Visualization: Use Jaeger or Kibana to verify traces:

    docker run -p 16686:16686 jaegertracing/all-in-one:latest
    

    Configure exporter:

    OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
    OTEL_EXPORTER_OTLP_PROTOCOL=grpc
    

Tips

  1. Performance Tuning: Disable unused instrumentations (e.g., twig, events) to reduce overhead.

  2. Custom Attributes: Extend spans with context via providers:

    use Danilovl\OpenTelemetryBundle\Provider\SpanAttributeProviderInterface;
    
    class UserIdSpanAttributeProvider implements SpanAttributeProviderInterface
    {
        public function getAttributes(): array
        {
            return ['user_id' => $this->userIdService->getId()];
        }
    }
    

    Register as a service tagged danilovl.open_telemetry.span_attribute_provider.

  3. Sampling: Configure sampling in .env:

    OTEL_TRACES_SAMPLER=parentbased_always_on
    OTEL_TRACES_SAMPLER_ARG=1.0  # Sample 100% of traces
    
  4. Environment-Specific Configs: Use Symfony’s environment-aware configs:

    # config/packages/dev/open_telemetry.yaml
    danilovl_open_telemetry:
        service:
            environment: 'dev'
        instrumentation:
            http_server:
                tracing:
                    enabled: true
    
  5. Testing: Mock the `

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope