Skip to content

OpenTelemetry guide

OpenTelemetry for PHP and Laravel

PHP auto-instrumentation uses the opentelemetry extension to hook into Laravel, PDO, HTTP clients and queues, configured entirely with environment variables.

4 min read11 sectionsUpdated

On this page

Key points

  • Install the opentelemetry PHP extension (PECL) first.
  • Add the SDK, the OTLP exporter and the Laravel auto-instrumentation with Composer.
  • Enable with OTEL_PHP_AUTOLOAD_ENABLED=true and the usual OTEL_ variables.
  • PHP-FPM: set variables in the pool config, not only in the shell.

Install the extension and packages

bash
pecl install opentelemetry
echo "extension=opentelemetry.so" | sudo tee /etc/php/8.3/mods-available/opentelemetry.ini
sudo phpenmod opentelemetry

composer require open-telemetry/sdk open-telemetry/exporter-otlp \
  open-telemetry/opentelemetry-auto-laravel open-telemetry/opentelemetry-auto-pdo \
  php-http/guzzle7-adapter

The Guzzle adapter gives the exporter an HTTP client; any PSR-18 client works. Adjust the PHP version in the path to match yours.

Configure

bash
OTEL_PHP_AUTOLOAD_ENABLED=true
OTEL_SERVICE_NAME=shop-laravel
OTEL_RESOURCE_ATTRIBUTES=service.version=4.0.1,deployment.environment.name=production
OTEL_TRACES_EXPORTER=otlp
OTEL_METRICS_EXPORTER=none
OTEL_LOGS_EXPORTER=none
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_PROPAGATORS=tracecontext,baggage

PHP-FPM

PHP-FPM workers do not inherit your shell environment by default. Set the variables in the pool configuration:

www.confplaintext
; /etc/php/8.3/fpm/pool.d/www.conf
env[OTEL_PHP_AUTOLOAD_ENABLED] = true
env[OTEL_SERVICE_NAME] = shop-laravel
env[OTEL_TRACES_EXPORTER] = otlp
env[OTEL_EXPORTER_OTLP_PROTOCOL] = http/protobuf
env[OTEL_EXPORTER_OTLP_ENDPOINT] = http://localhost:4318

What you get

A server span per request named by route, spans for Eloquent and PDO queries, outgoing HTTP calls through Laravel's client or Guzzle (with the matching auto packages), and queue jobs. Because each PHP request is short-lived, export must be fast: send straight to VitalRix, or keep a Collector close to the app (on the same host or node).

Send it to VitalRix

Shortcut. The VitalRix package does all of the above for you: composer require vitalrix/vitalrix, then \Vitalrix\Vitalrix::init() early in the request: traces, metrics and logs. It reads VITALRIX_URL, VITALRIX_API_KEY, OTEL_SERVICE_NAME and DEPLOYMENT_ENVIRONMENT.

Directly. VitalRix accepts OTLP over HTTP with protobuf or JSON, which is what the PHP exporter sends. Instead of a Collector at localhost:4318, point the exporter at VitalRix with the per-signal endpoints and your API key. Create the key in VitalRix under Settings, API keys, with trace, metric and log ingestion.

bash
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://api.vitalrix.com/api/v1/traces/otlp/v1/traces
export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://api.vitalrix.com/api/v1/metrics/otlp/v1/metrics
export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=https://api.vitalrix.com/api/v1/logs/otlp/v1/logs
export OTEL_EXPORTER_OTLP_HEADERS=x-api-key=your_api_key

Through a Collector. Run an OpenTelemetry Collector next to your app, or as a shared service, when the SDK exports over gRPC or you want one place for batching, sampling and retries. It receives from the SDK and forwards to VitalRix with your key.

otel-collector.yamlyaml
receivers:
  otlp:
    protocols:
      grpc: { endpoint: 0.0.0.0:4317 }
      http: { endpoint: 0.0.0.0:4318 }
