Instrument a PHP application
Grafana Cloud

Instrument a PHP application

For most use cases Grafana Labs recommends Beyla, eBPF network-level auto-instrumentation, which is easy to set up and supports all languages and frameworks.

If you need process-level telemetry for PHP, follow this documentation to set up the upstream OpenTelemetry SDK for PHP for Application Observability.

Before you begin

The is the first step to get telemetry data into Application Observability, you need:

  1. A PHP development environment 8.0+.
  2. PECL.
  3. Composer.

Install the SDK

Before you begin ensure you have a PHP 8.0+ development environment, PECL or Composer, and a PHP application to instrument.

Use either the PECL or Composer to install the package.

PECL:

Install the opentelemetry and protobuf extensions with the following commands:

sh
pecl install opentelemetry
pecl install protobuf

Add the extensions to your php.ini file:

ini
extension=protobuf.so

[opentelemetry]
extension=opentelemetry.so

Composer:

Run the following command in the project folder:

sh
composer require \
  open-telemetry/sdk \
  open-telemetry/exporter-otlp \
  php-http/guzzle7-adapter

The php-http/guzzle7-adapter packages fulfills the psr/http-client-implementation and psr/http-factory-implementation OpenTelemetry requirements.

If you don’t have Composer autoloading enabled, modify the application entry point to include:

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

Auto-instrumentation

Many open source PHP projects have robust auto-instrumentation support so that you don’t have to manually instrument your application.

For a list of PHP frameworks and applications with auto-instrumentation packages, consult the OpenTelemetry Registry and filter for PHP instrumentation.

Collect logs

To collect logs for your PHP application:

  1. Use a PSR-3 compatible logger such as monolog.
  2. Add auto-instrumentation for PSR-3 Loggers to automatically collect logs.

Install the the package from the command line:

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

Now OpenTelemetry automatically instrument logs, for example:

php
/** @var \Psr\Log\LoggerInterface $logger */
$logger->info('This message will be sent to Grafana Cloud via OTLP');

Instrument your application

Export the following environment variables before starting your application to enable auto-instrumentation:

sh
export OTEL_PHP_AUTOLOAD_ENABLED=true
export OTEL_TRACES_EXPORTER=otlp
export OTEL_METRICS_EXPORTER=otlp
export OTEL_LOGS_EXPORTER=otlp
export OTEL_PROPAGATORS=baggage,tracecontext

Test your instrumentation

To test if you’ve successfully instrumented your application, run your application, generate some traffic, and you should see metrics and logs outputted to the console.

sh
php -S localhost:8080

Example application

See the Rolldice service for a complete example setup.

Next steps

  1. Create a free Grafana Cloud account.
  2. For a local development and testing, send data to the Grafana Cloud OTLP endpoint.
  3. For production, set up an OpenTelemetry Collector.
  4. Observe your services in Application Observability.

Resources

  1. PHP OpenTelemetry documentation
  2. opentelemetry package on PECL
  3. opentelemetry-php-instrumentation on GitHub