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: Example Application
If you already have a PHP application and don't need to create one from scratch, you can directly jump to the Instrumentation part of this guide.
For this tutorial, we will be using a basic Slim Framework application. However, OpenTelemetry PHP is compatible with other web frameworks such as WordPress, Symfony, and Laravel as well. Feel free to adapt the instructions to your preferred framework.
Step 3: Installation
- Initialize a minimal composer.json file in an empty directory.
2. Create a file called index.php in that same directory and addthe following content to it:
3. Run the application using the following command:
4. Open http://localhost:8080/rolldice in your web browser and ensure that the application is working.
Step 4: Instrumentation
We will use the OpenTelemetry PHP extension to automatically instrument the application.
1. Install the build tools using the following command:
2. Build the extension with PECL
3. Use the following command to verify that the extension is installed and enabled:
4. Add some additional dependencies to your application that are required for the automatic instrumentation of your code:
Step 5: Run the Instrumented App
1. Run the instrumented application to generate traces