Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

prometheus_client_php

> 数据库
Open source

Prometheus instrumentation library for PHP applications

552 stars0 likes1 views
WebsiteGitHub

About

Prometheus instrumentation library for PHP applications

A prometheus client library written in PHP

This library uses Redis or APCu to do the client side aggregation. If using Redis, we recommend running 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 five adapters. Redis, Predis, APC, APCng, or an in-memory adapter. While the first needs a separate binary running, the second and third just need 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.

Installation

Add as Composer dependency:

bash
composer require promphp/prometheus_client_php

Usage

A simple counter:

php
\Prometheus\CollectorRegistry::getDefault()
    ->getOrRegisterCounter('', 'some_quick_counter', 'just a quick measurement')
    ->inc();

Write some enhanced metrics:

…

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

php
$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:

php
$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):

php
\Prometheus\Storage\Redis::setDefaultOptions(
    [
        'host' => '127.0.0.1',
        'port' => 6379,
        'password' => null,
        'timeout' => 0.1, // in seconds
        'read_timeout' => '10', // in seconds
        'persistent_connections' => false
    ]
);

Using the Predis storage (requires predis/predis):

php
$registry = new CollectorRegistry(new \Prometheus\Storage\Predis());

Or with an existing connection:

php
$client = new \Predis\Client(['host' => '127.0.0.1']);
$registry = new CollectorRegistry(\Prometheus\Storage\Predis::fromExistingConnection($client));

Note: Using Redis::setPrefix() and Predis::setPrefix() share the same prefix. Using both adapters with different prefixes in the same application is not supported.

Using the InMemory storage:

php
$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());

Using the APC or APCng storage:

php
$registry = new CollectorRegistry(new APCng());
 or
$registry = new CollectorRegistry(new APC());

(see the README.APCng.md file for more details)

Using the PDO storage:

php
$registry = new CollectorRegistry(new \PDO('mysql:host=localhost;dbname=prometheus', 'username', 'password'));
 or
$registry = new CollectorRegistry(new \PDO('sqlite::memory:'));

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.

Eg:

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

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

Also look at the examples.

PushGateway Support

As of Version 2.0.0 this library doesn't support the Prometheus PushGateway anymore because we want to have this package as small als possible. If you need Prometheus PushGateway support, you could use the companion library:

composer require promphp/prometheus_push_gateway_php

Development

Dependencies

  • PHP ^8.2 (only supported PHP versions are supported)
  • 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=192.168.59.100
./vendor/bin/phpunit

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=apcng vendor/bin/phpunit tests/Test/
docker-compose run phpunit env ADAPTER=redis vendor/bin/phpunit tests/Test/

Performance testing

This currently tests the APC and APCng adapters head-to-head and reports if the APCng adapter is slower for any actions.

phpunit vendor/bin/phpunit tests/Test/ --group Performance

The test can also be run inside a container.

docker-compose up
docker-compose run phpunit vendor/bin/phpunit tests/Test/ --group Performance

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPapcuhacktoberfestmetricsphp

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库