Merge 31159258cb
into a581a2b145
This commit is contained in:
commit
bad521bb25
4 changed files with 45 additions and 64 deletions
15
README.md
15
README.md
|
@ -17,6 +17,8 @@ This package is a wrapper bridging [jimdo/prometheus_client_php](https://github.
|
||||||
composer require superbalist/laravel-prometheus-exporter
|
composer require superbalist/laravel-prometheus-exporter
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Laravel
|
||||||
|
|
||||||
Register the service provider in app.php
|
Register the service provider in app.php
|
||||||
```php
|
```php
|
||||||
'providers' => [
|
'providers' => [
|
||||||
|
@ -33,6 +35,19 @@ Register the facade in app.php
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Lumen
|
||||||
|
Register the service provider in boostrap/app.php
|
||||||
|
```php
|
||||||
|
$app->register(Superbalist\LaravelPrometheusExporter\PrometheusServiceProvider::class);
|
||||||
|
```
|
||||||
|
|
||||||
|
If you want to register the facade, in boostrap/app, replece the line with `$app->withFacades();` with the following:
|
||||||
|
```php
|
||||||
|
$app->withFacades(true, [
|
||||||
|
Superbalist\LaravelPrometheusExporter\PrometheusFacade::class => 'Prometheus',
|
||||||
|
]);
|
||||||
|
```
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
The package has a default configuration which uses the following environment variables.
|
The package has a default configuration which uses the following environment variables.
|
||||||
|
|
|
@ -2,63 +2,28 @@
|
||||||
|
|
||||||
namespace Superbalist\LaravelPrometheusExporter;
|
namespace Superbalist\LaravelPrometheusExporter;
|
||||||
|
|
||||||
use Illuminate\Contracts\Routing\ResponseFactory;
|
|
||||||
use Illuminate\Routing\Controller;
|
use Illuminate\Routing\Controller;
|
||||||
use Prometheus\RenderTextFormat;
|
use Prometheus\RenderTextFormat;
|
||||||
|
|
||||||
class MetricsController extends Controller
|
class MetricsController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var ResponseFactory
|
|
||||||
*/
|
|
||||||
protected $responseFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var PrometheusExporter
|
|
||||||
*/
|
|
||||||
protected $prometheusExporter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ResponseFactory $responseFactory
|
|
||||||
* @param PrometheusExporter $prometheusExporter
|
|
||||||
*/
|
|
||||||
public function __construct(ResponseFactory $responseFactory, PrometheusExporter $prometheusExporter)
|
|
||||||
{
|
|
||||||
$this->responseFactory = $responseFactory;
|
|
||||||
$this->prometheusExporter = $prometheusExporter;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return ResponseFactory
|
|
||||||
*/
|
|
||||||
public function getResponseFactory()
|
|
||||||
{
|
|
||||||
return $this->responseFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return PrometheusExporter
|
|
||||||
*/
|
|
||||||
public function getPrometheusExporter()
|
|
||||||
{
|
|
||||||
return $this->prometheusExporter;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET /metrics
|
* GET /metrics
|
||||||
*
|
*
|
||||||
* The route path is configurable in the prometheus.metrics_route_path config var, or the
|
* The route path is configurable in the prometheus.metrics_route_path config var, or the
|
||||||
* PROMETHEUS_METRICS_ROUTE_PATH env var.
|
* PROMETHEUS_METRICS_ROUTE_PATH env var.
|
||||||
*
|
*
|
||||||
|
* @param PrometheusExporter $prometheusExporter
|
||||||
* @return \Symfony\Component\HttpFoundation\Response
|
* @return \Symfony\Component\HttpFoundation\Response
|
||||||
*/
|
*/
|
||||||
public function getMetrics()
|
public function getMetrics(PrometheusExporter $prometheusExporter)
|
||||||
{
|
{
|
||||||
$metrics = $this->prometheusExporter->export();
|
return response()->make(
|
||||||
|
(new RenderTextFormat)->render(
|
||||||
$renderer = new RenderTextFormat();
|
$prometheusExporter->export()
|
||||||
$result = $renderer->render($metrics);
|
),
|
||||||
|
200,
|
||||||
return $this->responseFactory->make($result, 200, ['Content-Type' => RenderTextFormat::MIME_TYPE]);
|
['Content-Type' => RenderTextFormat::MIME_TYPE]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,18 +14,23 @@ class PrometheusServiceProvider extends ServiceProvider
|
||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
|
if (function_exists('config_path')) {
|
||||||
$this->publishes([
|
$this->publishes([
|
||||||
__DIR__ . '/../config/prometheus.php' => config_path('prometheus.php'),
|
__DIR__ . '/../config/prometheus.php' => config_path('prometheus.php'),
|
||||||
]);
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
if (config('prometheus.metrics_route_enabled')) {
|
if (config('prometheus.metrics_route_enabled')) {
|
||||||
$this->loadRoutesFrom(__DIR__ . '/routes.php');
|
$this->loadRoutesFrom(__DIR__ . '/routes.php');
|
||||||
}
|
}
|
||||||
|
|
||||||
$exporter = $this->app->make(PrometheusExporter::class); /* @var PrometheusExporter $exporter */
|
/* @var PrometheusExporter $exporter */
|
||||||
|
$exporter = $this->app->make(PrometheusExporter::class);
|
||||||
|
|
||||||
foreach (config('prometheus.collectors') as $class) {
|
foreach (config('prometheus.collectors') as $class) {
|
||||||
$collector = $this->app->make($class);
|
$exporter->registerCollector(
|
||||||
$exporter->registerCollector($collector);
|
$this->app->make($class)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +53,8 @@ class PrometheusServiceProvider extends ServiceProvider
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->app->bind(Adapter::class, function ($app) {
|
$this->app->bind(Adapter::class, function ($app) {
|
||||||
$factory = $app['prometheus.storage_adapter_factory']; /** @var StorageAdapterFactory $factory */
|
$factory = $app['prometheus.storage_adapter_factory'];
|
||||||
|
/** @var StorageAdapterFactory $factory */
|
||||||
$driver = config('prometheus.storage_adapter');
|
$driver = config('prometheus.storage_adapter');
|
||||||
$configs = config('prometheus.storage_adapters');
|
$configs = config('prometheus.storage_adapters');
|
||||||
$config = Arr::get($configs, $driver, []);
|
$config = Arr::get($configs, $driver, []);
|
||||||
|
|
|
@ -1,17 +1,12 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/** @var \Illuminate\Routing\Route $route */
|
/** @var \Illuminate\Routing\Route|\Laravel\Lumen\Routing\Router $route */
|
||||||
$route = Route::get(
|
$route = app('router');
|
||||||
config('prometheus.metrics_route_path'),
|
|
||||||
\Superbalist\LaravelPrometheusExporter\MetricsController::class . '@getMetrics'
|
|
||||||
);
|
|
||||||
|
|
||||||
if ($name = config('prometheus.metrics_route_name')) {
|
$params = array_filter([
|
||||||
$route->name($name);
|
'uses' => \Superbalist\LaravelPrometheusExporter\MetricsController::class . '@getMetrics',
|
||||||
}
|
'as' => config('prometheus.metrics_route_name'),
|
||||||
|
'middleware' => config('prometheus.metrics_route_middleware'),
|
||||||
|
]);
|
||||||
|
|
||||||
$middleware = config('prometheus.metrics_route_middleware');
|
$route->get(config('prometheus.metrics_route_path'), $params);
|
||||||
|
|
||||||
if ($middleware) {
|
|
||||||
$route->middleware($middleware);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue