bexiocom / prometheus_php
一个与 Prometheus 兼容的 PHP 性能监控库,支持 PHP 5.5, 5.6, 7.0 和 7.1。
dev-master
2017-08-31 18:54 UTC
Requires
- guzzlehttp/streams: ^3.0
Requires (Dev)
- ext-redis: *
- codeclimate/php-test-reporter: ^0.4.4
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^4.0
- sebastian/phpcpd: ^2.0
- squizlabs/php_codesniffer: ^3.0
This package is not auto-updated.
Last update: 2024-09-21 00:20:56 UTC
README
这个库旨在成为一个轻量级的工具,用于使用 Prometheus(Prometheus)对 PHP 应用进行性能监控。它深受 golang 客户端 的启发。
- 这个库应该与 PHP 版本 5.5, 5.6, 7.0 和 7.1 兼容。
- 这个库尽量不对使用环境的假设做任何假设。你应该能够在没有任何魔法或使用任何你正在使用的工具的情况下使用它。无论是轻量级依赖注入、Sympfony 还是其他什么。
特性
- 计数器、度量计和直方图指标类型。
- Redis 和内存存储。
- 渲染为文本格式。
缺少的功能
- 摘要指标类型。
- 能够将指标样本提交到 PushGateway。
- 使用文件系统、Memcached 和 APC 进行存储。
- 渲染为 Protocol buffer 格式。
- Registry 类,简化了在没有依赖注入工具的情况下使用库的情况。
入门指南
将此库添加到您的项目中。
composer require bexiocom/prometheus_php:dev-master
使用方法
没有标签的简单计数器
<?php use Bexio\PrometheusPHP\Metric\Counter; use Bexio\PrometheusPHP\Storage\InMemory; $storage = new InMemory(); $counter = Counter::createFromValues('simple_counter', 'Just a simple counting'); $counter->inc(); $storage->persist($counter);
具有标签的增强计数器
<?php use Bexio\PrometheusPHP\Metric\CounterCollection; use Bexio\PrometheusPHP\Storage\InMemory; $storage = new InMemory(); $collection = CounterCollection::createFromValues('labeled_counter', 'Counting with labels', ['type']); $blueCounter = $collection->withLabels(['type' => 'blue']); $blueCounter->inc(); $redCounter = $collection->withLabels(['type' => 'red']); $redCounter->add(42); $storage->persist($collection);
暴露指标
<?php use Bexio\PrometheusPHP\Metric\Counter; use Bexio\PrometheusPHP\Metric\CounterCollection; use Bexio\PrometheusPHP\Output\TextRenderer; use Bexio\PrometheusPHP\Storage\InMemory; use GuzzleHttp\Stream\BufferStream; $buffer = new BufferStream(); $renderer = TextRenderer::createFromStream($buffer); $storage = new InMemory([ 'simple_counter' => [ InMemory::DEFAULT_VALUE_INDEX => 1 ], 'labeled_counter' => [ '{"type":"blue"}' => 1, '{"type":"red"}' => 42, ], ]); $counter = Counter::createFromValues('simple_counter', 'Just a simple counting'); $renderer->render($counter, $storage->collectSamples($counter)); $collection = CounterCollection::createFromValues('labeled_counter', 'Counting with labels', ['type']); $renderer->render($collection, $storage->collectSamples($collection)); header(sprintf('Content-Type: %s', TextRenderer::MIME_TYPE)); echo $buffer->getContents();
使用 Redis 作为存储后端
注意:当使用 Redis 存储时,强烈建议将
ext-redis
作为项目需求添加。
<?php use Bexio\PrometheusPHP\Storage\Redis; $redis = new \Redis(); $redis->connect('localhost'); // Optional: tune your redis connection. $redis->setOption(\Redis::OPT_READ_TIMEOUT, 10); $storage = new Redis($redis, 'PROMETHEUS:');
使用 Redis 但打开连接轻松
<?php use Bexio\PrometheusPHP\Storage\Redis; $redis = new \Redis(); $storage = new Redis(new \Redis(), 'PROMETHEUS:', function(\Redis $redis) { if (!$redis->connect('localhost')) { throw new \Exception('Failed to connect to Redis server'); } $redis->setOption(\Redis::OPT_READ_TIMEOUT, 10); });
如何贡献
首先,感谢您考虑为这个 PHP Prometheus 客户端库做出贡献。就像您这样的人使得它对更多人有用。
只要遵循以下简单的规则,我们乐于接受拉取请求:
- 新功能必须通过测试进行覆盖
- 所有测试都必须通过
composer check
- 不要混在一起。每个拉取请求只包含一个功能/修复。