OpenTelemetry Slim monitoring

Vladimir Mihailenco
October 30, 2024
2 min read

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, process, and export telemetry data from applications and infrastructure. It combines metrics, logs, and distributed traces into a unified toolkit that helps developers understand how their systems are performing.

OpenTelemetry supports multiple programming languages and platforms, making it suitable for a wide range of applications and environments.

OpenTelemetry enables developers to instrument their code and collect telemetry data, which can then be exported to various OpenTelemetry backends or observability platforms for analysis and visualization. Deploying an OpenTelemetry Collector in your infrastructure provides enhanced telemetry data processing capabilities, including filtering, enrichment, and batching before data reaches your backend.

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:
    shell Linux (apt)
    sudo apt-get install gcc make autoconf php-dev
    
    shell macOS (homebrew)
    brew install gcc make autoconf
    
  2. Build/install the extension. With your environment set up you can install the extension:
    shell pecl
    pecl install opentelemetry
    
    shell picle
    php pickle.phar install opentelemetry
    
    shell php-extension-installer (docker)
    install-php-extensions opentelemetry
    
  3. Add the extension to your php.ini file (run php --ini to find out file location):
    ini
    [opentelemetry]
    extension=opentelemetry.so
    
  4. Verify that the extension is installed and enabled:
    shell
    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:

sh
composer require open-telemetry/opentelemetry-auto-slim

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

sh
composer require uptrace/uptrace

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

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

// Configure OpenTelemetry.
$uptrace = Uptrace\Distro::builder()
    // copy your project DSN here or use UPTRACE_DSN env var
    ->setDsn('<FIXME>')
    ->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:

php
$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.

For full-featured PHP frameworks with auto-instrumentation, explore Laravel or Symfony.