erickskrauch/prometheus

Prometheus instrumentation library for PHP applications.

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/erickskrauch/prometheus

0.1.0 2025-12-05 16:40 UTC

This package is auto-updated.

Last update: 2025-12-05 16:50:40 UTC


README

Prometheus metrics collector and exporter for PHP. Storage is abstracted to allow better integration with your infrastructure.

Latest Version on Packagist Total Downloads Software License Build Status

Installation

Install it as Composer dependency:

composer require erickskrauch/prometheus

Usage

use ErickSkrauch\Prometheus\CollectorRegistry;
use ErickSkrauch\Prometheus\NamespacedRegistry;
use ErickSkrauch\Prometheus\Storage\Redis;
use ErickSkrauch\Prometheus\Storage\Redis\PHPRedis;

$redisClientImplementation = PHPRedis::create(['host' => 'localhost', 'port' => 6379]);
$storage = new Redis($redisClientImplementation);
$registry = new CollectorRegistry($storage);
// Might be useful to prefix all metrics in your app
// $registry = new NamespacedRegistry('github', $registry);

$registry->counter('readme_readers_total')->inc()
$registry->gauge('stars_count', 'How many people read this code example and helped promote the project')->set(1_000);
$registry->histogram('delay_before_decision_to_install_seconds', [1, 5, 10, 20, 60] ['trafficSource'])->observe(5.78, ['google']);

In addition to the PHPRedis extension, it also offers an implementation for the Predis library. You can easily write your own adapter for any Redis client by implementing the \ErickSkrauch\Prometheus\Storage\Redis\RedisClient interface.

An in-memory storage driver is also available via \ErickSkrauch\Prometheus\Storage\InMemory.

To expose metrics, register a route in your framework, such as this example for Symfony:

use ErickSkrauch\Prometheus\RegistryInterface;
use ErickSkrauch\Prometheus\Renderer\RenderTextFormat;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class MetricsController
{
    #[Route('/metrics', name: 'prometheus_metrics')]
    public function expose(RegistryInterface $registry): Response
    {
        $renderer = new RenderTextFormat();

        return new Response(
            $renderer->render($registry->collectMetrics()),
            headers: ['Content-Type' => RenderTextFormat::MIME_TYPE],
        );
    }
}