Integrating OpenTelemetry with PHP Applications: A Zero-Code Approach

Ankita LunawatAnkita Lunawat
3 min read

Understanding OpenTelemetry

OpenTelemetry is an open-source observability framework that provides a unified way to generate, collect, and export telemetry data (metrics, logs, and traces). By integrating OpenTelemetry into your PHP application, you can gain valuable insights into its performance, behavior, and errors.

Zero-Code Instrumentation with OpenTelemetry PHP Agent

While OpenTelemetry offers SDKs for manual instrumentation, using a zero-code approach with the OpenTelemetry PHP Agent simplifies the process significantly.

Here's a step-by-step guide on how to integrate OpenTelemetry into your PHP application using the zero-code approach.

Prerequisites

AWS Account with EC2 Instance.

PHP, PECL, composer installed.

Update the list of packages.

sudo apt update

Install the command-line interface for PHP 8.3.

sudo apt install php8.3-cli

Verify that the installation was successful.

php -v

Install PECL to get the tools required 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 confirm the installation.

composer -v

Install the build tools needed for creating PECL extensions.

sudo apt-get install gcc make autoconf

Start Your PHP Project Using the Slim Framework

Create a new directory for your project.

mkdir opentelemetry-php-example

Navigate to the directory using the command cd opentelemetry-php-example.

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 listed in composer.json with the following command.

composer update

Create the Application File

Create an index.php file to hold a basic Slim application.

nano index.php

Add the following code to the file.

<?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. Imports and Autoloading: This imports necessary classes and loads dependencies installed with Composer, while Create Application initializes a new Slim application instance, with the $app variable managing routes and handling HTTP requests.Define the /rolldice Route.Defines a route (/rolldice) that creates a random number from 1 to 6, adds it to the response, and returns it to the client.

  2. Run the Application: Start the application to listen for incoming requests by using the built-in PHP server with 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.

Use OpenTelemetry to add monitoring to your PHP application without writing any additional code.

Updates the PECL channel to access the latest extensions available.

sudo pecl channel-update pecl.php.net

Install the OpenTelemetry PHP extension using PECL.

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 into it.

[opentelemetry]
extension=opentelemetry.so

Ensure the OpenTelemetry extension is installed and enabled.

php --ri opentelemetry

Install the 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 on 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

Hi there! I'm a passionate AWS DevOps Engineer with 2+ years of experience in building and managing scalable, reliable, and secure cloud infrastructure. I'm excited to share my knowledge and insights through this blog. Here, you'll find articles on: AWS Services: Deep dives into core AWS services like EC2, S3, Lambda, and more. DevOps Practices: Best practices for CI/CD, infrastructure as code, and automation. Security: Tips and tricks for securing your AWS environments. Serverless Computing: Building and deploying serverless applications. Troubleshooting: Common issues and solutions in AWS. I'm always eager to learn and grow, and I hope this blog can be a valuable resource for fellow DevOps enthusiasts. Feel free to connect with me on [LinkedIn/Twitter] or leave a comment below!