为真则非假 / lumen-prometheus-exporter
Lumen的Prometheus导出器
Requires
- php: ^7.2.5 || ^8.0 || ^8.1
- guzzlehttp/guzzle: ^6.3 || ^7.0
- illuminate/support: ^6.0 || ^7.0 || ^8.0 || ^9.0
- promphp/prometheus_client_php: ^2.2.1
Requires (Dev)
- mockery/mockery: ^1.3.3
- orchestra/testbench: ^4.0 || ^5.0 || ^6.0
- phpunit/phpunit: ^8.0 || ^9.0
README
Laravel和Lumen的Prometheus导出器包。
简介
Prometheus是一个带UI和复杂查询语言(PromQL)的时间序列数据库,可以通过HTTP抓取指标、计数器、仪表和直方图。
此包是一个将jimdo/prometheus_client_php桥接到Laravel和Lumen的包装器。
安装
使用Composer安装包
composer require trueifnotfalse/lumen-prometheus-exporter
之后,您可以在应用程序的bootstrap/app.php
中启用外观并注册外观
$userAliases = [ // ... TrueIfNotFalse\LumenPrometheusExporter\PrometheusFacade::class => 'Prometheus', ]; $app->withFacades(true, $userAliases);
然后您应在bootstrap/app.php
中注册服务提供者
$app->register(TrueIfNotFalse\LumenPrometheusExporter\PrometheusServiceProvider::class);
请参阅以下说明,了解如何在应用程序路由、Guzzle调用和SQL查询上启用指标。
配置
包具有默认配置,使用以下环境变量。
PROMETHEUS_NAMESPACE=app
PROMETHEUS_METRICS_ROUTE_ENABLED=true
PROMETHEUS_METRICS_ROUTE_PATH=metrics
PROMETHEUS_METRICS_ROUTE_MIDDLEWARE=null
PROMETHEUS_COLLECT_FULL_SQL_QUERY=true
PROMETHEUS_STORAGE_ADAPTER=memory
PROMETHEUS_REDIS_HOST=localhost
PROMETHEUS_REDIS_PORT=6379
PROMETHEUS_REDIS_TIMEOUT=0.1
PROMETHEUS_REDIS_READ_TIMEOUT=10
PROMETHEUS_REDIS_PERSISTENT_CONNECTIONS=0
PROMETHEUS_REDIS_PREFIX=PROMETHEUS_
要自定义配置值,您可以通过覆盖上述环境变量(通常在应用程序的.env
文件中完成)或复制包含的prometheus.php到config/prometheus.php
,编辑它并在应用程序中使用它如下
$app->loadComponent('prometheus', [ TrueIfNotFalse\LumenPrometheusExporter\PrometheusServiceProvider::class ]);
指标
此包允许您观察以下指标
- 应用程序路由。请求方法、请求路径和状态码的指标。
- Guzzle调用。请求方法、URI和状态码的指标。
- SQL查询。SQL查询和查询类型的指标。
为了在应用程序路由中观察指标(请求和响应之间的时间),您应在应用程序的bootstrap/app.php
中注册以下中间件
$app->middleware([ TrueIfNotFalse\LumenPrometheusExporter\RouteMiddleware::class, ]);
导出的标签是
[ 'method', 'route', 'status_code', ]
为了观察Guzzle指标,您应在bootstrap/app.php
中注册以下提供者
$app->register(TrueIfNotFalse\LumenPrometheusExporter\GuzzleServiceProvider::class);
导出的标签是
[ 'method', 'external_endpoint', 'status_code' ]
为了观察SQL指标,您应在bootstrap/app.php
中注册以下提供者
$app->register(TrueIfNotFalse\LumenPrometheusExporter\DatabaseServiceProvider::class);
导出的标签是
[ 'query', 'query_type', ]
注意:您可以通过关闭PROMETHEUS_COLLECT_FULL_SQL_QUERY
的配置来禁用记录完整查询。
存储适配器
存储适配器用于跨请求持久化指标。默认启用memory
适配器,意味着数据将仅在当前请求中持久化。
我们建议在生产环境中使用redis
或apc
适配器。当然,您的安装必须提供Redis或APC实现。
使用PROMETHEUS_STORAGE_ADAPTER
环境变量来指定存储适配器。
如果使用redis
,则还需要配置PROMETHEUS_REDIS_HOST
和PROMETHEUS_REDIS_PORT
变量。可选地,您可以更改PROMETHEUS_REDIS_TIMEOUT
、PROMETHEUS_REDIS_READ_TIMEOUT
和PROMETHEUS_REDIS_PERSISTENT_CONNECTIONS
变量。
导出指标
包添加了默认启用的/metrics
端点,该端点公开了收集器收集的所有指标。
您可以使用PROMETHEUS_METRICS_ROUTE_ENABLED
环境变量将其打开/关闭,也可以使用PROMETHEUS_METRICS_ROUTE_PATH
环境变量进行更改。
收集器
收集器是一个类,实现了CollectorInterface,负责收集一个或多个指标的数据。
请参阅以下示例。
您可以通过将它们添加到prometheus.php
配置中的collectors
数组来自动加载您的收集器。
示例
示例用法
以下是一个Lumen应用程序的示例用法
// retrieve the exporter (you can also use app('prometheus') or Prometheus::getFacadeRoot()) $exporter = app(\TrueIfNotFalse\LumenPrometheusExporter\PrometheusExporter::class); // register a new collector $collector = new \My\New\Collector(); $exporter->registerCollector($collector); // retrieve all collectors var_dump($exporter->getCollectors()); // retrieve a collector by name $collector = $exporter->getCollector('user'); // export all metrics // this is called automatically when the /metrics end-point is hit var_dump($exporter->export()); // the following methods can be used to create and interact with counters, gauges and histograms directly // these methods will typically be called by collectors, but can be used to register any custom metrics directly, // without the need of a collector // create a counter $counter = $exporter->registerCounter('search_requests_total', 'The total number of search requests.'); $counter->inc(); // increment by 1 $counter->incBy(2); // create a counter (with labels) $counter = $exporter->registerCounter('search_requests_total', 'The total number of search requests.', ['request_type']); $counter->inc(['GET']); // increment by 1 $counter->incBy(2, ['GET']); // retrieve a counter $counter = $exporter->getCounter('search_requests_total'); // create a gauge $gauge = $exporter->registerGauge('users_online_total', 'The total number of users online.'); $gauge->inc(); // increment by 1 $gauge->incBy(2); $gauge->dec(); // decrement by 1 $gauge->decBy(2); $gauge->set(36); // create a gauge (with labels) $gauge = $exporter->registerGauge('users_online_total', 'The total number of users online.', ['group']); $gauge->inc(['staff']); // increment by 1 $gauge->incBy(2, ['staff']); $gauge->dec(['staff']); // decrement by 1 $gauge->decBy(2, ['staff']); $gauge->set(36, ['staff']); // retrieve a gauge $counter = $exporter->getGauge('users_online_total'); // create a histogram $histogram = $exporter->registerHistogram( 'response_time_seconds', 'The response time of a request.', [], [0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0] ); // the buckets must be in asc order // if buckets aren't specified, the default 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0 buckets will be used $histogram->observe(5.0); // create a histogram (with labels) $histogram = $exporter->registerHistogram( 'response_time_seconds', 'The response time of a request.', ['request_type'], [0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0] ); // the buckets must be in asc order // if buckets aren't specified, the default 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0 buckets will be used $histogram->observe(5.0, ['GET']); // retrieve a histogram $counter = $exporter->getHistogram('response_time_seconds');
收集器
这是一个示例收集器实现
<?php declare(strict_types = 1); namespace App\Metrics; use Prometheus\Gauge; class ExampleCollector implements CollectorInterface { /** * @var Gauge */ protected $usersRegisteredGauge; /** * Return the name of the collector. * * @return string */ public function getName() : string { return 'users'; } /** * Register all metrics associated with the collector. * * The metrics needs to be registered on the exporter object. * eg: * ```php * $exporter->registerCounter('search_requests_total', 'The total number of search requests.'); * ``` * * @param PrometheusExporter $exporter */ public function registerMetrics(PrometheusExporter $exporter) : void { $this->usersRegisteredGauge = $exporter->registerGauge( 'users_registered_total', 'The total number of registered users.', ['group'] ); } /** * Collect metrics data, if need be, before exporting. * * As an example, this may be used to perform time consuming database queries and set the value of a counter * or gauge. */ public function collect() : void { // retrieve the total number of staff users registered // eg: $totalUsers = Users::where('group', 'staff')->count(); $this->usersRegisteredGauge->set(36, ['staff']); // retrieve the total number of regular users registered // eg: $totalUsers = Users::where('group', 'regular')->count(); $this->usersRegisteredGauge->set(192, ['regular']); } }