Skip to content

Commit

Permalink
feat: add support for rendering Infinity
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Podlipsky <[email protected]>
  • Loading branch information
simPod committed Jan 10, 2024
1 parent ad2a85f commit 4044837
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Prometheus/Sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Prometheus;

use function is_infinite;

class Sample
{
/**
Expand Down Expand Up @@ -67,6 +69,9 @@ public function getLabelValues(): array
*/
public function getValue(): string
{
if (is_infinite($this->value)) {
return $this->value > 0 ? '+Inf' : '-Inf';
}
return (string) $this->value;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Test/Prometheus/RenderTextFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Prometheus\Storage\InMemory;
use Prometheus\Storage\Redis;
use ValueError;
use const INF;

class RenderTextFormatTest extends TestCase
{
Expand All @@ -37,6 +38,10 @@ private function buildSamples(): array
->inc(['bob', 'al\ice']);
$registry->getOrRegisterGauge($namespace, 'gauge', 'gauge-help-text', ['label1', 'label2'])
->inc(["bo\nb", 'ali\"ce']);
$registry->getOrRegisterGauge($namespace, 'gauge2', '', ['label1'])
->set(INF, ["infinity"]);
$registry->getOrRegisterGauge($namespace, 'gauge2', '', ['label1'])
->set(-INF, ["infinity-negative"]);
$registry->getOrRegisterHistogram($namespace, 'histogram', 'histogram-help-text', ['label1', 'label2'], [0, 10, 100])
->observe(5, ['bob', 'alice']);
$registry->getOrRegisterSummary($namespace, 'summary', 'summary-help-text', ['label1', 'label2'], 60, [0.1, 0.5, 0.9])
Expand All @@ -54,6 +59,10 @@ private function getExpectedOutput(): string
# HELP mynamespace_gauge gauge-help-text
# TYPE mynamespace_gauge gauge
mynamespace_gauge{label1="bo\\nb",label2="ali\\\\\"ce"} 1
# HELP mynamespace_gauge2
# TYPE mynamespace_gauge2 gauge
mynamespace_gauge2{label1="infinity"} +Inf
mynamespace_gauge2{label1="infinity-negative"} -Inf
# HELP mynamespace_histogram histogram-help-text
# TYPE mynamespace_histogram histogram
mynamespace_histogram_bucket{label1="bob",label2="alice",le="0"} 0
Expand Down

0 comments on commit 4044837

Please sign in to comment.