Skip to content

Commit 8926bab

Browse files
committed
feat: add support for rendering Infinity
Signed-off-by: Simon Podlipsky <simon@podlipsky.net>
1 parent ad2a85f commit 8926bab

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Prometheus\Storage\InMemory;
1313
use Prometheus\Storage\Redis;
1414
use ValueError;
15+
use const INF;
1516

1617
class RenderTextFormatTest extends TestCase
1718
{
@@ -37,6 +38,10 @@ private function buildSamples(): array
3738
->inc(['bob', 'al\ice']);
3839
$registry->getOrRegisterGauge($namespace, 'gauge', 'gauge-help-text', ['label1', 'label2'])
3940
->inc(["bo\nb", 'ali\"ce']);
41+
$registry->getOrRegisterGauge($namespace, 'gauge2', '', ['label1'])
42+
->set(INF, ["infinity"]);
43+
$registry->getOrRegisterGauge($namespace, 'gauge2', '', ['label1'])
44+
->set(-INF, ["infinity-negative"]);
4045
$registry->getOrRegisterHistogram($namespace, 'histogram', 'histogram-help-text', ['label1', 'label2'], [0, 10, 100])
4146
->observe(5, ['bob', 'alice']);
4247
$registry->getOrRegisterSummary($namespace, 'summary', 'summary-help-text', ['label1', 'label2'], 60, [0.1, 0.5, 0.9])
@@ -54,6 +59,10 @@ private function getExpectedOutput(): string
5459
# HELP mynamespace_gauge gauge-help-text
5560
# TYPE mynamespace_gauge gauge
5661
mynamespace_gauge{label1="bo\\nb",label2="ali\\\\\"ce"} 1
62+
# HELP mynamespace_gauge2
63+
# TYPE mynamespace_gauge2 gauge
64+
mynamespace_gauge2{label1="infinity"} +Inf
65+
mynamespace_gauge2{label1="infinity-negative"} -Inf
5766
# HELP mynamespace_histogram histogram-help-text
5867
# TYPE mynamespace_histogram histogram
5968
mynamespace_histogram_bucket{label1="bob",label2="alice",le="0"} 0

0 commit comments

Comments
 (0)