zlodes / prometheus-client-laravel
The Prometheus Metrics client adapter for Laravel Framework
Installs: 11 784
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 2
Requires
- php: ^8.1
- ext-redis: *
- laravel/framework: ^9.0 || ^10.0 || ^11.0
- webmozart/assert: ^1.11
- zlodes/prometheus-client: ^2.0.0
Requires (Dev)
- ergebnis/composer-normalize: dev-main
- mockery/mockery: ^1.5
- orchestra/testbench: ^8.5 || ^9.0
- phpcompatibility/php-compatibility: ^9.3
- phpmd/phpmd: ^2.13
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-webmozart-assert: ^1.2
- phpunit/phpunit: ^10.0 || ^11.0
- psalm/plugin-laravel: ^2.8
- roave/security-advisories: dev-latest
- slevomat/coding-standard: ^8.11
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^5.0
README
This is a Laravel adapter/bridge package for zlodes/prometheus-client.
First steps
Installation
composer require zlodes/prometheus-client-laravel
Register a route for the metrics controller
Your application is responsible for metrics route registration. There is a controller ready to use. You can configure groups, middleware or prefixes as you want.
Example:
use Illuminate\Support\Facades\Route; use Zlodes\PrometheusClient\Laravel\Http\MetricsExporterController; Route::get('/metrics', MetricsExporterController::class);
Configure Storage for metrics [optional]
By default, it uses Redis storage. If you want to use other storage, you can do it easily following these three steps:
- Create a class implements
Storage
interface. - Publish a config:
php artisan vendor:publish --tag=prometheus-client
- Set your
storage
class in the config.
Metrics registration
In your ServiceProvider::register
:
$this->callAfterResolving(Registry::class, static function (Registry $registry): void { $registry ->registerMetric( new Counter('dummy_controller_hits', 'Dummy controller hits count') ) ->registerMetric( new Gauge('laravel_queue_size', 'Laravel queue length by Queue') ); });
Metrics Collector usage
You can work with your metrics whenever you want. Just use Collector
:
use Zlodes\PrometheusClient\Collector\CollectorFactory; class DummyController { public function __invoke(CollectorFactory $collector) { $collector->counter('dummy_controller_hits')->increment(); } }
Schedulable collectors
At times, there may be a need to gather metrics on a scheduled basis. The package offers a feature to register a SchedulableCollector that executes every minute using the Laravel Scheduler.
You can define your SchedulableCollectors
using a config or register it in SchedulableCollectorRegistry directly in a ServiceProvider
:
$this->callAfterResolving( SchedulableCollectorRegistry::class, static function (SchedulableCollectorRegistry $schedulableCollectorRegistry): void { $schedulableCollectorRegistry->push(YourSchedulableCollector::class); } );
Note For further details, see zlodes/prometheus-client
Available console commands
Command | Description |
---|---|
php artisan metrics:list |
Lists all registered metrics |
php artisan metrics:clear |
Clears metrics storage |
metrics:collect-scheduled |
Runs ScheduledCollectors . Using by Scheduler |
Upgrade guide
From 1.x to 2.x
- Run
php artisan vendor:publish --tag=prometheus-client
to publish a brand-new config - Configure the new config based on the previous one (
prometheus-exporter.php
) - Drop legacy config (
prometheus-exporter.php
)
Testing
Run tests
php ./vendor/bin/phpunit