From 7e70cebf9538d3994da0fdce2d42c6356c2f4ecf Mon Sep 17 00:00:00 2001 From: vlahanas Date: Fri, 24 Nov 2023 07:11:56 +0200 Subject: [PATCH] Include metric name in exception message (#131) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add metric name in the runtime exception message which is thrown when escapeAllLabels fails to combine labels. Signed-off-by: vlahanas Co-authored-by: Lukas Kämmerling --- src/Prometheus/RenderTextFormat.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Prometheus/RenderTextFormat.php b/src/Prometheus/RenderTextFormat.php index 98ee73ea..b160865c 100644 --- a/src/Prometheus/RenderTextFormat.php +++ b/src/Prometheus/RenderTextFormat.php @@ -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(); @@ -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) {