Instrument a PHP App
This guide walks you through the process of instrumenting PHP applications using zero-code OpenTelemetry for collecting traces, metrics, and logs with KloudMate. Zero-code instrumentation enables you to capture telemetry from many popular libraries and frameworks without modifying your application code.
Prerequisites
Section titled “Prerequisites”- PHP 8.0+ (PHP 8.2 recommended for Laravel)
- PECL
- Composer
- KloudMate workspace API key (used to authorize telemetry ingestion)
Example Application
Section titled “Example Application”For demonstration, we’ll use a simple Slim and Laravel application. You can adapt the same instructions for any other supported framework.
1. Create a Project Directory
Section titled “1. Create a Project Directory”Or use an existing Laravel application.
2. Create the Application
Section titled “2. Create the Application”Create an index.php file in the <project-name> folder and add the following code. The sample code simulates a dice rolling game that returns a random number from 1 to 6:
If you created a new Laravel app, it already includes a default welcome route. You can also add a simple test route in routes/web.php:
3. Run the Application
Section titled “3. Run the Application”Verify that the application is running by visiting localhost:8080/rolldice in your browser.
Verify that the application is running by visiting localhost:8000/rolldice in your browser.
Step 3: Install the OpenTelemetry PHP Extension
Section titled “Step 3: Install the OpenTelemetry PHP Extension”Zero-code instrumentation requires the opentelemetry PHP extension to intercept and instrument PHP function calls automatically.
Linux (Ubuntu/Debian)
Section titled “Linux (Ubuntu/Debian)”Enable the Extension
Section titled “Enable the Extension”Add the following line to your php.ini file:
Tip: Find your
php.inilocation by runningphp --ini.
Verify Installation
Section titled “Verify Installation”You should see opentelemetry in the output.
Step 4: Install OpenTelemetry Packages
Section titled “Step 4: Install OpenTelemetry Packages”You may also need to install the gRPC extension if not already present:
Run the following commands in your Laravel project root directory:
What each package does
Section titled “What each package does”| Package | Purpose |
|---|---|
open-telemetry/opentelemetry-auto-laravel | Automatic zero-code instrumentation hooks for Laravel |
open-telemetry/sdk | OpenTelemetry SDK for trace/metric/log export |
open-telemetry/exporter-otlp | OTLP protocol exporter to send data to collectors/agents |
open-telemetry/api | OpenTelemetry API contracts |
Step 5: Configure Environment Variables
Section titled “Step 5: Configure Environment Variables”Set the following environment variables to configure KloudMate OTLP endpoint and service metadata.
Replace <token> with your KloudMate API key.
Add the following variables to your Laravel .env file:
Replace <token> with your KloudMate API key.
Important Notes
Section titled “Important Notes”| Variable | Description |
|---|---|
OTEL_PHP_AUTOLOAD_ENABLED=true | Critical. Enables the auto-loader to register instrumentations automatically. |
OTEL_SERVICE_NAME | Name that identifies your application in the observability backend. |
OTEL_EXPORTER_OTLP_ENDPOINT | URL of your KloudMate OTLP endpoint. |
OTEL_EXPORTER_OTLP_HEADERS | Your KloudMate API key for authentication. |
OTEL_EXPORTER_OTLP_PROTOCOL | http/protobuf (default) or grpc. Must match what your collector supports. |
Docker to Host Machine (KM Agent on Host)
Section titled “Docker to Host Machine (KM Agent on Host)”If your Laravel app runs in Docker and the KM Agent runs on the host machine, use:
Note: Ensure your KM Agent is listening on
0.0.0.0(all interfaces), not just127.0.0.1, otherwise the container cannot reach it.
Step 6: Run Your Application with Auto-Instrumentation
Section titled “Step 6: Run Your Application with Auto-Instrumentation”Visit localhost:8080/rolldice in your browser. You should see spans on the Traces page in your KloudMate account.
Visit localhost:8000/rolldice in your browser. You should see spans on the Traces page in your KloudMate account.
Step 7: Verify the Integration
Section titled “Step 7: Verify the Integration”1. Confirm the PHP Extension is Active
Section titled “1. Confirm the PHP Extension is Active”Expected output: opentelemetry
Inside Tinker, run:
Expected output: YES
2. Generate Some Traffic
Section titled “2. Generate Some Traffic”Make requests to your application routes to generate telemetry data.
3. Check Your Observability Backend
Section titled “3. Check Your Observability Backend”- If using KM Agent — check the agent logs or the KloudMate dashboard.
- If using OTel Collector — check the collector container/service logs.
You should see traces, spans, and metadata automatically captured from your app.
Step 8: Sending Telemetry to an OpenTelemetry Collector
Section titled “Step 8: Sending Telemetry to an OpenTelemetry Collector”In most production deployments, it’s beneficial to use an OpenTelemetry Collector. Follow these steps to configure and run a local collector.
- Create a file named
otel-collector-config.yamlin the/tmp/directory and save the following configuration:
- Run the following Docker command to start the collector:
- Update your application environment variables to point to the local collector:
- Run the application again. By default, it will export traces and metrics over OTLP/gRPC to the collector running on
localhost:4317.
Optional: Fine-Tuning & Debugging
Section titled “Optional: Fine-Tuning & Debugging”Disable Specific Instrumentations
Section titled “Disable Specific Instrumentations”If you want to disable auto-instrumentation:
Switch to gRPC Protocol
Section titled “Switch to gRPC Protocol”Enable Debug Logging
Section titled “Enable Debug Logging”If traces are not appearing, enable SDK debug mode:
Add Custom Resource Attributes
Section titled “Add Custom Resource Attributes”Quick Reference: Minimal Configuration
Section titled “Quick Reference: Minimal Configuration”The absolute minimum required to get zero-code instrumentation working with KloudMate:
Replace <token> with your KloudMate API key.
Summary
Section titled “Summary”| Step | Action |
|---|---|
| 1 | Install the opentelemetry PHP extension via pecl |
| 2 | Install framework-specific auto-instrumentation packages via Composer |
| 3 | Add OTEL_* environment variables to your configuration |
| 4 | Ensure your KloudMate Agent / Collector is running and accessible |
| 5 | Run your application — instrumentation is fully automatic |
That’s it! Zero-code instrumentation means zero code changes in your application.
Laravel
Section titled “Laravel”This section provides a dedicated, step-by-step walkthrough for instrumenting a Laravel application with OpenTelemetry zero-code instrumentation and sending data to KloudMate.
Prerequisites
Section titled “Prerequisites”- PHP 8.0+ (PHP 8.2 recommended)
- Composer
- An existing Laravel application
- KloudMate workspace API key
Step 1: Create or Use an Existing Laravel App
Section titled “Step 1: Create or Use an Existing Laravel App”If you don’t have an existing app, create one:
Add a simple test route in routes/web.php to generate traffic later:
Step 2: Install the OpenTelemetry PHP Extension
Section titled “Step 2: Install the OpenTelemetry PHP Extension”Zero-code instrumentation requires the opentelemetry PHP extension.
Linux (Ubuntu/Debian)
Section titled “Linux (Ubuntu/Debian)”Enable the Extension
Section titled “Enable the Extension”Add the following line to your php.ini file:
Tip: Find your
php.inilocation by runningphp --ini.
Verify Installation
Section titled “Verify Installation”You should see opentelemetry in the output.
Step 3: Install OpenTelemetry Packages via Composer
Section titled “Step 3: Install OpenTelemetry Packages via Composer”Run the following commands in your Laravel project root directory:
What each package does
Section titled “What each package does”| Package | Purpose |
|---|---|
open-telemetry/opentelemetry-auto-laravel | Automatic zero-code instrumentation hooks for Laravel |
open-telemetry/sdk | OpenTelemetry SDK for trace/metric/log export |
open-telemetry/exporter-otlp | OTLP protocol exporter to send data to collectors/agents |
open-telemetry/api | OpenTelemetry API contracts |
Step 4: Configure Environment Variables
Section titled “Step 4: Configure Environment Variables”Add the following variables to your Laravel .env file:
Replace <token> with your KloudMate API key.
Important Notes
Section titled “Important Notes”| Variable | Description |
|---|---|
OTEL_PHP_AUTOLOAD_ENABLED=true | Critical. Enables the auto-loader to register instrumentations automatically. |
OTEL_SERVICE_NAME | Name that identifies your application in the observability backend. |
OTEL_EXPORTER_OTLP_ENDPOINT | URL of your KloudMate OTLP endpoint. |
OTEL_EXPORTER_OTLP_HEADERS | Your KloudMate API key for authentication. |
OTEL_EXPORTER_OTLP_PROTOCOL | http/protobuf (default) or grpc. Must match what your collector supports. |
Docker to Host Machine (KM Agent on Host)
Section titled “Docker to Host Machine (KM Agent on Host)”If your Laravel app runs in Docker and the KM Agent runs on the host machine, use:
Note: Ensure your KM Agent is listening on
0.0.0.0(all interfaces), not just127.0.0.1, otherwise the container cannot reach it.
Step 5: Understanding Zero-Code Instrumentation
Section titled “Step 5: Understanding Zero-Code Instrumentation”The opentelemetry-auto-laravel package uses the opentelemetry PHP extension to automatically instrument your application without any code changes.
What gets automatically instrumented
Section titled “What gets automatically instrumented”- HTTP Requests — Incoming requests to controllers and middleware
- Database Queries — Eloquent and Query Builder operations
- Cache Operations — Redis, Memcached, and file cache calls
- Queue Jobs — Job dispatching and processing (sync, database, redis drivers)
- Exceptions & Errors — Automatic error tracking and stack traces
- Outgoing HTTP Requests — Guzzle HTTP client calls
What you do NOT need to do
Section titled “What you do NOT need to do”- No manual span creation
- No middleware additions
- No controller modifications
- No service provider registration
- No
usestatements or imports
Step 6: Run the Application
Section titled “Step 6: Run the Application”Visit localhost:8000/rolldice in your browser. You should see spans on the Traces page in your KloudMate account.
Step 7: Verify the Integration
Section titled “Step 7: Verify the Integration”1. Confirm the PHP Extension is Active
Section titled “1. Confirm the PHP Extension is Active”Inside Tinker, run:
Expected output: YES
2. Generate Some Traffic
Section titled “2. Generate Some Traffic”Make requests to your application routes to generate telemetry data.
3. Check Your Observability Backend
Section titled “3. Check Your Observability Backend”- If using KM Agent — check the agent logs or the KloudMate dashboard.
- If using OTel Collector — check the collector container/service logs.
You should see traces, spans, and metadata automatically captured from your Laravel app.
Step 8: Sending Telemetry to an OpenTelemetry Collector
Section titled “Step 8: Sending Telemetry to an OpenTelemetry Collector”In most production deployments, it’s beneficial to use an OpenTelemetry Collector. Follow these steps to configure and run a local collector.
- Create a file named
otel-collector-config.yamlin the/tmp/directory and save the following configuration:
- Run the following Docker command to start the collector:
- Update your application environment variables to point to the local collector:
- Run the application again. By default, it will export traces and metrics over OTLP/gRPC to the collector running on
localhost:4317.
Optional: Fine-Tuning & Debugging
Section titled “Optional: Fine-Tuning & Debugging”Disable Specific Instrumentations
Section titled “Disable Specific Instrumentations”If you want to disable Laravel auto-instrumentation:
Switch to gRPC Protocol
Section titled “Switch to gRPC Protocol”Enable Debug Logging
Section titled “Enable Debug Logging”If traces are not appearing, enable SDK debug mode:
Add Custom Resource Attributes
Section titled “Add Custom Resource Attributes”Quick Reference: Minimal Configuration
Section titled “Quick Reference: Minimal Configuration”The absolute minimum required to get zero-code instrumentation working with KloudMate:
Replace <token> with your KloudMate API key.
Summary
Section titled “Summary”| Step | Action |
|---|---|
| 1 | Install the opentelemetry PHP extension via pecl |
| 2 | Install opentelemetry-auto-laravel + SDK + Exporter via Composer |
| 3 | Add OTEL_* environment variables to your .env file |
| 4 | Ensure your KloudMate Agent / Collector is running and accessible |
| 5 | Run your application — instrumentation is fully automatic |
Source URL for the example application: Getting Started with OpenTelemetry PHP
For more details and advanced configurations, refer to the official OpenTelemetry documentation: