zlodes / prometheus-client-laravel
适用于 Laravel 框架的 Prometheus 指标客户端适配器
2.1.0
2024-04-28 21:49 UTC
Requires
- php: ^8.1
- ext-redis: *
- laravel/framework: ^9.0 || ^10.0 || ^11.0
- webmozart/assert: ^1.11
- zlodes/prometheus-client: ^2.0.0
Requires (Dev)
- ergebnis/composer-normalize: dev-main
- mockery/mockery: ^1.5
- orchestra/testbench: ^8.5 || ^9.0
- phpcompatibility/php-compatibility: ^9.3
- phpmd/phpmd: ^2.13
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-webmozart-assert: ^1.2
- phpunit/phpunit: ^10.0 || ^11.0
- psalm/plugin-laravel: ^2.8
- roave/security-advisories: dev-latest
- slevomat/coding-standard: ^8.11
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^5.0
README
这是一个 Laravel 适配器/桥接包,用于 zlodes/prometheus-client。
第一步
安装
composer require zlodes/prometheus-client-laravel
注册指标控制器路由
您的应用程序负责指标路由注册。有一个 控制器 可以直接使用。您可以根据需要配置组、中间件或前缀。
示例
use Illuminate\Support\Facades\Route; use Zlodes\PrometheusClient\Laravel\Http\MetricsExporterController; Route::get('/metrics', MetricsExporterController::class);
配置指标存储 [可选]
默认情况下,它使用 Redis 存储。如果您想使用其他存储,可以轻松地按照以下三个步骤进行操作
- 创建一个实现
Storage
接口的类。 - 发布配置
php artisan vendor:publish --tag=prometheus-client
- 在配置中将您的
storage
类设置好。
指标注册
在您的 ServiceProvider::register
$this->callAfterResolving(Registry::class, static function (Registry $registry): void { $registry ->registerMetric( new Counter('dummy_controller_hits', 'Dummy controller hits count') ) ->registerMetric( new Gauge('laravel_queue_size', 'Laravel queue length by Queue') ); });
指标收集器使用
您可以在任何时候使用您的指标。只需使用 Collector
use Zlodes\PrometheusClient\Collector\CollectorFactory; class DummyController { public function __invoke(CollectorFactory $collector) { $collector->counter('dummy_controller_hits')->increment(); } }
可调度收集器
有时,可能需要在计划的基础上收集指标。该包提供了一种功能,可以注册一个可调度的收集器,它每分钟使用 Laravel 调度器执行一次。
您可以使用 配置 定义您的 SchedulableCollectors
或直接在 ServiceProvider
中注册它。
$this->callAfterResolving( SchedulableCollectorRegistry::class, static function (SchedulableCollectorRegistry $schedulableCollectorRegistry): void { $schedulableCollectorRegistry->push(YourSchedulableCollector::class); } );
注意 更多详细信息,请参阅 zlodes/prometheus-client
可用的控制台命令
升级指南
从 1.x 到 2.x
- 运行
php artisan vendor:publish --tag=prometheus-client
以发布全新的配置 - 根据上一个配置(
prometheus-exporter.php
)配置新配置 - 删除旧配置(
prometheus-exporter.php
)
测试
运行测试
php ./vendor/bin/phpunit