|
10 | 10 | use Prometheus\RenderTextFormat;
|
11 | 11 | use PHPUnit\Framework\TestCase;
|
12 | 12 | use Prometheus\Storage\InMemory;
|
| 13 | +use Prometheus\Storage\Redis; |
| 14 | +use ValueError; |
13 | 15 |
|
14 | 16 | class RenderTextFormatTest extends TestCase
|
15 | 17 | {
|
@@ -70,4 +72,57 @@ private function getExpectedOutput(): string
|
70 | 72 |
|
71 | 73 | TEXTPLAIN;
|
72 | 74 | }
|
| 75 | + |
| 76 | + public function testValueErrorThrownWithInvalidSamples(): void |
| 77 | + { |
| 78 | + $namespace = 'foo'; |
| 79 | + $counter = 'bar'; |
| 80 | + $storage = new Redis(['host' => REDIS_HOST]); |
| 81 | + $storage->wipeStorage(); |
| 82 | + |
| 83 | + $registry = new CollectorRegistry($storage, false); |
| 84 | + $registry->registerCounter($namespace, $counter, 'counter-help-text', ['label1', 'label2']) |
| 85 | + ->inc(['bob', 'alice']); |
| 86 | + |
| 87 | + // Reload the registry with an updated counter config |
| 88 | + $registry = new CollectorRegistry($storage, false); |
| 89 | + $registry->registerCounter($namespace, $counter, 'counter-help-text', ['label1', 'label2', 'label3']) |
| 90 | + ->inc(['bob', 'alice', 'eve']); |
| 91 | + |
| 92 | + $this->expectException(ValueError::class); |
| 93 | + |
| 94 | + $renderer = new RenderTextFormat(); |
| 95 | + $renderer->render($registry->getMetricFamilySamples()); |
| 96 | + } |
| 97 | + |
| 98 | + public function testOutputWithInvalidSamplesSkipped(): void |
| 99 | + { |
| 100 | + $namespace = 'foo'; |
| 101 | + $counter = 'bar'; |
| 102 | + $storage = new Redis(['host' => REDIS_HOST]); |
| 103 | + $storage->wipeStorage(); |
| 104 | + |
| 105 | + $registry = new CollectorRegistry($storage, false); |
| 106 | + $registry->registerCounter($namespace, $counter, 'counter-help-text', ['label1', 'label2']) |
| 107 | + ->inc(['bob', 'alice']); |
| 108 | + |
| 109 | + // Reload the registry with an updated counter config |
| 110 | + $registry = new CollectorRegistry($storage, false); |
| 111 | + $registry->registerCounter($namespace, $counter, 'counter-help-text', ['label1', 'label2', 'label3']) |
| 112 | + ->inc(['bob', 'alice', 'eve']); |
| 113 | + |
| 114 | + $expectedOutput = ' |
| 115 | +# HELP foo_bar counter-help-text |
| 116 | +# TYPE foo_bar counter |
| 117 | +foo_bar{label1="bob",label2="alice"} 1 |
| 118 | +# Error: array_combine(): Argument #1 ($keys) and argument #2 ($values) must have the same number of elements |
| 119 | +# Labels: ["label1","label2"] |
| 120 | +# Values: ["bob","alice","eve"] |
| 121 | +'; |
| 122 | + |
| 123 | + $renderer = new RenderTextFormat(); |
| 124 | + $output = $renderer->render($registry->getMetricFamilySamples(), true); |
| 125 | + |
| 126 | + self::assertSame(trim($expectedOutput), trim($output)); |
| 127 | + } |
73 | 128 | }
|
0 commit comments