Creating collectors | laravel-prometheus | Spatie

 SPATIE

  Laravel Prometheus
=====================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-prometheus](https://spatie.be/docs/laravel-prometheus/v1)  Advance-usage  Creating collectors

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/laravel-prometheus/v1)

- [ Introduction ](https://spatie.be/docs/laravel-prometheus/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-prometheus/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-prometheus/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-prometheus/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-prometheus/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-prometheus/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-prometheus/v1/about-us)

Basic usage
-----------

- [ Creating gauges ](https://spatie.be/docs/laravel-prometheus/v1/basic-usage/creating-gauges)
- [ Using Horizon exporters ](https://spatie.be/docs/laravel-prometheus/v1/basic-usage/using-horizon-exporters)
- [ Using Queue exporters ](https://spatie.be/docs/laravel-prometheus/v1/basic-usage/using-queue-exporters)

Setting up Prometheus and Grafana
---------------------------------

- [ Introduction ](https://spatie.be/docs/laravel-prometheus/v1/setting-up-prometheus-and-grafana/introduction)
- [ Using grafana.com ](https://spatie.be/docs/laravel-prometheus/v1/setting-up-prometheus-and-grafana/using-grafana-com)
- [ Self-hosted ](https://spatie.be/docs/laravel-prometheus/v1/setting-up-prometheus-and-grafana/self-hosted)
- [ Using fly.io ](https://spatie.be/docs/laravel-prometheus/v1/setting-up-prometheus-and-grafana/using-fly-metrics)

Advanced usage
--------------

- [ Creating multiple endpoints ](https://spatie.be/docs/laravel-prometheus/v1/advance-usage/using-multiple-endpoints)
- [ Creating collectors ](https://spatie.be/docs/laravel-prometheus/v1/advance-usage/creating-collectors)
- [ Using counter type metric ](https://spatie.be/docs/laravel-prometheus/v1/advance-usage/using-counter-metric)

 Creating collectors
===================

When you create a package that contains metrics that should be easily be registered by your package users, you can create a collector.

A collector is a class that implements the `Spatie\Prometheus\Collectors` interface and is responsible for registering the metrics.

This is how the interface looks like.

```
namespace Spatie\Prometheus\Collectors;

interface Collector
{
    public function register(): void;
}
```

In the `register` method of your collector, you should can register metrics, such as gauges. Here's an example collector take from our own package, that will register a gauge to export an horizon metric.

```
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());
    }
}
```

Users of your package can register your collector by calling the `registerCollectorClasses` method on the `Prometheus` facade.

```
// in a service provider

use Spatie\Prometheus\Facades\Prometheus;
use Spatie\Prometheus\Collectors\Horizon\CurrentMasterSupervisorCollector;

Prometheus::registerCollectorClasses([
    CurrentMasterSupervisorCollector::class,
]);
```