processors:
  memory_limiter: { check_interval: 1s, limit_mib: 512 }
  batch: {}
exporters:
  otlphttp/vitalrix:
    traces_endpoint: ${env:VITALRIX_API}/api/v1/traces/otlp/v1/traces
    metrics_endpoint: ${env:VITALRIX_API}/api/v1/metrics/otlp/v1/metrics
    headers:
      x-api-key: ${env:VITALRIX_KEY}
service:
  pipelines:
    traces:  { receivers: [otlp], processors: [memory_limiter, batch], exporters: [otlphttp/vitalrix] }
    metrics: { receivers: [otlp], processors: [memory_limiter, batch], exporters: [otlphttp/vitalrix] }
bash
docker run -d --name otel-collector -p 4317:4317 -p 4318:4318 \
  -e VITALRIX_API=https://api.vitalrix.com -e VITALRIX_KEY=your_api_key \
  -v "$PWD/otel-collector.yaml:/etc/otelcol-contrib/config.yaml" \
  otel/opentelemetry-collector-contrib:latest

Check that it works

  1. Send a few requests to the app.
  2. In VitalRix, open APM: shop-laravel appears in the service catalog within a minute, with request rate, error rate and p95.
  3. Open an endpoint and then one request to see its trace waterfall, including database spans.
  4. If nothing arrives, check the exporter's (or the Collector's) logs for export errors: 401 means the key or header is wrong, 402 that a new host is over the plan's APM host allowance. The packages have a check command that sends one test log.

Add service.version so new releases appear as deploy markers, and put the trace ID in your logs so each trace shows its log lines. See APM and logs, metrics and traces.

Verify the setup and avoid the common mistakes

MistakeEffectFix
Extension not loaded in PHP-FPMCLI works, web requests are not tracedCheck php -m and the FPM php.ini, then restart FPM
No service nameTraces show up as unknown_serviceSet OTEL_SERVICE_NAME in the FPM pool environment
Queue workers not instrumentedJobs are missing from tracesSet the same OTEL_* variables for artisan queue:work
Exporting synchronously on each requestAdded latencyUse the batch span processor
plaintext
; php-fpm pool: pass OpenTelemetry settings to the workers
env[OTEL_PHP_AUTOLOAD_ENABLED] = true
env[OTEL_SERVICE_NAME] = shop-web
env[OTEL_TRACES_EXPORTER] = otlp
env[OTEL_EXPORTER_OTLP_ENDPOINT] = https://otlp.example.com
env[OTEL_PROPAGATORS] = baggage,tracecontext

Frequently asked questions

Does it work without the extension?

Manual instrumentation with the SDK works without it, but auto-instrumentation depends on the extension’s hooks.

Does it slow PHP down?

There is some overhead per request from hooks and span creation. Measure p95 before and after, and sample on very high-traffic sites.

  • OpenTelemetry for FastAPI: Trace a FastAPI service with OpenTelemetry: instrument the app, async database drivers and httpx, run uvicorn workers correctly and export.
  • OpenTelemetry for Go: Instrument a Go service with OpenTelemetry: set up the tracer provider, wrap net/http with otelhttp, pass context everywhere and export the spans.
  • OpenTelemetry for NestJS: Trace a NestJS service with OpenTelemetry: load the SDK before Nest boots, instrument HTTP, Express and NestJS core, and export JSON to VitalRix.
  • OpenTelemetry for Flask: Instrument a Flask app with OpenTelemetry, in code or with zero code: trace routes, SQLAlchemy and outgoing calls, and export to VitalRix.
  • OpenTelemetry for Next.js: Trace a Next.js app with OpenTelemetry using instrumentation.ts: Node.js runtime setup, JSON export straight to VitalRix, fetch spans and Edge caveats.
  • OpenTelemetry for Python: setup guide: Add OpenTelemetry to a Python service: install the distro, auto-instrument with opentelemetry-instrument, export to VitalRix and add custom spans.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.