jlis/php-prometheus

Prometheus的PHP客户端。

v1.0.1 2018-09-11 09:20 UTC

This package is auto-updated.

Last update: 2024-09-12 04:10:05 UTC


README

Prometheus的PHP客户端

Build Status StyleCI

安装

使用以下命令通过composer安装此包

composer require jlis/php-prometheus

用法

首先创建一个适配器

$adapter = new \Jlis\PhpPrometheus\Adapter\RedisAdapter(['host' => 'localhost']);

然后,您可以将它传递给收集器并开始收集指标

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

注意:请确保为Prometheus设置正确的内容类型。

适配器

以下适配器可用

  • \Jlis\PhpPrometheus\Adapter\InMemoryAdapter 使用纯PHP数组,仅适用于测试。
  • \Jlis\PhpPrometheus\Adapter\RedisAdapter 使用Redis,需要PHP的 ext-redis 扩展。
  • \Jlis\PhpPrometheus\Adapter\ApcuAdapter 使用APCu,需要PHP的 ext-apcu 扩展。

您可以通过扩展 \Jlis\PhpPrometheus\Adapter\AbstractAdapter 类轻松创建自己的存储适配器。