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

Lumen support #17 #18

Closed
wants to merge 5 commits into from
Closed
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ composer require superbalist/laravel-prometheus-exporter
```

Register the service provider in app.php

#### Laravel
```php
'providers' => [
// ...
Expand All @@ -33,6 +35,11 @@ Register the facade in app.php
]
```

#### Lumen
```php
$app->register(Superbalist\LaravelPrometheusExporter\LumenPrometheusServiceProvider::class);
```

## Configuration

The package has a default configuration which uses the following environment variables.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
],
"require": {
"php": ">=5.6.0",
"illuminate/support": "^5.3 || ^6.0 || ^7.0",
"illuminate/routing": "^5.3 || ^6.0 || ^7.0",
"illuminate/support": "^5.3 || ^6.0 || ^7.0 || ^8.0",
"illuminate/routing": "^5.3 || ^6.0 || ^7.0 || ^8.0",
"jimdo/prometheus_client_php": "^0.9.0"
},
"autoload": {
Expand Down
25 changes: 25 additions & 0 deletions src/LumenPrometheusServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Superbalist\LaravelPrometheusExporter;

class LumenPrometheusServiceProvider extends PrometheusServiceProvider
{
/**
* Publish files.
*/
protected function publishFiles()
{
// do nothing
}

/**
* Load routes.
*/
protected function loadRoutes()
{
$this->app->router
->group(['namespace' => 'Superbalist\LaravelPrometheusExporter'], function ($router) {
require __DIR__ . '/lumen_routes.php';
});
}
}
21 changes: 3 additions & 18 deletions src/MetricsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,25 @@

namespace Superbalist\LaravelPrometheusExporter;

use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
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)
public function __construct(PrometheusExporter $prometheusExporter)
{
$this->responseFactory = $responseFactory;
$this->prometheusExporter = $prometheusExporter;
}

/**
* @return ResponseFactory
*/
public function getResponseFactory()
{
return $this->responseFactory;
}

/**
* @return PrometheusExporter
*/
Expand All @@ -59,6 +44,6 @@ public function getMetrics()
$renderer = new RenderTextFormat();
$result = $renderer->render($metrics);

return $this->responseFactory->make($result, 200, ['Content-Type' => RenderTextFormat::MIME_TYPE]);
return Response::create($result, 200, ['Content-Type' => RenderTextFormat::MIME_TYPE]);
}
}
25 changes: 21 additions & 4 deletions src/PrometheusServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ class PrometheusServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->publishes([
__DIR__ . '/../config/prometheus.php' => config_path('prometheus.php'),
]);
$this->publishFiles();
$this->loadRoutes();

if (config('prometheus.metrics_route_enabled')) {
$this->loadRoutesFrom(__DIR__ . '/routes.php');
$this->loadRoutes();
}

$exporter = $this->app->make(PrometheusExporter::class); /* @var PrometheusExporter $exporter */
Expand All @@ -29,6 +28,24 @@ public function boot()
}
}

/**
* Publish files.
*/
protected function publishFiles()
{
$this->publishes([
__DIR__ . '/../config/prometheus.php' => config_path('prometheus.php'),
]);
}

/**
* Load routes.
*/
protected function loadRoutes()
{
$this->loadRoutesFrom(__DIR__ . '/laravel_routes.php');
}

/**
* Register bindings in the container.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/laravel_routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

Route::get(config('prometheus.metrics_route_path'), [
'as' => config('prometheus.metrics_route_name'),
'middleware' => config('prometheus.metrics_route_middleware'),
'uses' => \Superbalist\LaravelPrometheusExporter\MetricsController::class . '@getMetrics',
]);
7 changes: 7 additions & 0 deletions src/lumen_routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$router->get(config('prometheus.metrics_route_path'), [
'as' => config('prometheus.metrics_route_name'),
'middleware' => config('prometheus.metrics_route_middleware'),
'uses' => 'MetricsController@getMetrics'
]);
17 changes: 0 additions & 17 deletions src/routes.php

This file was deleted.

25 changes: 5 additions & 20 deletions tests/MetricsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Tests;

use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
use Mockery;
use PHPUnit\Framework\TestCase;
use Prometheus\RenderTextFormat;
Expand All @@ -14,35 +12,22 @@ class MetricsControllerTest extends TestCase
{
public function testConstruct()
{
$responseFactory = Mockery::mock(ResponseFactory::class);
$exporter = Mockery::mock(PrometheusExporter::class);
$controller = new MetricsController($responseFactory, $exporter);
$this->assertSame($responseFactory, $controller->getResponseFactory());
$controller = new MetricsController($exporter);
$this->assertSame($exporter, $controller->getPrometheusExporter());
}

public function testGetMetrics()
{
$response = Mockery::mock(Response::class);

$responseFactory = Mockery::mock(ResponseFactory::class);
$responseFactory->shouldReceive('make')
->once()
->withArgs([
"\n",
200,
['Content-Type' => RenderTextFormat::MIME_TYPE],
])
->andReturn($response);

$exporter = Mockery::mock(PrometheusExporter::class);
$exporter->shouldReceive('export')
->once()
->andReturn([]);

$controller = new MetricsController($responseFactory, $exporter);
$controller = new MetricsController($exporter);

$r = $controller->getMetrics();
$this->assertSame($response, $r);
$response = $controller->getMetrics();
$this->assertSame(200, $response->getStatusCode());
$this->assertSame(RenderTextFormat::MIME_TYPE, $response->headers->get('Content-Type'));
}
}