Monitor Slim with OpenTelemetry Instrumentation

OpenTelemetry Slim is an instrumentation library designed to automatically add OpenTelemetry traces to your PHP applications built on the Slim framework.

It allows you to identify performance bottlenecks and optimize your application.

What is OpenTelemetry?

OpenTelemetry is an open source observability framework that provides a standardized way to collect and export telemetry data from applications and infrastructure. It offers tools for distributed tracing, metrics collection, and logging, enabling developers and operators to gain insights into the behavior and performance of complex, cloud-native software systems.

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 Slim

The package automatically registers instrumentation hooks via Composer. This means that key application functions, such as App::handle() and routing middleware, will automatically create spans for tracing without requiring additional code changes.

To install the package:

composer require open-telemetry/opentelemetry-auto-slim

To export data to Uptrace, you need to install uptrace-php which comes with OpenTelemetry SDK:

composer require uptrace/uptrace

In the src/public/index.php, add the following code right after the auto-loader registration:

require __DIR__.'/../vendor/autoload.php';

// Configure OpenTelemetry.
$uptrace = Uptrace\Distro::builder()
    // copy your project DSN here or use UPTRACE_DSN env var
    ->setDsn('https://<token>@api.uptrace.dev?grpc=4317')
    ->setServiceName('myservice')
    ->setServiceVersion('1.0.0')
    ->buildAndRegisterGlobal();

Manual instrumentation

While auto-instrumentation provides a lot out of the box, you can still add custom instrumentation where needed:

$tracer = \OpenTelemetry\API\GlobalTracerProvider::get()->getTracer('my-tracer');
$span = $tracer->spanBuilder('my-custom-operation')->startSpan();
// Your code here
$span->end();

Conclusion

By integrating OpenTelemetry with Slim, you enable robust observability, making it easier to maintain, debug, and optimize your PHP applications.

Last Updated: