Spatie Laravel Prometheus

repository·main·Indexed 43 days ago

https://github.com/spatie/laravel-prometheus

A Laravel package that exports application and performance metrics to Prometheus for monitoring and visualization in tools like Grafana. It provides built-in support for Laravel queues and Horizon, allows for custom Gauge and Counter metrics via the Prometheus facade, and supports custom collectors and multiple export endpoints.

Tokens
12.1K
Snippets
50
Records
63
Agent score
53%

What's inside Spatie Laravel Prometheus

  1. Export Laravel metrics to Prometheus

    main

    The spatie/laravel-prometheus package allows you to export application metrics to Prometheus. It provides a mechanism to register custom metrics and includes built-in support for Laravel queues and Horizon.

    Metrics are exposed via the /prometheus endpoint. You can configure your Prometheus instance to scrape this endpoint periodically and use tools like Grafana to visualize the data. The package also supports adding a security layer to the /prometheus endpoint to prevent sensitive metrics from being publicly accessible.

    Prometheus::addGauge('User count')
        ->value(fn() => User::count());
  2. How collectors work in Spatie Prometheus

    main

    A collector is a class used to bundle and register metrics, making it easy for package authors to provide metrics to their users.

    To create a collector, you must implement the Spatie\Prometheus\Collectors\Collector interface, which requires a register() method. Inside this method, you use the Prometheus facade to define and register your metrics (such as gauges, counters, etc.).

    namespace Spatie\Prometheus\Collectors;
    
    interface Collector
    {
        public function register(): void;
    }
  3. Enable built-in Queue metrics

    main

    The package provides built-in collectors to monitor Laravel queues, including metrics for queue sizes, pending jobs, and delayed jobs. It supports all Laravel queue drivers (Redis, Database, SQS, etc.).

    To enable these collectors, use the registerQueueCollectors method. You must provide an array of queue names and the name of the queue driver being used.

    Note: If you are using Laravel Horizon, use the Horizon collectors instead of these queue collectors.

    // Enable all queue collectors
    $this->registerQueueCollectors(['high', 'normal', 'low'], 'redis');
  4. Create a custom collector

    main

    To create a custom collector, implement the Spatie\Prometheus\Collectors\Collector interface. In the register() method, use the Prometheus facade to define your metrics.

    For example, to register a gauge that tracks a specific value (like the number of master supervisors in Laravel Horizon), you can use Prometheus::addGauge() and provide a callback to the value() method to resolve the current metric value.

    namespace Spatie\Prometheus\Collectors\Horizon;
    
    use Laravel\Horizon\Contracts\MasterSupervisorRepository;
    use Spatie\Prometheus\Collectors\Collector;
    use Spatie\Prometheus\Facades\Prometheus;
    
    class CurrentMasterSupervisorCollector implements Collector
    {
        public function register(): void
        {
            Prometheus::addGauge('Number of master supervisors')
                ->name('horizon_master_supervisors')
                ->helpText('The number of master supervisors')
                ->value(fn () => app(MasterSupervisorRepository::class)->all());
        }
    }
  5. Register collector classes with the Prometheus facade

    main

    Once you have created a collector class, you need to tell the Prometheus package to use it. You can do this by calling the registerCollectorClasses() method on the Prometheus facade, typically within a Laravel Service Provider's register or boot method. Pass an array of the collector class names to be registered.

    // in a service provider
    
    use Spatie\Prometheus\Facades\Prometheus;
    use Spatie\Prometheus\Collectors\Horizon\CurrentMasterSupervisorCollector;
    
    Prometheus::registerCollectorClasses([
        CurrentMasterSupervisorCollector::class,
    ]);
  6. Set up Grafana.com with the Grafana Agent

    main

    To visualize your Laravel metrics using the hosted Grafana service, you must install the Grafana Agent on your server. The agent acts as a bridge, scraping the /prometheus endpoint of your Laravel application and pushing that data to Grafana.com.

    Setup Steps:

    1. Create a free account at grafana.com.
    2. Launch your Grafana instance from the account dashboard.
    3. Navigate to Connections and add a new connection of type Hosted prometheus Metrics.
    4. Select Via Grafana Agent when prompted.
    5. Follow the wizard to install the agent and generate a configuration file.
    6. To ensure the agent remains running on your server, use a process manager like Supervisord or, if using Laravel Forge, create a daemon.
  7. Export custom metrics to Prometheus

    main

    You can export application-specific metrics by registering them using the Prometheus facade. For example, to export a gauge representing a count that changes over time (like a user count), use addGauge and provide a callback function to the value method that returns the current value.

    Metrics are exposed via the /prometheus endpoint. It is recommended to add a security layer to this endpoint to prevent sensitive metrics from being publicly accessible.

    Prometheus::addGauge('User count')
        ->value(fn() => User::count());
  8. Connect Grafana to Prometheus

    main

    Once Prometheus is running, you need to tell Grafana how to access it as a data source:

    1. Open Grafana at http://your-server-ip:3000.
    2. Log in (default credentials: admin / admin).
    3. Navigate to Data Sources via the gear icon on the left sidebar.
    4. Click Add data source and select Prometheus.
    5. Set the following configuration:
      • Name: Prometheus
      • Type: Prometheus
      • URL: http://localhost:9090 (assuming Prometheus is running on the same server).
    6. Save the data source.
  9. Enable built-in Horizon metrics

    main

    If you are using Laravel Horizon, you can export specific metrics including supervisor status, workload distribution, job throughput, and failure rates using the built-in Horizon collectors.

    To enable these, use the registerHorizonCollectors method.

    // Enable all Horizon collectors
    $this->registerHorizonCollectors();