add unit tests
This commit is contained in:
parent
00cb369d0b
commit
9c2453f0e4
8 changed files with 508 additions and 22 deletions
|
@ -150,7 +150,7 @@ $counter = $exporter->getGauge('users_online_total');
|
|||
// create a histogram
|
||||
$histogram = $exporter->registerHistogram(
|
||||
'response_time_seconds',
|
||||
'The response time of a request',
|
||||
'The response time of a request.',
|
||||
[],
|
||||
[0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0]
|
||||
);
|
||||
|
@ -161,7 +161,7 @@ $histogram->obeserve(5.0);
|
|||
// create a histogram (with labels)
|
||||
$histogram = $exporter->registerHistogram(
|
||||
'response_time_seconds',
|
||||
'The response time of a request',
|
||||
'The response time of a request.',
|
||||
['request_type'],
|
||||
[0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0]
|
||||
);
|
||||
|
|
|
@ -2,24 +2,48 @@
|
|||
|
||||
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(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
|
||||
*
|
||||
|
@ -35,7 +59,6 @@ class MetricsController extends Controller
|
|||
$renderer = new RenderTextFormat();
|
||||
$result = $renderer->render($metrics);
|
||||
|
||||
return response($result)
|
||||
->header('Content-Type', RenderTextFormat::MIME_TYPE);
|
||||
return $this->responseFactory->make($result, 200, ['Content-Type' => RenderTextFormat::MIME_TYPE]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -160,7 +160,7 @@ class PrometheusExporter
|
|||
*/
|
||||
public function getGauge($name)
|
||||
{
|
||||
return $this->prometheus->getCounter($this->namespace, $name);
|
||||
return $this->prometheus->getGauge($this->namespace, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -42,8 +42,8 @@ class PrometheusServiceProvider extends ServiceProvider
|
|||
});
|
||||
$this->app->alias(PrometheusExporter::class, 'prometheus');
|
||||
|
||||
$this->app->bind('prometheus.storage_adapter_factory', function ($app) {
|
||||
return new StorageAdapterFactory($app);
|
||||
$this->app->bind('prometheus.storage_adapter_factory', function () {
|
||||
return new StorageAdapterFactory();
|
||||
});
|
||||
|
||||
$this->app->bind(Adapter::class, function ($app) {
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Superbalist\LaravelPrometheusExporter;
|
||||
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use InvalidArgumentException;
|
||||
use Prometheus\Storage\Adapter;
|
||||
use Prometheus\Storage\APC;
|
||||
|
@ -11,19 +10,6 @@ use Prometheus\Storage\Redis;
|
|||
|
||||
class StorageAdapterFactory
|
||||
{
|
||||
/**
|
||||
* @var Container
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* @param Container $container
|
||||
*/
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory a storage adapter.
|
||||
*
|
||||
|
|
48
tests/MetricsControllerTest.php
Normal file
48
tests/MetricsControllerTest.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Illuminate\Contracts\Routing\ResponseFactory;
|
||||
use Illuminate\Http\Response;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prometheus\RenderTextFormat;
|
||||
use Superbalist\LaravelPrometheusExporter\MetricsController;
|
||||
use Superbalist\LaravelPrometheusExporter\PrometheusExporter;
|
||||
|
||||
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());
|
||||
$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);
|
||||
|
||||
$r = $controller->getMetrics();
|
||||
$this->assertSame($response, $r);
|
||||
}
|
||||
}
|
386
tests/PrometheusExporterTest.php
Normal file
386
tests/PrometheusExporterTest.php
Normal file
|
@ -0,0 +1,386 @@
|
|||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prometheus\CollectorRegistry;
|
||||
use Prometheus\Counter;
|
||||
use Prometheus\Gauge;
|
||||
use Prometheus\Histogram;
|
||||
use Superbalist\LaravelPrometheusExporter\CollectorInterface;
|
||||
use Superbalist\LaravelPrometheusExporter\PrometheusExporter;
|
||||
|
||||
class PrometheusExporterTest extends TestCase
|
||||
{
|
||||
public function testConstruct()
|
||||
{
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
$this->assertEquals('app', $exporter->getNamespace());
|
||||
$this->assertSame($registry, $exporter->getPrometheus());
|
||||
}
|
||||
|
||||
public function testConstructWithCollectors()
|
||||
{
|
||||
$collector1 = Mockery::mock(CollectorInterface::class);
|
||||
$collector1->shouldReceive('getName')
|
||||
->once()
|
||||
->andReturn('users');
|
||||
$collector1->shouldReceive('registerMetrics')
|
||||
->once()
|
||||
->with(Mockery::type(PrometheusExporter::class));
|
||||
$collector2 = Mockery::mock(CollectorInterface::class);
|
||||
$collector2->shouldReceive('getName')
|
||||
->once()
|
||||
->andReturn('search_requests');
|
||||
$collector2->shouldReceive('registerMetrics')
|
||||
->once()
|
||||
->with(Mockery::type(PrometheusExporter::class));
|
||||
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$exporter = new PrometheusExporter('app', $registry, [$collector1, $collector2]);
|
||||
|
||||
$collectors = $exporter->getCollectors();
|
||||
$this->assertCount(2, $collectors);
|
||||
$this->assertArrayHasKey('users', $collectors);
|
||||
$this->assertArrayHasKey('search_requests', $collectors);
|
||||
$this->assertSame($collector1, $collectors['users']);
|
||||
$this->assertSame($collector2, $collectors['search_requests']);
|
||||
}
|
||||
|
||||
public function testRegisterCollector()
|
||||
{
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$this->assertEmpty($exporter->getCollectors());
|
||||
|
||||
$collector = Mockery::mock(CollectorInterface::class);
|
||||
$collector->shouldReceive('getName')
|
||||
->once()
|
||||
->andReturn('users');
|
||||
$collector->shouldReceive('registerMetrics')
|
||||
->once()
|
||||
->with($exporter);
|
||||
|
||||
$exporter->registerCollector($collector);
|
||||
|
||||
$collectors = $exporter->getCollectors();
|
||||
$this->assertCount(1, $collectors);
|
||||
$this->assertArrayHasKey('users', $collectors);
|
||||
$this->assertSame($collector, $collectors['users']);
|
||||
}
|
||||
|
||||
public function testRegisterCollectorWhenCollectorIsAlreadyRegistered()
|
||||
{
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$this->assertEmpty($exporter->getCollectors());
|
||||
|
||||
$collector = Mockery::mock(CollectorInterface::class);
|
||||
$collector->shouldReceive('getName')
|
||||
->once()
|
||||
->andReturn('users');
|
||||
$collector->shouldReceive('registerMetrics')
|
||||
->once()
|
||||
->with($exporter);
|
||||
|
||||
$exporter->registerCollector($collector);
|
||||
|
||||
$collectors = $exporter->getCollectors();
|
||||
$this->assertCount(1, $collectors);
|
||||
$this->assertArrayHasKey('users', $collectors);
|
||||
$this->assertSame($collector, $collectors['users']);
|
||||
|
||||
$exporter->registerCollector($collector);
|
||||
|
||||
$collectors = $exporter->getCollectors();
|
||||
$this->assertCount(1, $collectors);
|
||||
$this->assertArrayHasKey('users', $collectors);
|
||||
$this->assertSame($collector, $collectors['users']);
|
||||
}
|
||||
|
||||
public function testGetCollector()
|
||||
{
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$this->assertEmpty($exporter->getCollectors());
|
||||
|
||||
$collector = Mockery::mock(CollectorInterface::class);
|
||||
$collector->shouldReceive('getName')
|
||||
->once()
|
||||
->andReturn('users');
|
||||
$collector->shouldReceive('registerMetrics')
|
||||
->once()
|
||||
->with($exporter);
|
||||
|
||||
$exporter->registerCollector($collector);
|
||||
|
||||
$c = $exporter->getCollector('users');
|
||||
$this->assertSame($collector, $c);
|
||||
}
|
||||
|
||||
public function testGetCollectorWhenCollectorIsNotRegistered()
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('The collector "test" is not registered.');
|
||||
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$exporter->getCollector('test');
|
||||
}
|
||||
|
||||
public function testRegisterCounter()
|
||||
{
|
||||
$counter = Mockery::mock(Counter::class);
|
||||
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$registry->shouldReceive('registerCounter')
|
||||
->once()
|
||||
->withArgs([
|
||||
'app',
|
||||
'search_requests_total',
|
||||
'The total number of search requests.',
|
||||
['request_type'],
|
||||
])
|
||||
->andReturn($counter);
|
||||
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$c = $exporter->registerCounter(
|
||||
'search_requests_total',
|
||||
'The total number of search requests.',
|
||||
['request_type']
|
||||
);
|
||||
$this->assertSame($counter, $c);
|
||||
}
|
||||
|
||||
public function testGetCounter()
|
||||
{
|
||||
$counter = Mockery::mock(Counter::class);
|
||||
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$registry->shouldReceive('getCounter')
|
||||
->once()
|
||||
->withArgs([
|
||||
'app',
|
||||
'search_requests_total',
|
||||
])
|
||||
->andReturn($counter);
|
||||
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$c = $exporter->getCounter('search_requests_total');
|
||||
$this->assertSame($counter, $c);
|
||||
}
|
||||
|
||||
public function testGetOrRegisterCounter()
|
||||
{
|
||||
$counter = Mockery::mock(Counter::class);
|
||||
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$registry->shouldReceive('getOrRegisterCounter')
|
||||
->once()
|
||||
->withArgs([
|
||||
'app',
|
||||
'search_requests_total',
|
||||
'The total number of search requests.',
|
||||
['request_type'],
|
||||
])
|
||||
->andReturn($counter);
|
||||
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$c = $exporter->getOrRegisterCounter(
|
||||
'search_requests_total',
|
||||
'The total number of search requests.',
|
||||
['request_type']
|
||||
);
|
||||
$this->assertSame($counter, $c);
|
||||
}
|
||||
|
||||
public function testRegisterGauge()
|
||||
{
|
||||
$gauge = Mockery::mock(Gauge::class);
|
||||
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$registry->shouldReceive('registerGauge')
|
||||
->once()
|
||||
->withArgs([
|
||||
'app',
|
||||
'users_online_total',
|
||||
'The total number of users online.',
|
||||
['group'],
|
||||
])
|
||||
->andReturn($gauge);
|
||||
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$g = $exporter->registerGauge(
|
||||
'users_online_total',
|
||||
'The total number of users online.',
|
||||
['group']
|
||||
);
|
||||
$this->assertSame($gauge, $g);
|
||||
}
|
||||
|
||||
public function testGetGauge()
|
||||
{
|
||||
$gauge = Mockery::mock(Gauge::class);
|
||||
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$registry->shouldReceive('getGauge')
|
||||
->once()
|
||||
->withArgs([
|
||||
'app',
|
||||
'users_online_total',
|
||||
])
|
||||
->andReturn($gauge);
|
||||
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$g = $exporter->getGauge('users_online_total');
|
||||
$this->assertSame($gauge, $g);
|
||||
}
|
||||
|
||||
public function testGetOrRegisterGauge()
|
||||
{
|
||||
$gauge = Mockery::mock(Gauge::class);
|
||||
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$registry->shouldReceive('getOrRegisterGauge')
|
||||
->once()
|
||||
->withArgs([
|
||||
'app',
|
||||
'users_online_total',
|
||||
'The total number of users online.',
|
||||
['group'],
|
||||
])
|
||||
->andReturn($gauge);
|
||||
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$g = $exporter->getOrRegisterGauge(
|
||||
'users_online_total',
|
||||
'The total number of users online.',
|
||||
['group']
|
||||
);
|
||||
$this->assertSame($gauge, $g);
|
||||
}
|
||||
|
||||
public function testRegisterHistogram()
|
||||
{
|
||||
$histogram = Mockery::mock(Histogram::class);
|
||||
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$registry->shouldReceive('registerHistogram')
|
||||
->once()
|
||||
->withArgs([
|
||||
'app',
|
||||
'response_time_seconds',
|
||||
'The response time of a request.',
|
||||
['request_type'],
|
||||
[0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0],
|
||||
])
|
||||
->andReturn($histogram);
|
||||
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$h = $exporter->registerHistogram(
|
||||
'response_time_seconds',
|
||||
'The response time of a request.',
|
||||
['request_type'],
|
||||
[0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0]
|
||||
);
|
||||
$this->assertSame($histogram, $h);
|
||||
}
|
||||
|
||||
public function testGetHistogram()
|
||||
{
|
||||
$histogram = Mockery::mock(Histogram::class);
|
||||
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$registry->shouldReceive('getHistogram')
|
||||
->once()
|
||||
->withArgs([
|
||||
'app',
|
||||
'response_time_seconds',
|
||||
])
|
||||
->andReturn($histogram);
|
||||
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$h = $exporter->getHistogram('response_time_seconds');
|
||||
$this->assertSame($histogram, $h);
|
||||
}
|
||||
|
||||
public function testGetOrRegisterHistogram()
|
||||
{
|
||||
$histogram = Mockery::mock(Histogram::class);
|
||||
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$registry->shouldReceive('getOrRegisterHistogram')
|
||||
->once()
|
||||
->withArgs([
|
||||
'app',
|
||||
'response_time_seconds',
|
||||
'The response time of a request.',
|
||||
['request_type'],
|
||||
[0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0],
|
||||
])
|
||||
->andReturn($histogram);
|
||||
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$h = $exporter->getOrRegisterHistogram(
|
||||
'response_time_seconds',
|
||||
'The response time of a request.',
|
||||
['request_type'],
|
||||
[0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0]
|
||||
);
|
||||
$this->assertSame($histogram, $h);
|
||||
}
|
||||
|
||||
public function testExport()
|
||||
{
|
||||
$samples = ['meh'];
|
||||
|
||||
$registry = Mockery::mock(CollectorRegistry::class);
|
||||
$registry->shouldReceive('getMetricFamilySamples')
|
||||
->once()
|
||||
->andReturn($samples);
|
||||
|
||||
$exporter = new PrometheusExporter('app', $registry);
|
||||
|
||||
$collector1 = Mockery::mock(CollectorInterface::class);
|
||||
$collector1->shouldReceive('getName')
|
||||
->once()
|
||||
->andReturn('users');
|
||||
$collector1->shouldReceive('registerMetrics')
|
||||
->once()
|
||||
->with($exporter);
|
||||
$collector1->shouldReceive('collect')
|
||||
->once();
|
||||
|
||||
$exporter->registerCollector($collector1);
|
||||
|
||||
$collector2 = Mockery::mock(CollectorInterface::class);
|
||||
$collector2->shouldReceive('getName')
|
||||
->once()
|
||||
->andReturn('search_requests');
|
||||
$collector2->shouldReceive('registerMetrics')
|
||||
->once()
|
||||
->with($exporter);
|
||||
$collector2->shouldReceive('collect')
|
||||
->once();
|
||||
|
||||
$exporter->registerCollector($collector2);
|
||||
|
||||
$s = $exporter->export();
|
||||
$this->assertSame($samples, $s);
|
||||
}
|
||||
}
|
43
tests/StorageAdapterFactoryTest.php
Normal file
43
tests/StorageAdapterFactoryTest.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prometheus\Storage\APC;
|
||||
use Prometheus\Storage\InMemory;
|
||||
use Prometheus\Storage\Redis;
|
||||
use Superbalist\LaravelPrometheusExporter\StorageAdapterFactory;
|
||||
|
||||
class StorageAdapterFactoryTest extends TestCase
|
||||
{
|
||||
public function testMakeMemoryAdapter()
|
||||
{
|
||||
$factory = new StorageAdapterFactory();
|
||||
$adapter = $factory->make('memory');
|
||||
$this->assertInstanceOf(InMemory::class, $adapter);
|
||||
}
|
||||
|
||||
public function testMakeApcAdapter()
|
||||
{
|
||||
$factory = new StorageAdapterFactory();
|
||||
$adapter = $factory->make('apc');
|
||||
$this->assertInstanceOf(APC::class, $adapter);
|
||||
}
|
||||
|
||||
public function testMakeRedisAdapter()
|
||||
{
|
||||
$factory = new StorageAdapterFactory();
|
||||
$adapter = $factory->make('redis');
|
||||
$this->assertInstanceOf(Redis::class, $adapter);
|
||||
}
|
||||
|
||||
public function testMakeInvalidAdapter()
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('The driver [moo] is not supported.');
|
||||
|
||||
$factory = new StorageAdapterFactory();
|
||||
$factory->make('moo');
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue