PHP
PHP auto-instrumentation has two parts: the
opentelemetry PHP extension, which hooks into function calls, and Composer packages that
use those hooks for Laravel, Symfony, Slim, PDO, Guzzle, PSR-18 clients and
more.
You don’t change your code.
Use this page when you run a PHP application (Laravel, Symfony, WordPress or plain PHP) and want it in FuseScope.
| Signal | Status | What is sent |
|---|---|---|
| Traces | Stable | Spans for requests, HTTP calls, database queries. |
| Metrics | Stable | What your code records through the API. |
| Logs | Stable | PSR-3 logger records (Monolog, Laravel) with trace IDs. |
Prerequisites
Section titled “Prerequisites”- PHP 8.1 or later (the extension supports 8.0, current SDK packages need 8.1).
- Composer, with your app using its autoloader (
vendor/autoload.php). - To build the extension from source:
gcc,makeandautoconf. - A FuseScope collector you can reach on port 4317 or 4318. See OTLP.
Install
Section titled “Install”-
Install the extension, with one of:
Terminal window pecl install opentelemetry# or, with PIEpie install open-telemetry/ext-opentelemetry# or, in Docker images with docker-php-extension-installerinstall-php-extensions opentelemetry -
Enable it in
php.ini(PIE andinstall-php-extensionsdo this for you):php.ini [opentelemetry]extension=opentelemetryCheck it loads with
php --ri opentelemetry. -
Add the SDK, the OTLP exporter, an HTTP client for the exporter, and the instrumentation for your framework and libraries:
Terminal window composer require \open-telemetry/sdk \open-telemetry/exporter-otlp \php-http/guzzle7-adapter \open-telemetry/opentelemetry-auto-laravel \open-telemetry/opentelemetry-auto-pdo \open-telemetry/opentelemetry-auto-guzzleSwap
auto-laravelforauto-symfony,auto-slim,auto-wordpressand so on. Each package is listed in the OpenTelemetry registry.
Configure and run
Section titled “Configure and run”export OTEL_PHP_AUTOLOAD_ENABLED=trueexport OTEL_SERVICE_NAME=checkoutexport OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=production,service.version=1.4.2export OTEL_TRACES_EXPORTER=otlpexport OTEL_METRICS_EXPORTER=otlpexport OTEL_LOGS_EXPORTER=otlpexport OTEL_EXPORTER_OTLP_ENDPOINT=http://fusescope-collector.example.com:4318export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobufexport OTEL_PROPAGATORS=baggage,tracecontextphp -S localhost:8080 -t publicOTEL_PHP_AUTOLOAD_ENABLED=true makes the SDK start itself from vendor/autoload.php.
Without it, nothing is sent.
Another option on Linux is the PHP distro: deb, rpm and apk packages with production defaults, installed without Composer.
Send to FuseScope
Section titled “Send to FuseScope”gRPC needs the grpc extension and a transport package:
pecl install grpccomposer require open-telemetry/transport-grpcOTEL_EXPORTER_OTLP_ENDPOINT=http://fusescope-collector.example.com:4317OTEL_EXPORTER_OTLP_PROTOCOL=grpcThe grpc extension takes a long time to compile. HTTP is usually simpler.
OTEL_EXPORTER_OTLP_ENDPOINT=http://fusescope-collector.example.com:4318OTEL_EXPORTER_OTLP_PROTOCOL=http/protobufBehind the chart ingress, use https://otlp.example.com. See
from outside the cluster.
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318OTEL_EXPORTER_OTLP_PROTOCOL=http/protobufA local collector also keeps export off the request path: PHP sends at the end of each request, so a nearby collector answers faster. Run it with Docker, on a VM or in Kubernetes.
Docker
Section titled “Docker”This example installs the extension with install-php-extensions from
mlocati/docker-php-extension-installer,
which also enables it:
FROM php:8.3-apacheCOPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/RUN install-php-extensions opentelemetry zipCOPY --from=composer:2 /usr/bin/composer /usr/bin/composerWORKDIR /var/www/htmlCOPY composer.json composer.lock ./RUN composer install --no-dev --no-scripts --no-autoloaderCOPY . .RUN composer dump-autoload --optimizedocker run -p 8080:80 \ -e OTEL_PHP_AUTOLOAD_ENABLED=true \ -e OTEL_SERVICE_NAME=checkout \ -e OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=production \ -e OTEL_EXPORTER_OTLP_ENDPOINT=http://fusescope-collector.example.com:4318 \ -e OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf \ checkout:latestWith a php:*-fpm image, apply the PHP-FPM caution above.
Kubernetes
Section titled “Kubernetes”Set the variables in the pod spec. The downward API adds the pod name and namespace:
env: - name: OTEL_PHP_AUTOLOAD_ENABLED value: "true" - name: OTEL_SERVICE_NAME value: checkout - name: OTEL_EXPORTER_OTLP_ENDPOINT value: http://fusescope-otel-collector.fusescope.svc.cluster.local:4318 - name: OTEL_EXPORTER_OTLP_PROTOCOL value: http/protobuf - name: K8S_POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: K8S_NAMESPACE_NAME valueFrom: fieldRef: fieldPath: metadata.namespace - name: OTEL_RESOURCE_ATTRIBUTES value: deployment.environment.name=production,k8s.pod.name=$(K8S_POD_NAME),k8s.namespace.name=$(K8S_NAMESPACE_NAME)$(VAR) only expands variables defined earlier in the list, so keep
OTEL_RESOURCE_ATTRIBUTES last.
Any PSR-3 logger, which includes Monolog and the Laravel and Symfony loggers, can be captured by the PSR-3 instrumentation:
composer require open-telemetry/opentelemetry-auto-psr3OTEL_PHP_PSR3_MODE |
Effect |
|---|---|
inject |
Adds trace_id and span_id to each record’s context, for your own log output. |
export |
Also exports each record over OTLP (needs OTEL_LOGS_EXPORTER=otlp). |
The PSR-3 package is still 0.x. To attach OpenTelemetry to Monolog yourself instead, add
the handler from open-telemetry/opentelemetry-logger-monolog. If a collector already reads
your log files or stdout, use inject so each line is stored once.
Check that data arrives
Section titled “Check that data arrives”- Send a few requests to the application.
- Open Services. The service appears under its
OTEL_SERVICE_NAMEwithin a minute. - Open the Traces explorer and filter by
service.name.
Nothing arrives? Check that the extension is loaded where requests run (phpinfo() lists
opentelemetry; the CLI and PHP-FPM can use different php.ini files), that
OTEL_PHP_AUTOLOAD_ENABLED=true reaches PHP (print getenv('OTEL_PHP_AUTOLOAD_ENABLED')),
and that an HTTP client such as php-http/guzzle7-adapter is installed.