-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathRenderTextFormatTest.php
More file actions
75 lines (64 loc) · 2.95 KB
/
RenderTextFormatTest.php
File metadata and controls
75 lines (64 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace Test\Prometheus;
use Prometheus\CollectorRegistry;
use Prometheus\Exception\MetricsRegistrationException;
use Prometheus\MetricFamilySamples;
use Prometheus\RenderTextFormat;
use PHPUnit\Framework\TestCase;
use Prometheus\Storage\InMemory;
class RenderTextFormatTest extends TestCase
{
public function testOutputMatchesExpectations(): void
{
$metrics = $this->buildSamples();
$renderer = new RenderTextFormat();
$output = $renderer->render($metrics);
self::assertSame($this->getExpectedOutput(), $output);
}
/**
* @return MetricFamilySamples[]
* @throws MetricsRegistrationException
*/
private function buildSamples(): array
{
$namespace = 'mynamespace';
$registry = new CollectorRegistry(new InMemory(), false);
$registry->getOrRegisterCounter($namespace, 'counter', 'counter-help-text', ['label1', 'label2'])
->inc(['bob', 'al\ice']);
$registry->getOrRegisterCounter($namespace, 'counter_with_timestamp', 'counter-with-timestamp-help-text', ['label1', 'label2'])
->inc(['bob', 'al\ice'], 1395066363000);
$registry->getOrRegisterGauge($namespace, 'gauge', 'gauge-help-text', ['label1', 'label2'])
->inc(["bo\nb", 'ali\"ce']);
$registry->getOrRegisterGauge($namespace, 'gauge_with_timestamp', 'gauge-with-timestamp-help-text', ['label1', 'label2'])
->inc(["bo\nb", 'ali\"ce'], 1395066363000);
$registry->getOrRegisterHistogram($namespace, 'histogram', 'histogram-help-text', ['label1', 'label2'], [0, 10, 100])
->observe(5, ['bob', 'alice']);
return $registry->getMetricFamilySamples();
}
private function getExpectedOutput(): string
{
return <<<TEXTPLAIN
# HELP mynamespace_counter counter-help-text
# TYPE mynamespace_counter counter
mynamespace_counter{label1="bob",label2="al\\\\ice"} 1
# HELP mynamespace_counter_with_timestamp counter-with-timestamp-help-text
# TYPE mynamespace_counter_with_timestamp counter
mynamespace_counter_with_timestamp{label1="bob",label2="al\\\\ice"} 1 1395066363000
# HELP mynamespace_gauge gauge-help-text
# TYPE mynamespace_gauge gauge
mynamespace_gauge{label1="bo\\nb",label2="ali\\\\\"ce"} 1
# HELP mynamespace_gauge_with_timestamp gauge-with-timestamp-help-text
# TYPE mynamespace_gauge_with_timestamp gauge
mynamespace_gauge_with_timestamp{label1="bo\\nb",label2="ali\\\\\"ce"} 1 1395066363000
# HELP mynamespace_histogram histogram-help-text
# TYPE mynamespace_histogram histogram
mynamespace_histogram_bucket{label1="bob",label2="alice",le="0"} 0
mynamespace_histogram_bucket{label1="bob",label2="alice",le="10"} 1
mynamespace_histogram_bucket{label1="bob",label2="alice",le="100"} 1
mynamespace_histogram_bucket{label1="bob",label2="alice",le="+Inf"} 1
mynamespace_histogram_count{label1="bob",label2="alice"} 1
mynamespace_histogram_sum{label1="bob",label2="alice"} 5
TEXTPLAIN;
}
}