Set up production instrumentation for PHP
Follow these steps set up production Application Observability with a telemetry collector:
- Install the instrumentation library
- Configure an application
- Set up a telemetry data collector
- Run the application
- Observe the service in Application Observability
Note
For a quick and easy local development setup, consult the quickstart documentation.
Install the instrumentation library
The OpenTelemetry PHP extension requires the following to be installed:
Install the latest release of the OpenTelemetry PHP extension:
pecl install opentelemetry
Install the protobuf extension for improved OTLP exporter performance:
pecl install protobuf
Add the extensions to php.ini
:
extension=protobuf.so
[opentelemetry]
extension=opentelemetry.so
Install required packages via composer
, including auto-instrumentation for the Slim framework:
composer require \
open-telemetry/sdk \
open-telemetry/exporter-otlp \
open-telemetry/opentelemetry-auto-slim \
php-http/guzzle7-adapter
If Composer autoloading is not already enabled, modify the application entrypoint to include this line:
require __DIR__ . '/vendor/autoload.php';
Note
A number of popular PHP libraries have auto-instrumention packages available, including Laravel and WordPress. You can find the full list here.
Configure an application
Next, customize the following shell script to configure an application:
export OTEL_SERVICE_NAME=<Service Name>
export OTEL_RESOURCE_ATTRIBUTES=deployment.environment=<Environment>,service.namespace=<Namespace>,service.version=<Version>,service.instance.id=<Instance>
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
php -S localhost:8080
- Choose a Service Name to identify the service, for example
cart
- Add attributes to filter data:
- deployment.environment: Name of the deployment environment (
staging
orproduction
) - service.namespace: A namespace to group similar services
(e.g.
shop
would createshop/cart
in Application Observability) - service.version: The application version, to see if a new version has introduced a bug
- service.instance.id: The unique instance, for example the Pod name (a UUID is generated by default)
- deployment.environment: Name of the deployment environment (
- Replace
php -S localhost:8080
with the start command that runs the application to instrument
Set up a telemetry data collector
In production environments, a robust and flexible observability setup needs to process telemetry data before ingestion into databases. Follow the collector setup documentation to set up a collector to process and send telemetry data to Application Observability.
Finally, set the following environment variables from the exporter configuration:
Configuration | Options | Result |
---|---|---|
export OTEL_EXPORTER_OTLP_ENDPOINT=<host> | http://localhost:4317 , remote host address | The default local host address, or a remote host address. |
export OTEL_EXPORTER_OTLP_PROTOCOL=<protocol> | grpc , http/protobuf | The default grpc protocol or http/protobuf |
For example, for a local Grafana Alloy:
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
export OTEL_EXPORTER_OTLP_PROTOCOL=grpc
Run the application
Finally, run the application with the shell script and make some requests to the service to send data to Grafana Cloud.
Observe the service in Application Observability
Open Application observability:
- Navigate to a stack
https://<your-stack-name.>grafana.net
- Expand the top left menu below the Grafana logo
- Click on Application
Activate metrics generation
Application Observability relies on metrics generated from traces already sent to Grafana Cloud Traces.
Metrics generation is self-serve, and can be enabled during onboarding and disabled from Application Observability configuration.
To complete the setup, click Activate Application Observability to enable metrics generation.
Note
After activating Application Observability and enabling metrics generation, it might take up to five minutes to generate metrics and show graphs.
Visualize and discover
Discover more about Application Observability in the documentation:
- Service Inventory: filter, and search services and view RED metrics.
- Service Overview: traces, logs, RED metrics, operations, and runtime information.
- Service Map: graph of connected services, service dependencies, and data flow.