Skip to content

Commit

Permalink
Include metric name in exception message (#131)
Browse files Browse the repository at this point in the history
Add metric name in the runtime exception message which is thrown when escapeAllLabels fails to combine labels.

Signed-off-by: vlahanas <v.lahanas@ferryhopper.com>
Co-authored-by: Lukas Kämmerling <lukas.kaemmerling@hetzner-cloud.de>
  • Loading branch information
vlahanas and LKaemmerling committed Nov 24, 2023
1 parent 99356e2 commit 7e70ceb
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Prometheus/RenderTextFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private function renderSample(MetricFamilySamples $metric, Sample $sample): stri
{
$labelNames = $metric->getLabelNames();
if ($metric->hasLabelNames() || $sample->hasLabelNames()) {
$escapedLabels = $this->escapeAllLabels($labelNames, $sample);
$escapedLabels = $this->escapeAllLabels($metric, $labelNames, $sample);
return $sample->getName() . '{' . implode(',', $escapedLabels) . '} ' . $sample->getValue();
}
return $sample->getName() . ' ' . $sample->getValue();
Expand All @@ -56,19 +56,20 @@ private function escapeLabelValue(string $v): string
}

/**
* @param MetricFamilySamples $metric
* @param string[] $labelNames
* @param Sample $sample
*
* @return string[]
*/
private function escapeAllLabels(array $labelNames, Sample $sample): array
private function escapeAllLabels(MetricFamilySamples $metric, array $labelNames, Sample $sample): array
{
$escapedLabels = [];

$labels = array_combine(array_merge($labelNames, $sample->getLabelNames()), $sample->getLabelValues());

if ($labels === false) {
throw new RuntimeException('Unable to combine labels.');
throw new RuntimeException('Unable to combine labels for metric named ' . $metric->getName());
}

foreach ($labels as $labelName => $labelValue) {
Expand Down

0 comments on commit 7e70ceb

Please sign in to comment.