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

v0.1.0 2025-12-30 09:29 UTC

This package is auto-updated.

Last update: 2025-12-30 12:49:17 UTC


README

Tests Publish Release Latest Version on Packagist License

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
Mail 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.