PHP code example of extraswoft / prometheus-exporter
1. Go to this page and download the library: Download extraswoft/prometheus-exporter library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
extraswoft / prometheus-exporter example snippets
composer
'middlewares' => [
InitPrometheusExporterMiddleware::class,
ContextMiddleware::class,
DevToolMiddleware::class,//http://127.0.0.1:20009/__devtool php bin/swoft dev:publish swoft/devtool -f
]
'bootScan' => [
'ExtraSwoft\PrometheusExporter\Boot'
],
'beanScan' => [
"ExtraSwoft\\PrometheusExporter\\"
],
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
/**
* @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注解的方法返回值必须是个数组,逗号代表着层级
/**
* @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);
// }
}
@PECacheTable()
$this->collectorRegistry->cacheTable();
/**
* @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()));
$this->collectorRegistry->getRender();