zlodes/prometheus-client-laravel

适用于 Laravel 框架的 Prometheus 指标客户端适配器

2.1.0 2024-04-28 21:49 UTC

This package is auto-updated.

Last update: 2024-08-28 22:29:21 UTC


README

codecov

这是一个 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 存储。如果您想使用其他存储,可以轻松地按照以下三个步骤进行操作

  1. 创建一个实现 Storage 接口的类。
  2. 发布配置
    php artisan vendor:publish --tag=prometheus-client
  3. 在配置中将您的 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

  1. 运行 php artisan vendor:publish --tag=prometheus-client 以发布全新的配置
  2. 根据上一个配置(prometheus-exporter.php)配置新配置
  3. 删除旧配置(prometheus-exporter.php

测试

运行测试

php ./vendor/bin/phpunit