A PHP client for Prometheus
Require this package with composer using the following command:
composer require jlis/php-prometheus
First create an adapter:
$adapter = new \Jlis\PhpPrometheus\Adapter\RedisAdapter(['host' => 'localhost']);
Next, you may pass it to the collector and start collecting metrics:
$collector = new \Jlis\PhpPrometheus\Collector\MetricsCollector($adapter);
$collector->incrementCount('http_requests_total', 1, ['url' => 'http://foo.bar']);
$collector->updateGauge('current_queue_size', 1337, ['queue' => 'notifications']);
$collector->updateHistogram('some_histogram', 3.5, ['color' => 'blue'], [0.1, 1, 2, 3.5, 4, 5, 6, 7, 8, 9]);
header('Content-Type: ' . \Jlis\PhpPrometheus\Collector\MetricsCollector::MIME_TYPE);
echo $collector->render();
Note: Be sure to set the correct content type for Prometheus.
The following adapters are available:
\Jlis\PhpPrometheus\Adapter\InMemoryAdapter
uses a plain PHP array, only suitable for testing.\Jlis\PhpPrometheus\Adapter\RedisAdapter
uses Redis and requires theext-redis
PHP extension.\Jlis\PhpPrometheus\Adapter\ApcuAdapter
uses APCu and requires theext-apcu
PHP extension.
You can easily create your own storage adapter by extending the \Jlis\PhpPrometheus\Adapter\AbstractAdapter
class.