click / laravel-prometheus-metrics
There is no license information available for the latest version (1.0.1) of this package.
Framework-agnostic Laravel middleware for Prometheus metrics collection
1.0.1
2025-05-07 11:39 UTC
Requires
- php: ^8.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.75
- phpstan/phpstan: ^2.1
README
A simple and extensible Laravel package for collecting and exposing metrics in the Prometheus format. Ideal as a starting point for building custom monitoring solutions.
🚀 Features
- Ready-to-use
/metrics
endpoint for Prometheus scraping - Flexible and centralized configuration
- Extensible architecture with support for custom collectors
- Histogram buckets for operation duration tracking
- Cache support to reduce overhead
📦 Installation
composer require click/laravel-prometheus-metrics
⚙️ Publishing Configuration
Publish the configuration file using Artisan:
php artisan vendor:publish --tag=prometheus-config
Option | Description | Default |
---|---|---|
route_path |
The path to expose metrics | /metrics |
cache_ttl |
Cache duration in seconds (uses Laravel Cache) | 3600 (1 hour) |
prefix |
Metric name prefix (can be set via PREFIX_PROMETHEUS_METRICS env var) |
'' |
buckets |
Histogram buckets for measuring durations | [0.01, 0.05, 0.1, 0.3, 0.5, 1] |
🌐 Routes
After installation, the following route becomes available automatically:
GET /metrics
This route exposes metrics in Prometheus text exposition format.
🧩 Middleware Integration
You can attach Prometheus metric collection to specific routes via middleware.
Option 1: Using an alias
Register the alias in your config/app.php
:
'aliases' => [ 'prometheus' => \YourVendor\Prometheus\Middleware\PrometheusMiddleware::class, // ... ],
Then apply the middleware by alias:
use Illuminate\Support\Facades\Route; Route::get('/health', fn() => ['ok' => true]) ->middleware('prometheus');
Option 2: Using the class directly
use YourVendor\Prometheus\Middleware\PrometheusMiddleware; Route::get('/health', fn() => ['ok' => true]) ->middleware([PrometheusMiddleware::class]);