Integration of Prometheus Metrics in PHP: Libraries and Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Prometheus Metrics in PHP: Application Performance Monitoring



Prometheus is a powerful open-source monitoring and alerting system that has become the de facto standard in the Cloud Native world. For integrating Prometheus with PHP applications, there are several mature libraries. In this article, we will look at two main ones: promphp/prometheus_client_php and artisanphpex/prometheus, and show how to add metrics to your PHP project.



Why are metrics needed in PHP?

Monitoring PHP applications allows you to track:

  • Number of requests and their execution time
  • Errors and exceptions
  • Memory and CPU usage
  • Queue status and external services
  • Business metrics (number of orders, registrations, etc.)


Collecting this data helps quickly identify problems, plan scaling, and improve the user experience.



Library promphp/prometheus_client_php

This is the most popular and actively maintained library. It implements the official Prometheus client for PHP and supports all metric types:

  • Counter — a counter that only increases (e.g., number of requests)
  • Gauge — a value that can both increase and decrease (current memory, number of active users)
  • Histogram — a histogram for measuring value distribution (response time, request size)
  • Summary — quantile distribution (similar to Histogram, but with quantile calculation on the client side)


Installation

composer require promphp/prometheus_client_php


Usage Example

<?phprequire 'vendor/autoload.php';

use Prometheus\\CollectorRegistry;use Prometheus\\Storage\\InMemory;use Prometheus\\RenderTextFormat;

// Create storage (can be Redis, APC, InMemory)$adapter = new InMemory();$registry = new CollectorRegistry($adapter);

// Create a counter$counter = $registry->registerCounter( 'app', 'requests_total', 'Total number of HTTP requests', ['method', 'endpoint']);

// Increment on each request$counter->incBy(1, ['GET', '/api/users']);

// Create a histogram for response time$histogram = $registry->registerHistogram( 'app', 'response_time_seconds', 'Response time in seconds', ['method', 'endpoint'], [0.1, 0.5, 1, 2, 5] // buckets);

// Observe request execution time$histogram->observe(0.234, ['GET', '/api/users']);

// Output metrics in Prometheus format$renderer = new RenderTextFormat();$result = $renderer->render($registry->getMetricFamilySamples());

header('Content-Type: ' . RenderTextFormat::MIME_TYPE);echo $result;


Library artisanphpex/prometheus

This is a more lightweight alternative focused on ease of use. It also supports Counter, Gauge, Histogram, and Summary.



Installation

composer require artisanphpex/prometheus


Usage Example

<?phprequire 'vendor/autoload.php';

use ArtisanPhp\\Prometheus\\CollectorRegistry;use ArtisanPhp\\Prometheus\\Storage\\InMemory;use ArtisanPhp\\Prometheus\\RenderTextFormat;

$adapter = new InMemory();$registry = new CollectorRegistry($adapter);

// Create a Gauge$gauge = $registry->registerGauge( 'app', 'memory_usage_bytes', 'Current memory usage in bytes', ['process']);

$gauge->set(memory_get_usage(true), ['main']);

// Create a Summary$summary = $registry->registerSummary( 'app', 'request_duration_milliseconds', 'Request duration in ms', ['method'], 0.95, // quantile 0.01 // error);

$summary->observe(150, ['GET']);

// Output m

Recommendations