Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

add Lumen support #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ This package is a wrapper bridging [jimdo/prometheus_client_php](https://github.
composer require superbalist/laravel-prometheus-exporter
```

## Laravel

Register the service provider in app.php
```php
'providers' => [
Expand All @@ -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

The package has a default configuration which uses the following environment variables.
Expand Down
53 changes: 9 additions & 44 deletions src/MetricsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,28 @@

namespace Superbalist\LaravelPrometheusExporter;

use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Routing\Controller;
use Prometheus\RenderTextFormat;

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
*
* The route path is configurable in the prometheus.metrics_route_path config var, or the
* PROMETHEUS_METRICS_ROUTE_PATH env var.
*
* @param PrometheusExporter $prometheusExporter
* @return \Symfony\Component\HttpFoundation\Response
*/
public function getMetrics()
public function getMetrics(PrometheusExporter $prometheusExporter)
{
$metrics = $this->prometheusExporter->export();

$renderer = new RenderTextFormat();
$result = $renderer->render($metrics);

return $this->responseFactory->make($result, 200, ['Content-Type' => RenderTextFormat::MIME_TYPE]);
return response()->make(
(new RenderTextFormat)->render(
$prometheusExporter->export()
),
200,
['Content-Type' => RenderTextFormat::MIME_TYPE]
);
}
}
20 changes: 13 additions & 7 deletions src/PrometheusServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@ class PrometheusServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->publishes([
__DIR__ . '/../config/prometheus.php' => config_path('prometheus.php'),
]);
if (function_exists('config_path')) {
$this->publishes([
__DIR__ . '/../config/prometheus.php' => config_path('prometheus.php'),
]);
}

if (config('prometheus.metrics_route_enabled')) {
$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) {
$collector = $this->app->make($class);
$exporter->registerCollector($collector);
$exporter->registerCollector(
$this->app->make($class)
);
}
}

Expand All @@ -48,7 +53,8 @@ public function register()
});

$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');
$configs = config('prometheus.storage_adapters');
$config = Arr::get($configs, $driver, []);
Expand Down
21 changes: 8 additions & 13 deletions src/routes.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
<?php

/** @var \Illuminate\Routing\Route $route */
$route = Route::get(
config('prometheus.metrics_route_path'),
\Superbalist\LaravelPrometheusExporter\MetricsController::class . '@getMetrics'
);
/** @var \Illuminate\Routing\Route|\Laravel\Lumen\Routing\Router $route */
$route = app('router');

if ($name = config('prometheus.metrics_route_name')) {
$route->name($name);
}
$params = array_filter([
'uses' => \Superbalist\LaravelPrometheusExporter\MetricsController::class . '@getMetrics',
'as' => config('prometheus.metrics_route_name'),
'middleware' => config('prometheus.metrics_route_middleware'),
]);

$middleware = config('prometheus.metrics_route_middleware');

if ($middleware) {
$route->middleware($middleware);
}
$route->get(config('prometheus.metrics_route_path'), $params);