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

WIP - Proof of concept how to include openmetric exemplars. #54

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions src/Prometheus/Histogram.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class Histogram extends Collector
*/
private $buckets;

/**
* @var array e.g [1.1 => Exemplar]
*/
protected $bucketExemplars;

/**
* @param Adapter $adapter
* @param string $namespace
Expand Down Expand Up @@ -129,10 +134,26 @@ public function observe(float $value, array $labels = []): void
'labelNames' => $this->getLabelNames(),
'labelValues' => $labels,
'buckets' => $this->buckets,
'bucketExemplars' => $this->bucketExemplars,
]
);
}

/**
* @see https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#exemplars-1
* @param float $value
* @param array $labels
* @param array $exemplarLabels e.g ['traceID' => 'my-trace-id, 'otherLabel' => 'value]
* @param int $timestamp e.g 1619827200
*/
public function observeWithExemplar(float $value, array $labels = [], array $exemplarLabels = [], int $timestamp = null): void
{
foreach ($exemplarLabels as $exemplarLabelKey => $exemplarLabelValue) {
self::assertValidLabel($exemplarLabelKey);
}
$this->observe($value, $labels);
}

/**
* @return string
*/
Expand Down
15 changes: 8 additions & 7 deletions tests/Test/Prometheus/RenderTextFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ private function buildSamples(): array
->inc(['bob', 'al\ice']);
$registry->getOrRegisterGauge($namespace, 'gauge', 'counter-help-text', ['label1', 'label2'])
->inc(["bo\nb", 'ali\"ce']);
$registry->getOrRegisterHistogram($namespace, 'histogram', 'counter-help-text', ['label1', 'label2'], [0, 10, 100])
->observe(5, ['bob', 'alice']);
$histogram = $registry->getOrRegisterHistogram($namespace, 'histogram', 'counter-help-text', ['label1', 'label2'], [0, 10, 100]);
$histogram->observe(5, ['bob', 'alice']);
$histogram->observeWithExemplar(1.337, ['bob', 'alice'], ['traceID' => 'my-trace-id'], 1619827200);

return $registry->getMetricFamilySamples();
}
Expand All @@ -54,11 +55,11 @@ private function getExpectedOutput(): string
# HELP mynamespace_histogram counter-help-text
# TYPE mynamespace_histogram histogram
mynamespace_histogram_bucket{label1="bob",label2="alice",le="0"} 0
mynamespace_histogram_bucket{label1="bob",label2="alice",le="10"} 1
mynamespace_histogram_bucket{label1="bob",label2="alice",le="100"} 1
mynamespace_histogram_bucket{label1="bob",label2="alice",le="+Inf"} 1
mynamespace_histogram_count{label1="bob",label2="alice"} 1
mynamespace_histogram_sum{label1="bob",label2="alice"} 5
mynamespace_histogram_bucket{label1="bob",label2="alice",le="10"} 2 # {trace_id="my-trace-id"} 1.337 1619827200
mynamespace_histogram_bucket{label1="bob",label2="alice",le="100"} 2
mynamespace_histogram_bucket{label1="bob",label2="alice",le="+Inf"} 2
mynamespace_histogram_count{label1="bob",label2="alice"} 2
mynamespace_histogram_sum{label1="bob",label2="alice"} 6.337

TEXTPLAIN;
}
Expand Down