OTel for PHP
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:
Step 2: Install PHP and setup the app 1. Create a dice-rolling application
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:
Step 2: setup the Opentelemetry auto instrument 1. Download the tools that are required to build the OpenTelemetry PHP extension.
2. Use PECL to build the OpenTelemetry PHP extension.
The result of the above command
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:
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.
The above command will run for an extended period of time.
After successful completion run the below command
6. Set environment variables and run the app
Replace <token>with your API key