OpenTelemetry Instrumentation for PHP
Overview
OpenTelemetry provides instrumentation for PHP applications, enabling you to collect telemetry data such as traces and metrics. Although Lumigo does not offer its own OpenTelemetry Distribution for PHP, you can still configure it. This guide explains how to set up OpenTelemetry PHP Automatic Instrumentation and configure it to report data to Lumigo.
Prerequisites
Before you begin, ensure you have the following:
- PHP Version: PHP 8.0 or higher is required for automatic instrumentation.
- Composer: A dependency manager for PHP.
- PECL: PHP Extension Community Library, used for installing PHP extensions.
Installation Steps
Install OpenTelemetry PHP Instrumentation and instrument your PHP application using the provided packages.
1. Install the OpenTelemetry PHP Extension
The OpenTelemetry extension enables automatic instrumentation by leveraging PHP's Observability API.
a. Install Required Build Tools
Depending on your operating system, install the necessary build tools:
-
Linux (using apt):
sudo apt-get install gcc make autoconf
-
macOS (using Homebrew):
brew install gcc make autoconf
b. Install the OpenTelemetry Extension via PECL
pecl install opentelemetry
c. Enable the Extension inphp.ini
Add the following line to your php.ini
file:
[opentelemetry]
extension=opentelemetry.so
d. Verify the Installation
Run the following command to ensure the extension is installed and enabled:
php --ri opentelemetry
You should see output showing that OpenTelemetry support is enabled.
2. Install the OpenTelemetry SDK and Instrumentation Libraries
With the extension installed, you need to now to install the OpenTelemetry SDK and the necessary instrumentation libraries for your application. OTEL has automatic instrumentation available for most of the most commonly used PHP libraries. For a full list, see instrumentation libraries on packagist . In the example below, we will assume your application uses the Slim Framework and a PSR-18 HTTP client:
composer require \
open-telemetry/sdk \
open-telemetry/exporter-otlp \
open-telemetry/opentelemetry-auto-slim \
open-telemetry/opentelemetry-auto-psr18
This command installs the SDK, an OTLP exporter, and auto-instrumentation packages for the Slim Framework and PSR-18 HTTP clients.
3. Configure the OpenTelemetry SDK
You can configure the SDK by using environment variables, or by setting directives in the php.ini
file. To send the collected telemetry data to Lumigo, configure the OpenTelemetry exporter to use Lumigo's OTLP endpoint:
a. Obtain Your Lumigo Token
Log in to your Lumigo account and navigate to Settings
> Tracing
> Manual tracing
to retrieve your Lumigo token.
b. Environment Variable Configuration
Set the following environment variables when running your application:
export OTEL_PHP_AUTOLOAD_ENABLED=true
export OTEL_SERVICE_NAME=your-service-name
export OTEL_TRACES_EXPORTER=otlp
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_ENDPOINT=https://ga-otlp.lumigo-tracer-edge.golumigo.com
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=LumigoToken YOUR_LUMIGO_TOKEN"
export OTEL_PROPAGATORS=baggage,tracecontext
c.php.ini
Configuration
Alternatively, you can add the following lines to your php.ini
file:
[opentelemetry]
otel_php_autoload_enabled=true
otel_service_name=your-service-name
otel_traces_exporter=otlp
otel_exporter_otlp_protocol=http/protobuf
otel_exporter_otlp_endpoint=https://ga-otlp.lumigo-tracer-edge.golumigo.com
otel_exporter_otlp_headers=Authorization=LumigoToken YOUR_LUMIGO_TOKEN
otel_propagators=baggage,tracecontext
Replace your-service-name
with the desired name for your service.
Replace YOUR_LUMIGO_TOKEN
with the token obtained from your Lumigo account. By completing these steps, your PHP application will be instrumented with OpenTelemetry and configured to send trace data to Lumigo for monitoring and analysis.
4. Run Your Application
After completing the installation and configuration, start your application as usual. The installed instrumentation libraries will automatically generate and export traces for supported libraries and frameworks used within your application.
For example, with the Slim Framework and PSR-18 HTTP client instrumentation, you can expect spans representing HTTP transactions, actions executed, and each HTTP request sent by the PSR-18 client.
Updated about 17 hours ago