Skip to content

Commit 3f915df

Browse files
authored
feat: add support for rendering Infinity (#144)
Signed-off-by: Simon Podlipsky <simon@podlipsky.net>
1 parent ad2a85f commit 3f915df

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/Prometheus/Sample.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Prometheus;
66

7+
use function is_infinite;
8+
79
class Sample
810
{
911
/**
@@ -67,6 +69,9 @@ public function getLabelValues(): array
6769
*/
6870
public function getValue(): string
6971
{
72+
if (is_float($this->value) && is_infinite($this->value)) {
73+
return $this->value > 0 ? '+Inf' : '-Inf';
74+
}
7075
return (string) $this->value;
7176
}
7277

tests/Test/Prometheus/RenderTextFormatTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Prometheus\Storage\Redis;
1414
use ValueError;
1515

16+
use const INF;
17+
1618
class RenderTextFormatTest extends TestCase
1719
{
1820
public function testOutputMatchesExpectations(): void
@@ -37,6 +39,10 @@ private function buildSamples(): array
3739
->inc(['bob', 'al\ice']);
3840
$registry->getOrRegisterGauge($namespace, 'gauge', 'gauge-help-text', ['label1', 'label2'])
3941
->inc(["bo\nb", 'ali\"ce']);
42+
$registry->getOrRegisterGauge($namespace, 'gauge2', '', ['label1'])
43+
->set(INF, ["infinity"]);
44+
$registry->getOrRegisterGauge($namespace, 'gauge2', '', ['label1'])
45+
->set(-INF, ["infinity-negative"]);
4046
$registry->getOrRegisterHistogram($namespace, 'histogram', 'histogram-help-text', ['label1', 'label2'], [0, 10, 100])
4147
->observe(5, ['bob', 'alice']);
4248
$registry->getOrRegisterSummary($namespace, 'summary', 'summary-help-text', ['label1', 'label2'], 60, [0.1, 0.5, 0.9])
@@ -54,6 +60,10 @@ private function getExpectedOutput(): string
5460
# HELP mynamespace_gauge gauge-help-text
5561
# TYPE mynamespace_gauge gauge
5662
mynamespace_gauge{label1="bo\\nb",label2="ali\\\\\"ce"} 1
63+
# HELP mynamespace_gauge2
64+
# TYPE mynamespace_gauge2 gauge
65+
mynamespace_gauge2{label1="infinity"} +Inf
66+
mynamespace_gauge2{label1="infinity-negative"} -Inf
5767
# HELP mynamespace_histogram histogram-help-text
5868
# TYPE mynamespace_histogram histogram
5969
mynamespace_histogram_bucket{label1="bob",label2="alice",le="0"} 0

0 commit comments

Comments
 (0)