Integrate OpenTelemetry into a PHP application without writing any code.

Ankita LunawatAnkita Lunawat
3 min read

Open Telemetry is a robust framework that offers observability for applications by collecting and exporting telemetry data, and zero-code instrumentation allows you to gain valuable insights into application performance with minimal changes to your existing codebase.

Prerequisites

  • AWS Account with Ubuntu machine.

  • PHP, PECL, composer installed.

Update the package list.

sudo apt update

Install the command-line interface for PHP 8.3 using the package manager.

sudo apt install php8.3-cli

Ensure the installation is correct.

php -v

Install PECL to get the tools needed for PHP extension development.

sudo apt install php-pear php8.3-dev

Download and install Composer, a dependency manager for PHP that makes managing libraries easier.

curl -sS https://getcomposer.org/installer | php

Move the Composer binary to a directory in /usr/local/bin/ to make it accessible globally.

sudo mv composer.phar /usr/local/bin/composer

Check the Composer version to verify the installation.

composer -v

Install build tools needed for creating PECL extensions.

sudo apt-get install gcc make autoconf

Create a PHP project using the Slim Framework.

Create a new project directory.

mkdir opentelemetry-php-example

Navigate to the directory.

cd opentelemetry-php-example

Initialize a new Composer project and add Slim as a dependency.

composer init --no-interaction --require slim/slim:"^4" --require slim/psr7:"^1"

Install the dependencies defined in composer.json using the following command.

composer update

Create the Application File

Create an index.php file with a basic Slim application.

nano index.php

Add the following code to it.

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\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();

Explanation of the code:

  1. The code imports necessary classes and loads dependencies with Composer, then initializes a new Slim application instance using the $app variable to manage routes and handle HTTP requests.

  2. Define the /rolldice Route: Sets up a /rolldice route that generates a random number between 1 and 6, adds it to the response, and sends it back to the client.

  3. Run the Application Starts the application and listens for incoming requests.

Now, start the built-in PHP server to test your application using the following command..

php -S 0.0.0.0:8080

Open http://<Public-IP-Address>:8080/rolldice in your browser to see a random number between 1 and 6.

Integrate OpenTelemetry into a PHP application without writing any additional code using zero-code instrumentation.

Update the PECL channel to obtain the latest available extensions.

sudo pecl channel-update pecl.php.net

Use PECL to install the OpenTelemetry PHP extension.

sudo pecl install opentelemetry

After installing the OpenTelemetry extension, enable it in your PHP configuration (php.ini).

sudo nano /etc/php/8.3/cli/php.ini

Add the following code to it.

[opentelemetry]
extension=opentelemetry.so

Verify that the extension is installed and enabled.

php --ri opentelemetry

Install necessary packages for automatic instrumentation.

composer config allow-plugins.php-http/discovery false
composer require open-telemetry/sdk open-telemetry/opentelemetry-auto-slim

Start the PHP server with OpenTelemetry enabled.

Start the application with OpenTelemetry environment variables to display trace output in the console.

env OTEL_PHP_AUTOLOAD_ENABLED=true \
    OTEL_TRACES_EXPORTER=console \
    OTEL_METRICS_EXPORTER=none \
    OTEL_LOGS_EXPORTER=none \
    php -S 0.0.0.0:8080

Open http://<Public-IP-Address>:8080/rolldice in your browser and refresh the page.

Accessing the application will display trace information in the console, confirming that OpenTelemetry is capturing telemetry data.

0
Subscribe to my newsletter

Read articles from Ankita Lunawat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ankita Lunawat
Ankita Lunawat

I am a dedicated and experienced Cloud Engineer with two years in the industry, specializing in designing, implementing, and managing scalable and secure cloud infrastructures. With a strong foundation in AWS, Azure, and GCP, I excel at leveraging cloud services to optimize performance, enhance security, and reduce operational costs. My expertise includes automated deployment pipelines, infrastructure as code (IaC) with tools like Terraform and container orchestration using Kubernetes and Docker. Throughout my career, I've collaborated with cross-functional teams to deliver robust cloud solutions, ensuring high availability and fault tolerance. I'm passionate about staying at the forefront of cloud technology trends and continuously enhancing my skill set to provide innovative solutions that drive business success. Whether it's migrating legacy systems to the cloud or architecting new cloud-native applications, I bring a strategic approach to every project, focusing on efficiency, scalability, and reliability. In addition to my technical skills, I am an advocate for DevOps practices, promoting a culture of collaboration and continuous improvement within development and operations teams. My commitment to learning and adapting to new technologies ensures that I can meet the evolving needs of any organization and deliver top-tier cloud solutions.