Monitor Symfony with OpenTelemetry Instrumentation

OpenTelemetry Symfony integration allows you to collect telemetry data such as traces, metrics, and logs from your Symfony applications, providing insights into their performance and behavior.

What is OpenTelemetry?

OpenTelemetry is an open source observability framework designed to facilitate the generation, collection and export of telemetry data, including traces, metrics and logs.

OpenTelemetry provides a standardized way to collect and export telemetry data from applications and infrastructure, offering insights into their behavior and performance.

Auto-instrumentation

OpenTelemetry PHP auto-instrumentation is a powerful feature that allows you to collect telemetry data from your PHP applications with minimal manual configuration.

It automatically adds instrumentation to your application, capturing telemetry data without requiring you to modify code manually.

To use auto-instrumentation, you need to install OpenTelemetry extension.

  1. Setup development environment. Installing from source requires proper development environment and some dependencies:

    sudo apt-get install gcc make autoconf php-dev
    
    brew install gcc make autoconf
    
  2. Build/install the extension. With your environment set up you can install the extension:

    pecl install opentelemetry
    
    php pickle.phar install opentelemetry
    
    install-php-extensions opentelemetry
    
  3. Add the extension to your php.ini file (run php --ini to find out file location):

    [opentelemetry]
    extension=opentelemetry.so
    
  4. Verify that the extension is installed and enabled:

    php -m | grep opentelemetry
    

OpenTelemetry Symfony

Symfony auto-instrumentation significantly reduces the manual work required to implement OpenTelemetry in a Symfony application, providing immediate visibility into application performance and behavior.

Install the OpenTelemetry Symfony bundle:

composer require open-telemetry/opentelemetry-auto-symfony

Add the bundle to your config/bundles.php:

return [
    // ...
    OpenTelemetry\Contrib\Symfony\OtelSdkBundle\OtelSdkBundle::class => ['all' => true],
];

Create config/packages/otel.yaml:

otel_sdk:
  resource:
    attributes:
      service.name: 'your-service-name'
  traces:
    sampler:
      type: 'always_on'
  exporters:
    otlp:
      dsn: 'https://otlp.uptrace.dev:4318'
      protocol: 'http/protobuf'

Manual instrumentation

For custom instrumentation, you can inject the tracer into your services:

use OpenTelemetry\API\Trace\TracerInterface;

class YourService
{
    public function __construct(private TracerInterface $tracer) {}

    public function someMethod()
    {
        $span = $this->tracer->spanBuilder('custom-operation')->startSpan();
        try {
            // Your code here
        } finally {
            $span->end();
        }
    }
}

Conclusion

By integrating OpenTelemetry with Symfony, you can build more robust, observable, and performant PHP applications.

Last Updated: