From 7e70cebf9538d3994da0fdce2d42c6356c2f4ecf Mon Sep 17 00:00:00 2001 From: vlahanas Date: Fri, 24 Nov 2023 07:11:56 +0200 Subject: [PATCH 1/2] 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) { From bf674d5db57a0bad22c3e5f005f675938cbf2e79 Mon Sep 17 00:00:00 2001 From: daneiserman <32951606+daneiserman@users.noreply.github.com> Date: Fri, 24 Nov 2023 08:16:52 +0300 Subject: [PATCH 2/2] Feature/verbose exceptions (#134) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * verbose redis exceptions Signed-off-by: Даниил Эйзерман * composer + readme Signed-off-by: Даниил Эйзерман * rollback composer.json and README.md Signed-off-by: Даниил Эйзерман --------- Signed-off-by: Даниил Эйзерман Co-authored-by: Даниил Эйзерман Co-authored-by: Lukas Kämmerling --- src/Prometheus/Storage/Redis.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Prometheus/Storage/Redis.php b/src/Prometheus/Storage/Redis.php index 3071c8d1..122d11a3 100644 --- a/src/Prometheus/Storage/Redis.php +++ b/src/Prometheus/Storage/Redis.php @@ -226,10 +226,17 @@ private function connectToServer(): void $connection_successful = $this->redis->connect($this->options['host'], (int) $this->options['port'], (float) $this->options['timeout']); } if (!$connection_successful) { - throw new StorageException("Can't connect to Redis server", 0); + throw new StorageException( + sprintf("Can't connect to Redis server. %s", $this->redis->getLastError()), + 0 + ); } } catch (\RedisException $e) { - throw new StorageException("Can't connect to Redis server", 0, $e); + throw new StorageException( + sprintf("Can't connect to Redis server. %s", $e->getMessage()), + $e->getCode(), + $e + ); } }