2018-12-18 02:23:44 +01:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-12-18 02:23:44 +01:00
|
|
|
namespace Engelsystem\Test\Unit\Controllers\Metrics;
|
|
|
|
|
|
|
|
use Engelsystem\Controllers\Metrics\MetricsEngine;
|
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
|
|
|
|
|
|
|
class MetricsEngineTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Controllers\Metrics\MetricsEngine::escape
|
2018-12-18 02:23:44 +01:00
|
|
|
* @covers \Engelsystem\Controllers\Metrics\MetricsEngine::formatData
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Controllers\Metrics\MetricsEngine::formatValue
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\MetricsEngine::get
|
2018-12-18 02:23:44 +01:00
|
|
|
* @covers \Engelsystem\Controllers\Metrics\MetricsEngine::renderLabels
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\MetricsEngine::renderValue
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testGet(): void
|
2018-12-18 02:23:44 +01:00
|
|
|
{
|
|
|
|
$engine = new MetricsEngine();
|
|
|
|
|
2022-05-11 00:25:20 +02:00
|
|
|
$this->assertEquals("\n", $engine->get('/metrics'));
|
2018-12-18 02:23:44 +01:00
|
|
|
|
2022-05-11 00:25:20 +02:00
|
|
|
$this->assertEquals("engelsystem_users 13\n", $engine->get('/metrics', ['users' => 13]));
|
2018-12-18 02:23:44 +01:00
|
|
|
|
2022-05-11 00:25:20 +02:00
|
|
|
$this->assertEquals("engelsystem_bool_val 0\n", $engine->get('/metrics', ['bool_val' => false]));
|
2018-12-18 02:23:44 +01:00
|
|
|
|
2022-05-11 00:25:20 +02:00
|
|
|
$this->assertEquals('# Lorem \n Ipsum' . "\n", $engine->get('/metrics', ["Lorem \n Ipsum"]));
|
2018-12-18 02:23:44 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
2022-05-11 00:25:20 +02:00
|
|
|
'engelsystem_foo{lorem="ip\\\\sum"} \\"lorem\\n\\\\ipsum\\"' . "\n",
|
2018-12-18 02:23:44 +01:00
|
|
|
$engine->get('/metrics', [
|
2019-04-24 11:01:37 +02:00
|
|
|
'foo' => ['labels' => ['lorem' => 'ip\\sum'], 'value' => "\"lorem\n\\ipsum\""],
|
2018-12-18 02:23:44 +01:00
|
|
|
])
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2022-05-11 00:25:20 +02:00
|
|
|
'engelsystem_foo_count{bar="14"} 42' . "\n",
|
2019-04-24 11:01:37 +02:00
|
|
|
$engine->get('/metrics', ['foo_count' => ['labels' => ['bar' => 14], 'value' => 42]])
|
2018-12-18 02:23:44 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2022-05-11 00:25:20 +02:00
|
|
|
'engelsystem_lorem{test="123"} NaN' . "\n" . 'engelsystem_lorem{test="456"} 999.99' . "\n",
|
2018-12-18 02:23:44 +01:00
|
|
|
$engine->get('/metrics', [
|
|
|
|
'lorem' => [
|
|
|
|
['labels' => ['test' => 123], 'value' => 'NaN'],
|
|
|
|
['labels' => ['test' => 456], 'value' => 999.99],
|
|
|
|
],
|
|
|
|
])
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2022-05-11 00:25:20 +02:00
|
|
|
"# HELP engelsystem_test Some help\\n text\n# TYPE engelsystem_test counter\nengelsystem_test 99\n",
|
2018-12-18 02:23:44 +01:00
|
|
|
$engine->get('/metrics', ['test' => ['help' => "Some help\n text", 'type' => 'counter', 'value' => 99]])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-05 16:33:50 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\MetricsEngine::expandData
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\MetricsEngine::formatHistogram
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\MetricsEngine::get
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testGetHistogram(): void
|
2020-06-05 16:33:50 +02:00
|
|
|
{
|
|
|
|
$engine = new MetricsEngine();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
<<<'EOD'
|
|
|
|
# TYPE engelsystem_test_minimum_histogram histogram
|
|
|
|
engelsystem_test_minimum_histogram_bucket{le="3"} 4
|
|
|
|
engelsystem_test_minimum_histogram_bucket{le="+Inf"} 4
|
|
|
|
engelsystem_test_minimum_histogram_sum 1.337
|
|
|
|
engelsystem_test_minimum_histogram_count 4
|
2022-05-11 00:25:20 +02:00
|
|
|
|
2020-06-05 16:33:50 +02:00
|
|
|
EOD,
|
|
|
|
$engine->get('/metrics', [
|
|
|
|
'test_minimum_histogram' => [
|
|
|
|
'type' => 'histogram', [3 => 4, 'sum' => 1.337]
|
|
|
|
],
|
|
|
|
])
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
<<<'EOD'
|
|
|
|
# TYPE engelsystem_test_short_histogram histogram
|
|
|
|
engelsystem_test_short_histogram_bucket{le="0"} 0
|
|
|
|
engelsystem_test_short_histogram_bucket{le="60"} 10
|
|
|
|
engelsystem_test_short_histogram_bucket{le="120"} 19
|
|
|
|
engelsystem_test_short_histogram_bucket{le="+Inf"} 300
|
|
|
|
engelsystem_test_short_histogram_sum 123.456
|
|
|
|
engelsystem_test_short_histogram_count 300
|
2022-05-11 00:25:20 +02:00
|
|
|
|
2020-06-05 16:33:50 +02:00
|
|
|
EOD,
|
|
|
|
$engine->get('/metrics', [
|
|
|
|
'test_short_histogram' => [
|
|
|
|
'type' => 'histogram',
|
|
|
|
'value' => [120 => 19, '+Inf' => 300, 60 => 10, 0 => 0, 'sum' => 123.456]
|
|
|
|
],
|
|
|
|
])
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
<<<'EOD'
|
|
|
|
# TYPE engelsystem_test_multiple_histogram histogram
|
|
|
|
engelsystem_test_multiple_histogram_bucket{handler="foo",le="0.1"} 32
|
|
|
|
engelsystem_test_multiple_histogram_bucket{handler="foo",le="3"} 99
|
|
|
|
engelsystem_test_multiple_histogram_bucket{handler="foo",le="+Inf"} 99
|
|
|
|
engelsystem_test_multiple_histogram_sum{handler="foo"} 42
|
|
|
|
engelsystem_test_multiple_histogram_count{handler="foo"} 99
|
|
|
|
engelsystem_test_multiple_histogram_bucket{handler="bar",le="0.2"} 0
|
|
|
|
engelsystem_test_multiple_histogram_bucket{handler="bar",le="+Inf"} 3
|
|
|
|
engelsystem_test_multiple_histogram_sum{handler="bar"} 3
|
|
|
|
engelsystem_test_multiple_histogram_count{handler="bar"} 3
|
2022-05-11 00:25:20 +02:00
|
|
|
|
2020-06-05 16:33:50 +02:00
|
|
|
EOD,
|
|
|
|
$engine->get('/metrics', [
|
|
|
|
'test_multiple_histogram' => [
|
|
|
|
'type' => 'histogram',
|
|
|
|
['labels' => ['handler' => 'foo'], 'sum' => '42', 'value' => ['0.1' => 32, 3 => 99]],
|
|
|
|
['labels' => ['handler' => 'bar'], 'sum' => '3', 'value' => ['0.2' => 0, '+Inf' => 3]],
|
|
|
|
],
|
|
|
|
])
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
<<<'EOD'
|
|
|
|
# TYPE engelsystem_test_minimum_histogram histogram
|
|
|
|
engelsystem_test_minimum_histogram_bucket{le="+Inf"} NaN
|
|
|
|
engelsystem_test_minimum_histogram_sum NaN
|
|
|
|
engelsystem_test_minimum_histogram_count NaN
|
2022-05-11 00:25:20 +02:00
|
|
|
|
2020-06-05 16:33:50 +02:00
|
|
|
EOD,
|
|
|
|
$engine->get('/metrics', [
|
|
|
|
'test_minimum_histogram' => [
|
|
|
|
'type' => 'histogram', []
|
|
|
|
],
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-18 02:23:44 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\MetricsEngine::canRender
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testCanRender(): void
|
2018-12-18 02:23:44 +01:00
|
|
|
{
|
|
|
|
$engine = new MetricsEngine();
|
|
|
|
|
|
|
|
$this->assertFalse($engine->canRender('/'));
|
|
|
|
$this->assertFalse($engine->canRender('/metrics.foo'));
|
|
|
|
$this->assertTrue($engine->canRender('/metrics'));
|
|
|
|
}
|
2018-12-03 23:39:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Metrics\MetricsEngine::share
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testShare(): void
|
2018-12-03 23:39:50 +01:00
|
|
|
{
|
|
|
|
$engine = new MetricsEngine();
|
|
|
|
|
|
|
|
$engine->share('foo', 42);
|
2022-05-11 00:25:20 +02:00
|
|
|
// Just ignore it
|
|
|
|
$this->assertEquals("\n", $engine->get('/metrics'));
|
2018-12-03 23:39:50 +01:00
|
|
|
}
|
2018-12-18 02:23:44 +01:00
|
|
|
}
|