tuleap / prometheus-client
Prometheus 仪表库
v1.7.0
2023-12-06 14:51 UTC
Requires
- php: ~8.1.0|~8.2.0|~8.3.0
- ext-json: *
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0 || ^2.0
Requires (Dev)
- doctrine/coding-standard: ^12.0
- guzzlehttp/psr7: ^2.0
- infection/infection: ^0.27
- php-http/discovery: ^1.12
- php-http/guzzle7-adapter: ^1.0.0
- php-http/mock-client: ^1.3
- phpstan/phpstan: ^1.2
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.1
- phpunit/phpunit: ^9.5.10
- psalm/plugin-phpunit: ^0.18.0
- vimeo/psalm: ^5.1
Suggests
- ext-apcu: Required if using the APCu storage.
- ext-redis: Required if using the Redis storage.
- dev-master
- v1.7.0
- v1.6.0
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.0
- v0.9.1
- v0.9.0
- v0.8.0
- v0.7.0
- v0.6.0
- v0.5.1
- v0.5.0
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.0
- v0.2.1
- v0.2.0
- v0.1.0
- dev-dependabot/github_actions/cachix/install-nix-action-v29
- dev-dependabot/composer/phpstan/phpstan-1.12.5
- dev-infection-0.24.0
This package is auto-updated.
Last update: 2024-09-27 07:13:01 UTC
README
此库使用 Redis 或 APCu 进行客户端聚合。如果使用 Redis,建议在 PHP 工作进程旁边运行本地 Redis 实例。
它是如何工作的?
通常 PHP 工作进程不共享任何状态。您可以从三种适配器中选择。Redis、APC 或内存适配器。虽然第一个需要一个单独的二进制运行,但第二个只需要安装 APC 扩展。如果您不需要在请求之间持久化度量(例如,长时间运行的 cron 作业或脚本),则内存适配器可能适合使用。
安装
通过 Composer 安装
composer require tuleap/prometheus-client
使用
简单的计数器
$storage = new \Enalean\Prometheus\Storage\InMemoryStore(); (new \Enalean\Prometheus\Registry\CollectorRegistry($storage)) ->getOrRegisterCounter(\Enalean\Prometheus\Value\MetricName::fromName('some_quick_counter'), 'just a quick measurement') ->inc();
编写一些增强型度量
$storage = new \Enalean\Prometheus\Storage\InMemoryStore(); $registry = new \Enalean\Prometheus\Registry\CollectorRegistry($storage); $counter = $registry->getOrRegisterCounter( \Enalean\Prometheus\Value\MetricName::fromNamespacedName('test', 'some_counter'), 'it increases', \Enalean\Prometheus\Value\MetricLabelNames::fromNames('type') ); $counter->incBy(3, 'blue'); $gauge = $registry->getOrRegisterGauge( \Enalean\Prometheus\Value\MetricName::fromNamespacedName('test', 'some_gauge'), 'it sets', \Enalean\Prometheus\Value\MetricLabelNames::fromNames('type') ); $gauge->set(2.5, 'blue'); $histogram = $registry->getOrRegisterHistogram( \Enalean\Prometheus\Value\MetricName::fromNamespacedName('test', 'some_histogram'), 'it observes', \Enalean\Prometheus\Value\HistogramLabelNames::fromNames('type'), [0.1, 1, 2, 3.5, 4, 5, 6, 7, 8, 9] ); $histogram->observe(3.5, 'blue');
手动注册和检索度量(这些步骤在 getOrRegister...
方法中合并)
$storage = new \Enalean\Prometheus\Storage\InMemoryStore(); $registry = new \Enalean\Prometheus\Registry\CollectorRegistry($storage); $counterA = $registry->registerCounter( \Enalean\Prometheus\Value\MetricName::fromNamespacedName('test', 'some_counter'), 'it increases', \Enalean\Prometheus\Value\MetricLabelNames::fromNames('type') ); $counterA->incBy(3, 'blue'); // once a metric is registered, it can be retrieved using e.g. getCounter: $counterB = $registry->getCounter(\Enalean\Prometheus\Value\MetricName::fromNamespacedName('test', 'some_counter')); $counterB->incBy(2, 'red');
公开度量
$storage = new \Enalean\Prometheus\Storage\InMemoryStore(); $registry = new \Enalean\Prometheus\Registry\CollectorRegistry($storage); $renderer = new \Enalean\Prometheus\Renderer\RenderTextFormat(); header('Content-type: ' . $renderer->getMimeType()); echo $renderer->render($registry->getMetricFamilySamples());
使用 Redis 存储空间
$redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $storage = new \Enalean\Prometheus\Storage\RedisStore($redis); $registry = new \Enalean\Prometheus\Registry\CollectorRegistry($storage); $counter = $registry->registerCounter( \Enalean\Prometheus\Value\MetricName::fromNamespacedName('test', 'some_counter'), 'it increases', \Enalean\Prometheus\Value\MetricLabelNames::fromNames('type') ); $counter->incBy(3, 'blue'); $renderer = new \Enalean\Prometheus\Renderer\RenderTextFormat(); $result = $renderer->render($registry->getMetricFamilySamples());
使用 APCu 存储空间
$storage = new \Enalean\Prometheus\Storage\APCUStore(); $registry = new \Enalean\Prometheus\Registry\CollectorRegistry($storage); $counter = $registry->registerCounter( \Enalean\Prometheus\Value\MetricName::fromNamespacedName('test', 'some_counter'), 'it increases', \Enalean\Prometheus\Value\MetricLabelNames::fromNames('type') ); $counter->incBy(3, 'blue'); $renderer = new \Enalean\Prometheus\Renderer\RenderTextFormat(); $result = $renderer->render($registry->getMetricFamilySamples());
还可以查看 示例。
开发
依赖项
- PHP 7.3+
- PHP Redis 扩展
- PHP APCu 扩展
- Composer
- Redis
启动 Redis 实例
docker-compose up redis
测试
运行测试
composer install
# when Redis is not listening on localhost:
# export REDIS_HOST=192.168.59.100
./vendor/bin/phpunit
# You might need to enable APCu on the CLI
php -d apc.enable_cli=1 vendor/bin/phpunit
使用变异测试运行测试
# when Redis is not listening on localhost:
# export REDIS_HOST=192.168.59.100
./vendor/bin/infection --initial-tests-php-options="-d apc.enable_cli=1"
运行静态分析
vendor/bin/psalm
检查是否符合编码标准
vendor/bin/phpcs
黑盒测试
仅使用 docker-compose 启动 nginx、fpm 和 Redis 配置
docker-compose up
选择您想要测试的适配器。
docker-compose exec phpunit env ADAPTER=apcu vendor/bin/phpunit --testsuite=functionnal
docker-compose exec phpunit env ADAPTER=redis vendor/bin/phpunit --testsuite=functionnal
致谢
此库基于在 Jimdo/prometheus_client_php 上所做的工作。