Skip to content

Commit 91851b4

Browse files
hrodicrodrigo-garcia-edo
authored andcommitted
wip
1 parent bca39eb commit 91851b4

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/Prometheus/Histogram.php

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class Histogram extends Collector
1616
*/
1717
private $buckets;
1818

19+
/**
20+
* @var array e.g [1.1 => Exemplar]
21+
*/
22+
protected $bucketExemplars;
23+
1924
/**
2025
* @param Adapter $adapter
2126
* @param string $namespace
@@ -129,10 +134,26 @@ public function observe(float $value, array $labels = []): void
129134
'labelNames' => $this->getLabelNames(),
130135
'labelValues' => $labels,
131136
'buckets' => $this->buckets,
137+
'bucketExemplars' => $this->bucketExemplars,
132138
]
133139
);
134140
}
135141

142+
/**
143+
* @see https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#exemplars-1
144+
* @param float $value
145+
* @param array $labels
146+
* @param array $exemplarLabels e.g ['traceID' => 'my-trace-id, 'otherLabel' => 'value]
147+
* @param int $timestamp e.g 1619827200
148+
*/
149+
public function observeWithExemplar(float $value, array $labels = [], array $exemplarLabels = [], int $timestamp = null): void
150+
{
151+
foreach ($exemplarLabels as $exemplarLabelKey => $exemplarLabelValue) {
152+
self::assertValidLabel($exemplarLabelKey);
153+
}
154+
$this->observe($value, $labels);
155+
}
156+
136157
/**
137158
* @return string
138159
*/

tests/Test/Prometheus/RenderTextFormatTest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ private function buildSamples(): array
3636
->inc(['bob', 'al\ice']);
3737
$registry->getOrRegisterGauge($namespace, 'gauge', 'counter-help-text', ['label1', 'label2'])
3838
->inc(["bo\nb", 'ali\"ce']);
39-
$registry->getOrRegisterHistogram($namespace, 'histogram', 'counter-help-text', ['label1', 'label2'], [0, 10, 100])
40-
->observe(5, ['bob', 'alice']);
39+
$histogram = $registry->getOrRegisterHistogram($namespace, 'histogram', 'counter-help-text', ['label1', 'label2'], [0, 10, 100]);
40+
$histogram->observe(5, ['bob', 'alice']);
41+
$histogram->observeWithExemplar(1.337, ['bob', 'alice'], ['traceID' => 'my-trace-id'], 1619827200);
4142

4243
return $registry->getMetricFamilySamples();
4344
}
@@ -54,7 +55,7 @@ private function getExpectedOutput(): string
5455
# HELP mynamespace_histogram counter-help-text
5556
# TYPE mynamespace_histogram histogram
5657
mynamespace_histogram_bucket{label1="bob",label2="alice",le="0"} 0
57-
mynamespace_histogram_bucket{label1="bob",label2="alice",le="10"} 1
58+
mynamespace_histogram_bucket{label1="bob",label2="alice",le="10"} 1 # {trace_id="my-trace-id"} 1.337 1619827200
5859
mynamespace_histogram_bucket{label1="bob",label2="alice",le="100"} 1
5960
mynamespace_histogram_bucket{label1="bob",label2="alice",le="+Inf"} 1
6061
mynamespace_histogram_count{label1="bob",label2="alice"} 1

0 commit comments

Comments
 (0)