PHP Opentelemetry Instrumentation

This document contains instructions on how to set up OpenTelemetry instrumentation in your PHP 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, if you want to send traces to self hosted SigNoz , you can refer to this. We will be using Zero-code configuration for Automatic Instrumentation.

Step 1: Setup Development Environment

To configure our PHP 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-slim
Info

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=ingest.<region>.signoz.cloud:443 \
    OTEL_EXPORTER_OTLP_HEADERS=signoz-ingestion-key=<INGESTION_KEY> \
    OTEL_PROPAGATORS=baggage,tracecontext \
    php -S localhost:8080 app.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 PHP 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-slim
Info

You can install the additional dependencies provided by OpenTelemetry for different PHP frameworks from here.

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 app.php

You can change the env vars value by referencing values from the following lookup table

Environment VariableValue
OTEL_SERVICE_NAME<SERVICE_NAME> replace it with name of your app
OTEL_EXPORTER_OTLP_ENDPOINT<COLLECTOR_ENDPOINT> replace this with the Otel Collector Endpoint. If you have hosted it somewhere, provide the URL. Otherwise, the default is http://localhost:4317, if you have followed our guide.
php -S localhost:8080 app.phpyou can replace this with the run command of your PHP application

Sample PHP Application

We have included a sample PHP application at Sample PHP App Github Repo,

Tutorial

Here's a tutorial with step by step guide on how to install SigNoz and start monitoring a sample PHP app.

Was this page helpful?