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
brew 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
brew 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
Step 1: Install OTel Collector agent in K8s
For Laravel Application deployed on Kubernetes, you need to install OTel Collector agent in your k8s infra to collect and send traces to SigNoz Cloud. You can find the instructions to install OTel Collector agent here.
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
brew install gcc make autoconf
Step 3: Build the extension
With our environment set up we can install the extension using PECL:
pecl install opentelemetry
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/transport-grpc \
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
- 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
From Windows, there are two ways to send data to SigNoz Cloud.
- Send traces directly to SigNoz Cloud
- Send traces via OTel Collector binary (recommended)
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
Initialize the application using
composer init --no-interaction --stability beta
After successful run of prior command a file named composer.json
will be generated if it does not exist. Next run the following command, make sure the version of php
you are using is 7.4
or else you might encounter some issues running the following command.
composer update
Step 2: Build the extension
With our environment set up we can install the extension using PECL:
pecl install opentelemetry
If gRPC is already downloaded and installed, skip the following step.
pecl install grpc
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/transport-grpc guzzlehttp/guzzle
Step 4: Modify php code
<?php
use OpenTelemetry\API\Common\Instrumentation\Globals;
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
use OpenTelemetry\Contrib\Otlp\SpanExporter;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Sdk;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\Sampler\ParentBased;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessorBuilder;
use OpenTelemetry\SDK\Trace\TracerProvider;
use OpenTelemetry\SemConv\ResourceAttributes;
use OpenTelemetry\Contrib\Grpc\GrpcTransportFactory;
use OpenTelemetry\Contrib\Otlp\OtlpUtil;
use OpenTelemetry\API\Common\Signal\Signals;
function initOpenTelemetry()
{
$resource = ResourceInfoFactory::emptyResource()->merge(ResourceInfo::create(Attributes::create([
ResourceAttributes::SERVICE_NAME => '<service_name>'
])));
$headers = [
'signoz-ingestion-key' => "<your-ingestion-key>",
];
$transport = (new GrpcTransportFactory())->create('ingest.<region>.signoz.cloud:443' . OtlpUtil::method(Signals::TRACE), 'application/x-protobuf', $headers);
$spanExporter = new SpanExporter($transport);
$tracerProvider = TracerProvider::builder()
->addSpanProcessor(
(new BatchSpanProcessorBuilder($spanExporter))->build()
)
->setResource($resource)
->setSampler(new ParentBased(new AlwaysOnSampler()))
->build();
Sdk::builder()
->setTracerProvider($tracerProvider)
->setPropagator(TraceContextPropagator::getInstance())
->setAutoShutdown(true)
->buildAndRegisterGlobal();
}
?>
- 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
Step 5: Set environment variables and run app
Run application using your run command or the following generalized command (depending on framework you are using):
php -S localhost:8080 -t public public/index.php
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:
Step 3: Build the extension
With our environment set up we can install the extension using PECL:
pecl install opentelemetry
If gRPC is already downloaded and installed, skip the following step.
pecl install grpc
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/transport-grpc guzzlehttp/guzzle
Step 5: Modify php code
<?php
use OpenTelemetry\API\Common\Instrumentation\Globals;
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
use OpenTelemetry\Contrib\Otlp\SpanExporter;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Sdk;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\Sampler\ParentBased;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessorBuilder;
use OpenTelemetry\SDK\Trace\TracerProvider;
use OpenTelemetry\SemConv\ResourceAttributes;
use OpenTelemetry\Contrib\Grpc\GrpcTransportFactory;
use OpenTelemetry\Contrib\Otlp\OtlpUtil;
use OpenTelemetry\API\Common\Signal\Signals;
function initOpenTelemetry()
{
$resource = ResourceInfoFactory::emptyResource()->merge(ResourceInfo::create(Attributes::create([
ResourceAttributes::SERVICE_NAME => '<service_name>'
])));
$headers = [];
$transport = (new GrpcTransportFactory())->create('<collector_endpoint>' . OtlpUtil::method(Signals::TRACE), 'application/x-protobuf', $headers);
$spanExporter = new SpanExporter($transport);
$tracerProvider = TracerProvider::builder()
->addSpanProcessor(
(new BatchSpanProcessorBuilder($spanExporter))->build()
)
->setResource($resource)
->setSampler(new ParentBased(new AlwaysOnSampler()))
->build();
Sdk::builder()
->setTracerProvider($tracerProvider)
->setPropagator(TraceContextPropagator::getInstance())
->setAutoShutdown(true)
->buildAndRegisterGlobal();
}
?>
Step 6: Set environment variables and run app
Run application using your run command or the following generalized command (depending on framework you are using):
php -S localhost:8080 -t public public/index.php
- 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
Send Traces to Self-Hosted SigNoz
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
brew 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/opentelemetry-auto-laravel \
php-http/guzzle7-adapter \
open-telemetry/exporter-otlp
You can install the additional dependencies provided by OpenTelemetry for different PHP frameworks from here.
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=<self-host-ip> \
OTEL_PROPAGATORS=baggage,tracecontext \
php -S localhost:8080 -t public public/index.php
- Set the
<self-host-ip>
as the IP of Self-Hosted SigNoz. <service_name>
is name of your service
Sample Laravel application
We have included a sample Laravel application at Sample PHP App Github Repo,