Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-garcia-edo committed May 3, 2021
1 parent bca39eb commit d9c5cb7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
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
7 changes: 4 additions & 3 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,7 +55,7 @@ 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="10"} 1 # {trace_id="my-trace-id"} 1.337 1619827200
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
Expand Down

0 comments on commit d9c5cb7

Please sign in to comment.