PHPackages                             imiphp/imi-prometheus - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. imiphp/imi-prometheus

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

imiphp/imi-prometheus
=====================

imi 框架普罗米修斯（Prometheus）服务监控指标组件。imi Prometheus component.

2.1.x-dev(3y ago)50MulanPSL-2.0PHP

Since Oct 21Pushed 3y ago1 watchersCompare

[ Source](https://github.com/imiphp/imi-prometheus)[ Packagist](https://packagist.org/packages/imiphp/imi-prometheus)[ RSS](/packages/imiphp-imi-prometheus/feed)WikiDiscussions 2.1 Synced 1mo ago

READMEChangelog (2)Dependencies (12)Versions (3)Used By (0)

imi-prometheus
==============

[](#imi-prometheus)

[![Latest Version](https://camo.githubusercontent.com/883753913d85cec98e33f2c4c21f0b3bc0f7539bd641ec8f69be6d5f20dfb472/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696d697068702f696d692d70726f6d6574686575732e737667)](https://packagist.org/packages/imiphp/imi-prometheus)[![Php Version](https://camo.githubusercontent.com/4a5c2ab20974058a8bab53ecb30ac4c2e6bb961df6229b7386fdc097ab53dfa8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e342d627269676874677265656e2e737667)](https://secure.php.net/)[![Swoole Version](https://camo.githubusercontent.com/2b08ceed852c672ccac1ba1e0db0399fd6c269b1f40d58b8d4cf11455a95446d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73776f6f6c652d2533453d342e382e302d627269676874677265656e2e737667)](https://github.com/swoole/swoole-src)[![imi License](https://camo.githubusercontent.com/98bdbfaa35e79370786c656980aab520be3e8af9551d8418a3ca4da1775f84c9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d756c616e50534c253230322e302d627269676874677265656e2e737667)](https://github.com/imiphp/imi-prometheus/blob/2.1/LICENSE)

介绍
--

[](#介绍)

此项目是 imi 框架的普罗米修斯（Prometheus）服务监控指标组件。

普罗米修斯官方文档：

> 正在开发中，随时可能修改，请勿用于生产环境！

安装
--

[](#安装)

`composer require imiphp/imi-prometheus:~2.1.0`

使用说明
----

[](#使用说明)

普罗米修斯组件只支持以实例为单位上报，不支持以 Worker 进程为单位。

### 配置

[](#配置)

首先需要配置 Redis 连接，可以参考文档，这里不再赘述。

**配置监控指标：**

`@app.beans`：

```
[
    'MeterRegistry' => [
        'driver'  => \Imi\Prometheus\PrometheusMeterRegistry::class,
        'options' => [
            'adapter' => [
                'class'   => \Imi\Prometheus\Storage\Redis::class,
                'options' => [
                    // 'poolName' => null, // 连接池名称，如果为 null 则使用默认 Redis 连接池
                    // 'prefix' => 'PROMETHEUS_', // 键名前缀
                ],
            ],
            // 所有标签如设为空字符串或 null 则忽略该标签
            'resultTag' => 'result', // 标签名-结果
            'exceptionTag' => 'exception', // 标签名-异常类名
            'instanceTag' => 'instance', // 标签名-实例
            'workerTag' => 'worker', // 标签名-WorkerId
            'instance' => 'imi', // 当前实例名称，每个实例要设置不同
        ],
    ],
]
```

### 使用

[](#使用)

#### 内置监控

[](#内置监控)

##### 连接池监控

[](#连接池监控)

`@app.beans`：

```
// 连接池监控
'PoolMonitor' => [
    'enable'         => true, // 启用
    // 'pools'       => null, // 监控的连接池名称数组。如果为 null 则代表监控所有连接池
    // 'interval'    => 10, // 上报时间间隔，单位：秒
    // 'countKey'    => 'pool_count', // 连接总数量键名
    // 'usedKey'     => 'pool_used', // 忙碌连接数量键名
    // 'freeKey'     => 'pool_free', // 空间连接数量键名
    // 'workerIdTag' => 'worker_id', // 工作进程 ID 标签名
    // 'poolNameTag' => 'pool_name', // 连接池标签名
],
```

##### Swoole 服务器指标监控

[](#swoole-服务器指标监控)

`@app.beans`：

```
// Swoole 服务器指标监控
'SwooleServerMonitor' => [
    'enable'         => true, // 启用
    // 要监控的指标名称数组
    // 详见：https://wiki.swoole.com/#/server/methods?id=stats
    // 格式1：stats() 指标名称 => 实际指标名称
    // 格式2：stats() 指标名称同时作为实际指标名称
    'stats'          => [
        'connection_num' => 'swoole_connection',
        'request_count'  => 'swoole_request_count',
        'coroutine_num'  => 'swoole_coroutine_num',
        'coroutine_num',
    ],
    // 'interval'    => 10, // 上报时间间隔，单位：秒
    // 'workerIdTag' => 'worker_id', // 工作进程 ID 标签名
],
```

#### 注解

[](#注解)

##### @Counted

[](#counted)

类名：`\Imi\Meter\Annotation\Counted`

计数统计，适合只累加，不减少的统计数据类型。

例如：访问次数统计。

参数名类型默认值描述name`string``imi.counted`指标名称recordFailuresOnly`false``bool`是否只在抛出异常时记录tags`array``[]`标签，键值数组description`string`描述options`array``[]`额外参数，每个驱动不同##### @Gauged

[](#gauged)

类名：`\Imi\Meter\Annotation\Gauged`

适合数字有上下波动的统计。

例如：CPU 占用率统计。

参数名类型默认值描述name`string``imi.counted`指标名称recordFailuresOnly`false``bool`是否只在抛出异常时记录tags`array``[]`标签，键值数组description`string`描述value`string|float``{returnValue}`写入的值；`{returnValue}` 表示方法返回值；`{returnValue.xxx}` 表示方法返回值的属性值；`{params.0}` 表示方法参数值；`{params.0.xxx}` 表示方法参数值的属性值；也可以是固定的 `float` 值operation`int``\Imi\Meter\Enum\GaugeOperation::SET`操作类型。设置`GaugeOperation::SET`；增加`GaugeOperation::INCREMENT`；减少`GaugeOperation::DECREMENT`options`array``[]`额外参数，每个驱动不同##### @Timed

[](#timed)

类名：`\Imi\Meter\Annotation\Timed`

耗时统计。

例如：方法执行耗时

参数名类型默认值描述name`string``imi.counted`指标名称tags`array``[]`标签，键值数组description`string`描述baseTimeUnit`int``\Imi\Meter\Enum\TimeUnit::NANO_SECOND`基础时间单位，默认纳秒，可以使用 `\Imi\Meter\Enum\TimeUnit::XXX` 常量设置。options`array``[]`额外参数，每个驱动不同`options` 在普罗米修斯的特定配置：

```
[
    'histogram' => true, // 设置为柱状图，否则默认为 Summary
    'buckets' => [], // 桶，仅柱状图
    'maxAgeSeconds' => 600, // Summary 最大生存时间
    'percentile' => [], // Summary 百分位
]
```

##### @Histogram

[](#histogram)

类名：`\Imi\Meter\Annotation\Histogram`

柱状图，一般人用不懂，如无特殊需求可以无视。

参数名类型默认值描述name`string``imi.counted`指标名称tags`array``[]`标签，键值数组description`string`描述buckets`array``[]`桶，例如：`[100, 500, 1000]`baseTimeUnit`int``\Imi\Meter\Enum\TimeUnit::NANO_SECOND`基础时间单位，默认纳秒，可以使用 `\Imi\Meter\Enum\TimeUnit::XXX` 常量设置。value`string|float``{returnValue}`写入的值；`{returnValue}` 表示方法返回值；`{returnValue.xxx}` 表示方法返回值的属性值；`{params.0}` 表示方法参数值；`{params.0.xxx}` 表示方法参数值的属性值；也可以是固定的 `float` 值options`array``[]`额外参数，每个驱动不同##### @Summary

[](#summary)

类名：`\Imi\Meter\Annotation\Summary`

采样点分位图，一般人用不懂，如无特殊需求可以无视。

参数名类型默认值描述name`string``imi.counted`指标名称tags`array``[]`标签，键值数组description`string`描述percentile`array``[]`百分位数，例如：`[0.01, 0.5, 0.99]`baseTimeUnit`int``\Imi\Meter\Enum\TimeUnit::NANO_SECOND`基础时间单位，默认纳秒，可以使用 `\Imi\Meter\Enum\TimeUnit::XXX` 常量设置。value`string|float``{returnValue}`写入的值；`{returnValue}` 表示方法返回值；`{returnValue.xxx}` 表示方法返回值的属性值；`{params.0}` 表示方法参数值；`{params.0.xxx}` 表示方法参数值的属性值；也可以是固定的 `float` 值options`array``[]`额外参数，每个驱动不同**代码示例：**

```
use Imi\Meter\Annotation\Gauged;
use Imi\Meter\Annotation\Histogram;
use Imi\Meter\Annotation\Summary;
use Imi\Meter\Annotation\Timed;
use Imi\Meter\Enum\TimeUnit;

/**
 * @Gauged(name="test_memory_usage", description="memory usage", tags={"workerId"="{returnValue.workerId}"}, value="{returnValue.memory}")
 */
public function recordMemoryUsage(): array
{
    return [
        'workerId' => Worker::getWorkerId(),
        'memory'   => memory_get_usage(),
    ];
}

/**
 * @Timed(name="test_timed", description="memory usage", baseTimeUnit=TimeUnit::MILLI_SECONDS, options={"quantiles"={0.1, 0.5, 0.99}})
 */
public function testTimed(): int
{
    $ms = mt_rand(10, 1000);
    usleep($ms * 1000);

    return $ms;
}

/**
 * @Timed(name="test_timed_histogram", description="memory usage", baseTimeUnit=TimeUnit::MILLI_SECONDS, options={"histogram"=true, "buckets"={50, 100, 300, 600, 800, 1000}})
 */
public function testTimedHistogram(): int
{
    $ms = mt_rand(10, 1000);
    usleep($ms * 1000);

    return $ms;
}

/**
 * @Histogram(name="test_histogram", baseTimeUnit=TimeUnit::MILLI_SECONDS, buckets={50, 100, 300, 600, 800, 1000})
 */
public function testHistogram(): int
{
    return mt_rand(10, 1000);
}

/**
 * @Summary(name="test_summary", baseTimeUnit=TimeUnit::MILLI_SECONDS, percentile={0.1, 0.5, 0.99})
 */
public function testSummary(): int
{
    return mt_rand(10, 1000);
}
```

#### 手动操作

[](#手动操作)

```
use \Imi\Meter\Facade\MeterRegistry;
use \Imi\Meter\Enum\TimeUnit;

$description = '我是描述';
$tags = ['result' => 'success'];

// counter
MeterRegistry::getDriverInstance()->counter('testCounterManual', $tags, $description)->increment();

// gauge
MeterRegistry::getDriverInstance()->gauge('testGaugeManual', $tags, $description)->record(114514);

// timer
$timer = MeterRegistry::getDriverInstance()->timer('testTimedManual', $tags, $description, TimeUnit::MILLI_SECONDS);
$timerSample = $timer->start();
usleep(mt_rand(10, 1000) * 1000);
$timerSample->stop($timer);

// timer Histogram
$timer = MeterRegistry::getDriverInstance()->timer('testTimedHistogramManual', $tags, $description, TimeUnit::MILLI_SECONDS, [
    'histogram' => true,
    'buckets'   => [100, 500, 1500],
]);
$timerSample = $timer->start();
usleep(mt_rand(10, 1000) * 1000); // 你的耗时代码
$timerSample->stop($timer);

// Histogram
$value = 114514;
$buckets = [100, 500, 1500];
MeterRegistry::getDriverInstance()->histogram('testHistogramManual', $tags, $description, $buckets)->record($value);

// Summary
$value = 114514;
$percentile = [0.1, 0.5, 0.99];
MeterRegistry::getDriverInstance()->summary('testHistogramManual', $tags, $description, $percentile)->record($value);
```

#### 清除 Redis 驱动存储数据

[](#清除-redis-驱动存储数据)

如果你的同名指标，修改了 `tags`，那么必须清理存储数据，否则会报错。

`vendor/bin/imi-swoole prometheus/wipe`

免费技术支持
------

[](#免费技术支持)

QQ群：17916227 [![点击加群](https://camo.githubusercontent.com/75b53e353bb9e5064662e185a6d39f4bb88c4e45bd3a1240ddf599525edb6afa/68747470733a2f2f7075622e69647171696d672e636f6d2f7770612f696d616765732f67726f75702e706e67 "点击加群")](https://jq.qq.com/?_wv=1027&k=5wXf4Zq)，如有问题会有人解答和修复。

运行环境
----

[](#运行环境)

- [PHP](https://php.net/) &gt;= 7.4
- [Composer](https://getcomposer.org/) &gt;= 2.0
- [Swoole](https://www.swoole.com/) &gt;= 4.8.0
- [imi](https://www.imiphp.com/) &gt;= 2.1

版权信息
----

[](#版权信息)

`imi-prometheus` 遵循 MulanPSL-2.0 开源协议发布，并提供免费使用。

捐赠
--

[](#捐赠)

[![](https://camo.githubusercontent.com/24487a1dd6ab2d9c7b7492027024eae97eba149b95eab696a568034005ae335d/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f696d697068702f696d6940322e312f7265732f7061792e706e67)](https://camo.githubusercontent.com/24487a1dd6ab2d9c7b7492027024eae97eba149b95eab696a568034005ae335d/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f696d697068702f696d6940322e312f7265732f7061792e706e67)

开源不求盈利，多少都是心意，生活不易，随缘随缘……

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~16 days

Total

3

Last Release

1264d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6f917bb42280d114c53cebadc2942a13ee03abe14971089f88895e266d637169?d=identicon)[Yurunsoft](/maintainers/Yurunsoft)

---

Top Contributors

[![Yurunsoft](https://avatars.githubusercontent.com/u/20104656?v=4)](https://github.com/Yurunsoft "Yurunsoft (18 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/imiphp-imi-prometheus/health.svg)

```
[![Health](https://phpackages.com/badges/imiphp-imi-prometheus/health.svg)](https://phpackages.com/packages/imiphp-imi-prometheus)
```

###  Alternatives

[psr/log

Common interface for logging libraries

10.4k1.2B9.1k](/packages/psr-log)[itsgoingd/clockwork

php dev tools in your browser

5.9k27.6M94](/packages/itsgoingd-clockwork)[graylog2/gelf-php

A php implementation to send log-messages to a GELF compatible backend like Graylog2.

41838.2M137](/packages/graylog2-gelf-php)[bugsnag/bugsnag-psr-logger

Official Bugsnag PHP PSR Logger.

32132.5M2](/packages/bugsnag-bugsnag-psr-logger)[consolidation/log

Improved Psr-3 / Psr\\Log logger based on Symfony Console components.

15462.2M7](/packages/consolidation-log)[datadog/php-datadogstatsd

An extremely simple PHP datadogstatsd client

19124.6M15](/packages/datadog-php-datadogstatsd)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
