承接 aduzenko/laravel-configurable-prometheus 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aduzenko/laravel-configurable-prometheus

最新稳定版本:v1.0.1

Composer 安装命令:

composer require aduzenko/laravel-configurable-prometheus

包简介

A package providing configurable interface to export Prometheus metrics in Laravel

README 文档

README

Run Tests Latest Version on Packagist License

A Laravel package for defining, managing, and exporting Prometheus metrics in a flexible, extensible way.

✨ Features

  • Simple configuration-driven metric definitions
  • Full support for Counter, Gauge, Histogram, and Summary
  • Designed for Laravel 12 and PHP 8.3+
  • Define metrics in your app, not just in the package

🚀 Installation

composer require aduzenko/laravel-configurable-prometheus

⚙️ Publishing config

php artisan vendor:publish --tag=prometheus-config

This will publish:

  • config/prometheus.php
  • example metrics route

🔐 Authentication for report route

The Prometheus endpoint is protected from unauthorized access by basic HTTP authentication.

Step 1: Add credentials to your .env file

PROMETHEUS_USER=prom
PROMETHEUS_PASSWORD=secret

📡 Route setup for Prometheus metrics

The Prometheus endpoint is configurable.

Update route in config/prometheus.php file

'endpoint' => 'prometheus',

🧩 Defining custom metrics

Define a class implementing MetricGroup:

namespace App\Metrics;

use AnatolyDuzenko\ConfigurablePrometheus\DTO\MetricDefinition;
use AnatolyDuzenko\ConfigurablePrometheus\Enums\MetricType;
use AnatolyDuzenko\ConfigurablePrometheus\Contracts\MetricGroup;

class ApiMetrics implements MetricGroup
{
    public function definitions(): array
    {
        return [
            new MetricDefinition(
                namespace: 'api',
                name: 'response_time_seconds',
                helpText: 'API response time',
                type: MetricType::Histogram,
                labelNames: ['route'],
                buckets: [0.1, 0.3, 0.5, 1, 2, 5]
            )
        ];
    }
}

Then reference your group in config/prometheus.php:

'groups' => [
    \App\Metrics\ApiMetrics::class,
],

📈 Usage

// In your class constructor, use
public function __construct(protected MetricManager $metrics)
    {}
// then
$this->metrics->inc('users', 'user_logins_total', ['web']);
$this->metrics->set('users', 'active_users', 42, ['web']);
$this->metrics->observe('api', 'response_time_seconds', 0.32, ['/api/v1']);

Alternate usage

// In your method
public function index(Request $request, MetricManager $metrics)
{
    // ....
    $metrics->inc('users', 'user_logins_total', ['web']);
    $metrics->set('users', 'active_users', 42, ['web']);
    $metrics->observe('api', 'response_time_seconds', 0.32, ['/api/v1']);
}

🧪 Testing

vendor/bin/phpunit

📄 License

MIT © Anatoly Duzenko

统计信息

  • 总下载量: 37
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-03