bushlanov-dev / laravel-prometheus-exporter
Laravel and Lumen prometheus exporter
Package info
github.com/BushlanovDev/laravel-prometheus-exporter
pkg:composer/bushlanov-dev/laravel-prometheus-exporter
Requires
- php: >=8.1
- guzzlehttp/guzzle: ^7.2 || ^8.0
- illuminate/routing: ^9.0||^10.0||^11.0||^12.0||^13.0
- illuminate/support: ^9.0||^10.0||^11.0||^12.0||^13.0
- promphp/prometheus_client_php: ^2.14
Requires (Dev)
- bushlanov-dev/php-coverage-badger: ^2.1
- friendsofphp/php-cs-fixer: ^3.65
- laravel/lumen-framework: ^9.0||^10.0||^11.0
- mockery/mockery: ^1.6.12
- orchestra/testbench: ^7.0|^8.0|^9.0|^10.0|^11.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^10.5.32||^11.5||^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A prometheus exporter for the Laravel and the Lumen.
This package is a wrapper bridging promphp/prometheus_client_php
Installation
composer require bushlanov-dev/laravel-prometheus-exporter
Laravel
The service provider and the Prometheus facade alias are registered automatically via Laravel package discovery.
If package discovery is disabled in your application, register the provider manually.
Laravel 11+ register the service provider in bootstrap/providers.php:
return [ // ... BushlanovDev\LaravelPrometheusExporter\Providers\PrometheusServiceProvider::class, ];
Old versions of laravel register the service provider in config/app.php:
'providers' => [ // ... BushlanovDev\LaravelPrometheusExporter\Providers\PrometheusServiceProvider::class, ];
Laravel 11+ register the middleware in bootstrap/app.php:
// ... ->withMiddleware(function (Middleware $middleware) { $middleware->alias([ 'prometheus' => BushlanovDev\LaravelPrometheusExporter\Middleware\PrometheusLaravelMiddleware::class, ]); })
Old versions of laravel register the middleware in app/Http/Kernel.php:
protected $routeMiddleware = [ // ... 'prometheus' => BushlanovDev\LaravelPrometheusExporter\Middleware\PrometheusLaravelMiddleware::class, ];
Register metrics route routes/web.php:
Route::get('metrics', [BushlanovDev\LaravelPrometheusExporter\Controllers\LaravelMetricsController::class, 'metrics']);
Add middleware to routes. It is advisable to give names to all routes:
Route::get('/', function () { // ... })->middleware('prometheus')->name('route_name');
Lumen
Register the service provider and middleware in bootstrap/app.php:
$app->register(BushlanovDev\LaravelPrometheusExporter\Providers\PrometheusServiceProvider::class);
$app->routeMiddleware([ 'prometheus' => BushlanovDev\LaravelPrometheusExporter\Middleware\PrometheusLumenMiddleware::class, ]);
Register metrics route:
$app->router->group(['namespace' => '\BushlanovDev\LaravelPrometheusExporter\Controllers'], function ($router) { $router->get('metrics', ['as' => 'metrics', 'uses'=> 'LumenMetricsController' . '@metrics']); });
Add middleware to routes. Give names to your routes — the route name is used as the metric label, and for unnamed routes the request path becomes the label:
$router->get('/', ['middleware' => 'prometheus', 'as' => 'route_name', function () use ($router) {/*...*/}]);
Guzzle metrics
To observe Guzzle metrics, you should register the following provider:
BushlanovDev\LaravelPrometheusExporter\Providers\GuzzleServiceProvider::class
And register the middleware for Http facade or Guzzle client:
Http::globalMiddleware(function ($handler) { return $this->app['prometheus.guzzle.middleware']($handler); }); $this->app->alias('prometheus.guzzle.client', Client::class);
Configuration
The package has a default configuration which uses the following environment variables.
PROMETHEUS_NAMESPACE=app
PROMETHEUS_STORAGE_ADAPTER=redis
PROMETHEUS_REDIS_HOST=localhost
PROMETHEUS_REDIS_PORT=6379
PROMETHEUS_REDIS_DATABASE=0
PROMETHEUS_REDIS_TIMEOUT=0.1
PROMETHEUS_REDIS_READ_TIMEOUT=10
PROMETHEUS_REDIS_PERSISTENT_CONNECTIONS=0
PROMETHEUS_REDIS_PREFIX=PROMETHEUS_
Boolean options such as PROMETHEUS_REDIS_PERSISTENT_CONNECTIONS and PROMETHEUS_ROUTE_AUTH_ENABLED
accept both true/false and 1/0.
To customize the configuration file, publish the package configuration using Artisan:
php artisan vendor:publish --provider="BushlanovDev\LaravelPrometheusExporter\Providers\PrometheusServiceProvider"
Collectors
Custom collectors listed in the configuration are registered automatically:
'collectors' => [ \App\Metrics\UsersCollector::class, ],
Each collector must implement BushlanovDev\LaravelPrometheusExporter\CollectorInterface.
Collectors are resolved through the service container, so constructor injection is available.
Protect metrics endpoint
If you need to prevent others from accessing your /metrics routes, you can enable the corresponding setting. Currently, only basic_auth is supported to secure your metrics endpoint.
PROMETHEUS_ROUTE_AUTH_ENABLED=true
PROMETHEUS_ROUTE_AUTH_USERNAME=username
PROMETHEUS_ROUTE_AUTH_PASSWORD=password
License
This repository's source code is available under the MIT License.