faktly / laravel-prometheus-metrics
Comprehensive Prometheus metrics exporter for Laravel with optional support for Event Sourcing, Horizon, Meilisearch, Spatie Permission, and more.
Installs: 19
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/faktly/laravel-prometheus-metrics
Requires
- php: ^8.4 || ^8.5
- illuminate/support: ^11 || ^12 || ^13
Requires (Dev)
- laravel/horizon: ^5.41
- laravel/pint: ^1.13
- marcocesarato/php-conventional-changelog: ^1.17
- meilisearch/meilisearch-php: ^1.16
- orchestra/testbench: ^8.0 || ^9.0
- phpunit/phpunit: ^10.5
- spatie/laravel-event-sourcing: ^7.12
- spatie/laravel-permission: ^6.24
- vimeo/psalm: ^6.5
Suggests
- laravel/horizon: For queue metrics via Horizon
- meilisearch/meilisearch-php: For Meilisearch metrics
- spatie/laravel-event-sourcing: For Event Sourcing metrics
- spatie/laravel-permission: For permission and role metrics
This package is auto-updated.
Last update: 2025-12-30 12:49:17 UTC
README
A comprehensive metrics exporter for Laravel that provides application insights via JSON for Prometheus, Grafana, and external monitoring tools. Collects 10+ different metrics from your Laravel application with zero configuration needed.
Features
โจ 10+ Metric Collectors - Database, Sessions, Queue, Mail, Cache, Users, Event Sourcing, Permissions, Horizon, Meilisearch
๐ Fully Optional - Each collector can be disabled independently
๐ High Performance - Built-in caching with configurable TTL
๐ Secure - Token-based authentication with hash-equals comparison
๐ฆ Framework Compatible - Laravel 11+, PHP 8.2+
๐ ๏ธ Configurable - 30+ configuration options via environment variables
๐ JSON Export - Ready for Prometheus/Grafana or custom exporters
๐งช Well Tested - 30+ test methods covering all collectors
Quick Start
Installation
composer require faktly/laravel-prometheus-metrics
php artisan vendor:publish --provider="Faktly\LaravelPrometheusMetrics\LaravelPrometheusMetricsServiceProvider"
Count user session with prometheus_metrics_user_sessions table
In some cases, you might use array/cookie session driver. Those session can not be counted. Therefore the metrics would be messing. Therefore, we provide a database table and middleware, which still counts the user sessions โ no matter which driver is being used.
If you use UUID/ULID, you might want to adjust the published database migration before you apply them.
Configure
PROMETHEUS_METRICS_TOKEN=your-secret-token-here
Configure Prometheus scrape config
You want your locally installed prometheus instance to scrape the laravel metrics. This can be accomplished by adding
the following scrape_config to /etc/prometheus/prometheus.yml:
- job_name: "laravel"
scheme: https
metrics_path: /internal/metrics
http_headers:
X-Metrics-Token:
values: ["YOUR_TOKEN"]
static_configs:
- targets: ["YOUR_HOST:443"]
Exchange YOUR_TOKEN with a secure token. You can create one with openssl rand -hex 64 and YOUR_HOST could be
a local IP:PORT combination or your website HOST:PORT.
Access Metrics
curl -H "X-Metrics-Token: your-secret-token-here" \
http://localhost/internal/metrics
Register Middleware for HTTP metrics
In some cases โ especially legacy apps or with custom Middleware setup โ you might want to register the required middleware for HTTP metrics as soon as possible but after Session middleware. Add this to your Kernel.php or in newer Laravel versions to bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) { // Global HTTP Middleware - runs for every request $middleware->use([ \Faktly\LaravelPrometheusMetrics\Http\Middleware\RecordHttpMetricsMiddleware::class, \Faktly\LaravelPrometheusMetrics\Http\Middleware\TrackPrometheusUserSession::class, ]); // Middleware Priority $middleware->priority([ \Illuminate\Cookie\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Faktly\LaravelPrometheusMetrics\Http\Middleware\RecordHttpMetricsMiddleware::class, \Faktly\LaravelPrometheusMetrics\Http\Middleware\TrackPrometheusUserSession::class, ]);
Test Locally
php artisan prometheus:test-metrics
What Gets Measured
| Collector | Metrics | Optional | Requires |
|---|---|---|---|
| Database | Active connections, queries | โ | Laravel DB |
| Sessions | Active count, driver info | โ | Laravel Sessions |
| Queue | Pending/failed jobs, per-queue | โ | Laravel Queue |
| Sent/failed count | โ | Laravel Mail | |
| Cache | Driver info, hits/misses | โ | Laravel Cache |
| Users | Total count, active, per-role | โ | Laravel Auth |
| Event Sourcing | Events total, aggregates, per-type | โ | Spatie |
| Permissions | Roles, permissions, users per role | โ | Spatie |
| Horizon | Jobs per minute, processes | โ | Laravel |
| Meilisearch | Health, indexes, documents | โ | Meilisearch |
License
MIT License. See LICENSE.md for details.