PHPackages                             extraswoft/prometheus-exporter - 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. extraswoft/prometheus-exporter

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

extraswoft/prometheus-exporter
==============================

prometheusExporter for swoft

v0.0.1.0(7y ago)53.4k[1 issues](https://github.com/masixun71/swoft-prometheus-exporter/issues)Apache-2.0PHP

Since Aug 3Pushed 7y ago1 watchersCompare

[ Source](https://github.com/masixun71/swoft-prometheus-exporter)[ Packagist](https://packagist.org/packages/extraswoft/prometheus-exporter)[ RSS](/packages/extraswoft-prometheus-exporter/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (7)Versions (11)Used By (0)

[![](https://camo.githubusercontent.com/27d1811ec3c2d961144061854c971d1d88e67ea12b3fab845e94a580ce08fd83/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f76657273696f6e2d76302e302e312e302d7265642e737667)](https://camo.githubusercontent.com/27d1811ec3c2d961144061854c971d1d88e67ea12b3fab845e94a580ce08fd83/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f76657273696f6e2d76302e302e312e302d7265642e737667)[![](https://camo.githubusercontent.com/40132ea396da1fc0c9f978249f670b77aef6e66ffb0f44987e0e2ba3113a175d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e312d6f72616e67652e737667)](https://camo.githubusercontent.com/40132ea396da1fc0c9f978249f670b77aef6e66ffb0f44987e0e2ba3113a175d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e312d6f72616e67652e737667)[![](https://camo.githubusercontent.com/6111524cc44700e3d9915944f83dcb7e7b4ee92d35ff93b17a8c5a0957d3120a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73776f6f6c652d2533453d342e302d626c75652e737667)](https://camo.githubusercontent.com/6111524cc44700e3d9915944f83dcb7e7b4ee92d35ff93b17a8c5a0957d3120a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73776f6f6c652d2533453d342e302d626c75652e737667)

简介
==

[](#简介)

本项目基于github上的swoft开源项目进行组件开发，扩展了一个prometheusExporter sdk组件, prometheus 是一个开源的系统监控和告警平台，通过Exporter即可快速地生成prometheus需要的记录信息， 通过中间件和注解来对监控数据非侵入式注入。

目前新增prometheus 是否持久化的选项

环境强制要求
======

[](#环境强制要求)

1. 根据swoft官方要求即可

配置步骤
====

[](#配置步骤)

1.加载包
-----

[](#1加载包)

```
   composer require extraswoft/prometheus-exporter
```

2.在base.php文件的middlewares里加入middleware
--------------------------------------

[](#2在basephp文件的middlewares里加入middleware)

#### 注意：加入该中间件是对所有请求进行的基本监控，包括持久化。

[](#注意加入该中间件是对所有请求进行的基本监控包括持久化)

```
        'middlewares' => [
            InitPrometheusExporterMiddleware::class,
            ContextMiddleware::class,
            DevToolMiddleware::class,//http://127.0.0.1:20009/__devtool  php bin/swoft dev:publish swoft/devtool -f

        ]
```

3.在app.php文件的bootScan和beanScan加入bean需要扫描的命名空间
---------------------------------------------

[](#3在appphp文件的bootscan和beanscan加入bean需要扫描的命名空间)

```
    'bootScan' => [
            'ExtraSwoft\PrometheusExporter\Boot'
        ],

    'beanScan' => [
            "ExtraSwoft\\PrometheusExporter\\"
        ],
```

4.在.env配置文件中添加以下配置
------------------

[](#4在env配置文件中添加以下配置)

##### PROMETHEUSEXPORTER\_REDIS\_PREFIX，持久化到redis的命名前缀

[](#prometheusexporter_redis_prefix持久化到redis的命名前缀)

##### PROMETHEUSEXPORTER\_PUSHGATEWAY\_HOST, 若采用pushGateway,它的地址

[](#prometheusexporter_pushgateway_host-若采用pushgateway它的地址)

##### PROMETHEUSEXPORTER\_INSTANCE, 实例名，可以配置，也可以用主机名，适合多实例

[](#prometheusexporter_instance-实例名可以配置也可以用主机名适合多实例)

##### PROMETHEUSEXPORTER\_COUNTER\_LINE，swoole table 申请的行数，下两个同理,若为0则不创建table

[](#prometheusexporter_counter_lineswoole-table-申请的行数下两个同理若为0则不创建table)

##### PROMETHEUSEXPORTER\_GAUGE\_LINE，

[](#prometheusexporter_gauge_line)

##### PROMETHEUSEXPORTER\_HISTOGRAM\_LINE，histogram需要的行数要较其他的较多，建议配置多点

[](#prometheusexporter_histogram_linehistogram需要的行数要较其他的较多建议配置多点)

##### PROMETHEUSEXPORTER\_PERSISTENCE，prometheus 是否从redis获取持久化数据

[](#prometheusexporter_persistenceprometheus-是否从redis获取持久化数据)

```
    PROMETHEUSEXPORTER_REDIS_PREFIX=www:www-api:cache:
    PROMETHEUSEXPORTER_PUSHGATEWAY_HOST=http://localhost:9091
    PROMETHEUSEXPORTER_INSTANCE=local
    PROMETHEUSEXPORTER_COUNTER_LINE=1024
    PROMETHEUSEXPORTER_GAUGE_LINE=1024
    PROMETHEUSEXPORTER_HISTOGRAM_LINE=4096
    PROMETHEUSEXPORTER_PERSISTENCE=false
```

日常使用
====

[](#日常使用)

1.注解使用（只支持方法）
-------------

[](#1注解使用只支持方法)

### 注意，若想在Controller里的方法使用(普通的bean可以不关心)，需要同时给Controller添加注解，比如:

[](#注意若想在controller里的方法使用普通的bean可以不关心需要同时给controller添加注解比如)

```
        /**
         * @Controller(prefix="/test")
         * @PECounter()
         * @PEHistogram()
         * @PEGaugeAfter()
         * @PEGauge()
         */
         class TestController
         {
                /**
                 * @PECounter()
                 * @PEHistogram()
                 * @PEGaugeAfter()
                 * @PEGauge()
                 * @RequestMapping()
                 * @return string
                 */
                public function demo()
                {}
         }
```

```
         @PECounter(namespace="test", name="demo", value=1, labels={"test":"ok"}, help="123")
         value是 调用改方法会增加或者减少的值
```

```
         @PEHistogram(namespace="test", name="demo", labels={"test":"ok"}, help="123")
         该注解的作用是记录调用该方法的整个执行时间再进行Histogram
```

```
         @PEGauge(namespace="test", name="demo", value=1, labels={"test":"ok"}, help="123")
         @PEGaugeAfter(namespace="test", name="demo", returnKey="data,aa", labels={"test":"ok"}, help="123")
         两者区别在于一个依赖默认值，一个依赖返回值
         After注解的方法返回值必须是个数组，逗号代表着层级

```

2.方法调用
------

[](#2方法调用)

```
       /**
        * @Inject()
        * @var PECollectorRegistry
        */
       private $pECollectorRegistry;

        /**
         * @Inject()
         * @var PrometheusExporterTable
         */
       private $prometheusExporterTable;

         public function demo()
           {
               $this->collectorRegistry->counterIncr('test', 'demo', 1);
               $this->collectorRegistry->counterIncr('test', 'demo', 1);
               $this->collectorRegistry->counterIncr('test', 'demo', 1);
               $this->collectorRegistry->counterIncr('test', 'demo2', 1);
               $this->collectorRegistry->counterIncr('test', 'demo2', 1);
               $this->collectorRegistry->counterDecr('test', 'demo2', 1);
               $this->collectorRegistry->counterIncr('test', 'demo21', 1, ['test' => 'ok']);
               $this->collectorRegistry->counterIncr('test', 'demo22', 1, ['test' => 'ok', 'test2' => 'ok3'], 'this is good');

               $this->collectorRegistry->gaugeSet('test', 'demo3', "123", ['test' => 'ok']);
               $this->collectorRegistry->gaugeSet('test', 'demo4', "123", ['test' => 'ok']);
               $this->collectorRegistry->gaugeSet('test', 'demo3', "1234", ['test' => 'ok']);

               $this->collectorRegistry->histogramIncr('test', 'demo5', 0.03, ['test' => 'ok', 'kk' => 1]);
               $this->collectorRegistry->histogramIncr('test', 'demo5', 0.1, ['test' => 'ok', 'kk' => 1]);
               $this->collectorRegistry->histogramIncr('test', 'demo5', 8, ['test' => 'ok', 'kk' => 1]);
               $this->collectorRegistry->histogramIncr('test', 'demo5', 11, ['test' => 'ok', 'kk' => 1]);
               $this->collectorRegistry->histogramIncr('test', 'demo5', 0.03, ['test' => 'ok', 'kk' => 1]);
               $this->collectorRegistry->histogramIncr('test', 'demo5', 0.1, ['test' => 'ok', 'kk' => 1]);
               $this->collectorRegistry->histogramIncr('test', 'demo5', 8, ['test' => 'ok', 'kk' => 1]);
               $this->collectorRegistry->histogramIncr('test', 'demo5', 11, ['test' => 'ok', 'kk' => 1]);

       //        foreach($this->collectorRegistry->getCounters() as $key => $value)
       //        {
       //            $res = $this->prometheusExporterTable->getCounterTable()->get($key);
       //            var_dump($res);
       //        }
       //
       //        foreach($this->collectorRegistry->getGauges() as $key => $value)
       //        {
       //            $res = $this->prometheusExporterTable->getGaugeTable()->get($key);
       //            var_dump($res);
       //        }
       //
       //        foreach($this->collectorRegistry->getHistograms() as $key => $value)
       //        {
       //            $res = $this->prometheusExporterTable->getHistogramTable()->get($key);
       //            var_dump($res);
       //        }
           }
```

持久化
===

[](#持久化)

1.注解，给某个方法或接口加上缓存注解，调用即可，灵活方便（推荐）
---------------------------------

[](#1注解给某个方法或接口加上缓存注解调用即可灵活方便推荐)

```
   @PECacheTable()
```

2.自行使用方法
--------

[](#2自行使用方法)

```
   $this->collectorRegistry->cacheTable();
```

使用pushGateway
=============

[](#使用pushgateway)

```
   /**
    * @Inject()
    * @var PushGateway
    */
    private $pushGateway;

   $this->pushGateway->push($this->collectorRegistry, 'swoft', array('instance'=>env('PROMETHEUSEXPORTER_INSTANCE')));
   $this->pushGateway->push($this->collectorRegistry, 'swoft', array('instance'=>gethostname()));
```

获取prometheus文本
==============

[](#获取prometheus文本)

具体可参照下面例子

```
    $this->collectorRegistry->getRender();
```

效果图
===

[](#效果图)

![image]()

注意事项
====

[](#注意事项)

- namespace 和 name加上的大小最好不要超过40字节，可能会被截断
- help参数限制的字符串为30字节以下
- 一开始配置的line大小可能随着业务发展不够了，需要及时扩大

问题
==

[](#问题)

\####1.prometheus怎么用，好用不，搭配grafana怎么用？

#### 答：好用，看完这本你都会了,[prometheus实战](https://songjiayang.gitbooks.io/prometheus/content/)

[](#答好用看完这本你都会了prometheus实战)

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

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 ~23 days

Recently: every ~52 days

Total

10

Last Release

2626d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2b3c64e496bcef59a9a12a74f9383e80acd78ea2fa4836d6ddb8f7f8873b0b69?d=identicon)[masixun71](/maintainers/masixun71)

---

Tags

phpswooleprometheusswoftprometheusExporter

### Embed Badge

![Health badge](/badges/extraswoft-prometheus-exporter/health.svg)

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

###  Alternatives

[swoft/swoft

Modern High performance AOP and Coroutine PHP Framework

5.6k34.5k](/packages/swoft-swoft)[swoft/http-server

swoft http server component

11174.5k8](/packages/swoft-http-server)[extraswoft/jaeger

jaeger-sdk for swoft

204.9k](/packages/extraswoft-jaeger)[swoft/http-client

HTTP Client Component for Swoft

1139.3k4](/packages/swoft-http-client)[swoft/redis

swoft redis component

12168.4k16](/packages/swoft-redis)

PHPackages © 2026

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