Laravel Opentelemetry Instrumentation
This document contains instructions on how to set up OpenTelemetry instrumentation in your Laravel applications and view your application traces in SigNoz.
Requirements
Send traces to SigNoz Cloud
Based on your application environment, you can choose the setup below to send traces to SigNoz Cloud.
From VMs, there are two ways to send data to SigNoz Cloud.
Send traces directly to SigNoz cloud
Here we will be sending traces to SigNoz cloud in 4 easy steps. We will be using Zero-code configuration for Automatic Instrumentation.
Step 1: Setup Development Environment
To configure our Laravel application to send data, you need to use OpenTelemetry PHP extension. Since the extension is built from the source, you need to have the build tools, which can be installed using the following command:
sudo apt-get install gcc make autoconf
Step 2: Build the extension
With our environment set up we can install the extension using PECL:
pecl install opentelemetry
pecl install protobuf
After successfully installing the OpenTelemetry extension, add the extension to php.ini
file of your project:
[opentelemetry]
extension=opentelemetry.so
Verify that the extension is enabled by running:
php -m | grep opentelemetry
This should output:
opentelemetry
Step 3: Add the dependencies
Add dependencies required for OpenTelemetry SDK for PHP to perform automatic instrumentation using this command :
composer config allow-plugins.php-http/discovery false
composer require \
open-telemetry/sdk \
open-telemetry/exporter-otlp \
php-http/guzzle7-adapter \
open-telemetry/opentelemetry-auto-laravel
Step 4: Set environment variables and run app
We are passing the environment variables on run time and this way we don't have to change anything in code. Run your application using:
env OTEL_PHP_AUTOLOAD_ENABLED=true \
OTEL_SERVICE_NAME=<service_name> \
OTEL_TRACES_EXPORTER=otlp \
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf \
OTEL_EXPORTER_OTLP_ENDPOINT=ingest.<region>.signoz.cloud:443 \
OTEL_EXPORTER_OTLP_HEADERS=signoz-ingestion-key=<your-ingestion-key> \
OTEL_PROPAGATORS=baggage,tracecontext \
php -S localhost:8080 -t public public/index.php
- Set the
<region>
to match your SigNoz Cloud region - Replace
<your-ingestion-key>
with your SigNoz ingestion key <service_name>
is name of your service
Send traces via OTel Collector binary
Step 1. Install OTel Collector binary
OTel Collector binary helps to collect logs, hostmetrics, resource and infra attributes.
You can find instructions to install OTel Collector binary here in your VM.
Step 2: Setup Development Environment
To configure our Laravel application to send data, you need to use OpenTelemetry PHP extension. Since the extension is built from the source, you need to have the build tools, which can be installed using the following command:
sudo apt-get install gcc make autoconf
Step 3: Build the extension
With our environment set up we can install the extension using PECL:
pecl install opentelemetry
pecl install protobuf
After successfully installing the OpenTelemetry extension, add the extension to php.ini
file of your project:
[opentelemetry]
extension=opentelemetry.so
Verify that the extension is enabled by running:
php -m | grep opentelemetry
This should output:
opentelemetry
Step 4: Add the dependencies
Add dependencies required for OpenTelemetry SDK for PHP to perform automatic instrumentation using this command :
composer config allow-plugins.php-http/discovery false
composer require \
open-telemetry/sdk \
open-telemetry/exporter-otlp \
php-http/guzzle7-adapter \
open-telemetry/opentelemetry-auto-laravel
Step 5: Set environment variables and run app
We are passing the environment variables on run time and this way we don't have to change anything in code. Run your application using:
env OTEL_PHP_AUTOLOAD_ENABLED=true \
OTEL_SERVICE_NAME=<service_name> \
OTEL_TRACES_EXPORTER=otlp \
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf \
OTEL_EXPORTER_OTLP_ENDPOINT=<collector_endpoint> \
OTEL_PROPAGATORS=baggage,tracecontext \
php -S localhost:8080 -t public public/index.php
You can change the env vars value by referencing values from the following lookup table
- Replace
<collector_endpoint>
with the Otel Collector endpoint. If you have hosted it somewhere, provide the URL. Otherwise, the default ishttp://localhost:4317
, if you have followed our guide. <service_name>
is name of your service
Sample Laravel application
We have included a sample Laravel application at Sample PHP App Github Repo,