aduzenko/laravel-configurable-prometheus

A package providing configurable interface to export Prometheus metrics in Laravel

v1.0.0 2025-05-03 16:21 UTC

This package is auto-updated.

Last update: 2025-05-05 14:27:37 UTC


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