Skip to content
Prometheus instrumentation library for PHP applications
PHP Dockerfile
Branch: master
Clone or download
Pull request Compare This branch is 40 commits ahead, 1 commit behind Jimdo:master.
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Run CI on PHP version 7.4 (#25) Nov 29, 2019
nginx run with 1100 samples Jun 24, 2016
php-fpm Add an integration test for PushGateway Sep 21, 2016
tests Update phpunit to version 8 (#23) Nov 29, 2019
.gitattributes Introduce Continuous Integration (#20) Nov 19, 2019
LICENSE run something Jun 17, 2016 Update phpunit to version 8 (#23) Nov 29, 2019
composer.json Update phpunit to version 8 (#23) Nov 29, 2019
docker-compose.yml Add an integration test for PushGateway Sep 21, 2016
phpcs.xml.dist Add code style check into CI pipeline (#21) Nov 29, 2019
phpunit.xml.dist Introduce Continuous Integration (#20) Nov 19, 2019

A prometheus client library written in PHP


This library uses Redis or APCu to do the client side aggregation. If using Redis, we recommend to run a local Redis instance next to your PHP workers.

How does it work?

Usually PHP worker processes don't share any state. You can pick from three adapters. Redis, APC or an in memory adapter. While the first needs a separate binary running, the second just needs the APC extension to be installed. If you don't need persistent metrics between requests (e.g. a long running cron job or script) the in memory adapter might be suitable to use.


Add as Composer dependency:

composer require endclothing/prometheus_client_php


A simple counter:

    ->getOrRegisterCounter('', 'some_quick_counter', 'just a quick measurement')

Write some enhanced metrics:

$registry = \Prometheus\CollectorRegistry::getDefault();

$counter = $registry->getOrRegisterCounter('test', 'some_counter', 'it increases', ['type']);
$counter->incBy(3, ['blue']);

$gauge = $registry->getOrRegisterGauge('test', 'some_gauge', 'it sets', ['type']);
$gauge->set(2.5, ['blue']);

$histogram = $registry->getOrRegisterHistogram('test', 'some_histogram', 'it observes', ['type'], [0.1, 1, 2, 3.5, 4, 5, 6, 7, 8, 9]);
$histogram->observe(3.5, ['blue']);

Manually register and retrieve metrics (these steps are combined in the getOrRegister... methods):

$registry = \Prometheus\CollectorRegistry::getDefault();

$counterA = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counterA->incBy(3, ['blue']);

// once a metric is registered, it can be retrieved using e.g. getCounter:
$counterB = $registry->getCounter('test', 'some_counter')
$counterB->incBy(2, ['red']);

Expose the metrics:

$registry = \Prometheus\CollectorRegistry::getDefault();

$renderer = new RenderTextFormat();
$result = $renderer->render($registry->getMetricFamilySamples());

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

Change the Redis options (the example shows the defaults):

        'host' => '',
        'port' => 6379,
        'password' => null,
        'timeout' => 0.1, // in seconds
        'read_timeout' => 10, // in seconds
        'persistent_connections' => false

Using the InMemory storage:

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

$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counter->incBy(3, ['blue']);

$renderer = new RenderTextFormat();
$result = $renderer->render($registry->getMetricFamilySamples());

Advanced Usage

Advanced Histogram Usage

On passing an empty array for the bucket parameter on instantiation, a set of default buckets will be used instead. Whilst this is a good base for a typical web application, there is named constructor to assist in the generation of exponential / geometric buckets.


Histogram::exponentialBuckets(0.05, 1.5, 10);

This will start your buckets with a value of 1.5, grow them by a factor of 1.5 per bucket across a set of 10 buckets.

Also look at the examples.



  • PHP ^7.3
  • PHP Redis extension
  • PHP APCu extension
  • Composer
  • Redis

Start a Redis instance:

docker-compose up Redis

Run the tests:

composer install

# when Redis is not listening on localhost:
# export REDIS_HOST=

Black box testing

Just start the nginx, fpm & Redis setup with docker-compose:

docker-compose up

Pick the adapter you want to test.

docker-compose run phpunit env ADAPTER=apc vendor/bin/phpunit tests/Test/
docker-compose run phpunit env ADAPTER=redis vendor/bin/phpunit tests/Test/
You can’t perform that action at this time.