Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for rendering Infinity #144

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_float($this->value) && is_infinite($this->value)) {
return $this->value > 0 ? '+Inf' : '-Inf';
}
return (string) $this->value;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Test/Prometheus/RenderTextFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Prometheus\Storage\Redis;
use ValueError;

use const INF;

class RenderTextFormatTest extends TestCase
{
public function testOutputMatchesExpectations(): void
Expand All @@ -37,6 +39,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 +60,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
Loading