OpenTelemetry Support
Using OpenTelemetry SDKs

OTel for PHP

7min
in this guide, we will walk you through the process of setting up and using opentelemetry in php you will learn how to instrument a simple php application to produce traces, metrics, and logs and export them to kloudmate step 1 prerequisites opentelemetry requires php 8 0+ for automatic instrumentation before moving forward ensure that you have the following installed php 8 0+ pecl composer step 2 install php and setup the app 1 create a dice rolling application mkdir \<project name> && cd \<project name> composer init \\ \ no interaction \\ \ stability beta \\ \ require slim/slim "^4" \\ \ require slim/psr7 "^1" composer update 2\ write the application code create an index php file in the \<project name> folder and add the following code the following sample code simulates a dice rolling game that rolls a dice and returns a random number in the range of one to six \<?php use psr\httpmessage\responseinterface as response; use psr\httpmessage\serverrequestinterface as request; use slim\factory\appfactory; require dir '/vendor/autoload php'; $app = appfactory create(); $app >get('/rolldice', function (request $request, response $response) { $result = random int(1,6); $response >getbody() >write(strval($result)); return $response; }); $app >run(); step 2 setup the opentelemetry auto instrument 1 download the tools that are required to build the opentelemetry php extension sudo apt get install gcc make autoconf sudo apt update sudo apt install php pear php dev zlib1g dev libgrpc dev 2\ use pecl to build the opentelemetry php extension pecl install opentelemetry the result of the above command build process completed successfully installing '/usr/lib/php/20230831/opentelemetry so' install ok channel //pecl php net/opentelemetry 1 1 0 configuration option "php ini" is not set to php ini location you should add "extension=opentelemetry so" to php ini 3\ enable the opentelemetry php extension if the extension opentelemetry enabled in php ini message is returned in the previous step, skip this step add the following code to the /etc/php/\<version>/cli/php ini file \[opentelemetry] extension=opentelemetry so 4\ verify whether the opentelemetry php extension is built and enabled method 1 php m | grep opentelemetry expected output opentelemetry 5\ add additional dependencies required for opentelemetry sdk for php to perform automatic instrumentation on the dice rolling application pecl install grpc the above command will run for an extended period of time after successful completion run the below 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 6\ set environment variables and run the app env otel php autoload enabled=true \\ otel service name=myapp \\ otel traces exporter=otlp \\ otel exporter otlp endpoint=https //otel kloudmate com 4318 \\ otel exporter otlp headers=authorization=\<token> \\ otel propagators=baggage,tracecontext \\ php s localhost 8080 replace \<token> with your api key source url for the example application https //opentelemetry io/docs/instrumentation/net/getting started/