promphp/prometheus_client_php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

promphp/prometheus_client_php

Composer 安装命令:

composer require promphp/prometheus_client_php

包简介

Prometheus instrumentation library for PHP applications.

README 文档

README

Tests Slack

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:

composer require promphp/prometheus_client_php

Usage

A simple counter:

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

Write some enhanced metrics:

$registry = \Prometheus\CollectorRegistry::getDefault();

$counter = $registry->getOrRegisterCounter('test', 'some_counter', 'it increases', ['type']);
$counter->incBy(3, ['blue']);

$gauge = $registry->getOrRegisterGauge('test', 'some_gauge', 'it sets', ['type']);
$gauge->set(2.5, ['blue']);

$histogram = $registry->getOrRegisterHistogram('test', 'some_histogram', 'it observes', ['type'], [0.1, 1, 2, 3.5, 4, 5, 6, 7, 8, 9]);
$histogram->observe(3.5, ['blue']);

$summary = $registry->getOrRegisterSummary('test', 'some_summary', 'it observes a sliding window', ['type'], 84600, [0.01, 0.05, 0.5, 0.95, 0.99]);
$summary->observe(5, ['blue']);

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

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

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

\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):

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

Or with an existing connection:

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

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

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

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

Using the PDO storage:

$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: https://github.com/PromPHP/prometheus_push_gateway_php

composer require promphp/prometheus_push_gateway_php

Development

Dependencies

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

promphp/prometheus_client_php 适用场景与选型建议

promphp/prometheus_client_php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29.91M 次下载、GitHub Stars 达 546, 最近一次更新时间为 2020 年 09 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 promphp/prometheus_client_php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 promphp/prometheus_client_php 我们能提供哪些服务?
定制开发 / 二次开发

基于 promphp/prometheus_client_php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 29.91M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 549
  • 点击次数: 31
  • 依赖项目数: 118
  • 推荐数: 12

GitHub 信息

  • Stars: 546
  • Watchers: 4
  • Forks: 108
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2020-09-30