From a5368804319e3f35aa723f44d670f4253989a36d Mon Sep 17 00:00:00 2001 From: Pierre Riteau Date: Tue, 22 Feb 2022 12:13:37 +0100 Subject: [PATCH] Raise CollectError when Prometheus query returns an error As described in the Prometheus HTTP API documentation [1], the JSON response to a Prometheus query includes a status field, which was ignored by CloudKitty. If it is set to "error", raise a CollectError exception to log the error type and details. For example, a query with a metric name containing a dot will produce the following error: cloudkitty.collector.exceptions.CollectError: bad_data: invalid parameter "query": 1:25: parse error: unexpected character: '.' [1] https://prometheus.io/docs/prometheus/latest/querying/api/ Change-Id: I0c2892a39ec50163de251b38d34493db6a22c858 Story: 2009869 Task: 44564 --- cloudkitty/collector/prometheus.py | 5 +++++ releasenotes/notes/prometheus-error-8eab9f1793c2280c.yaml | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 releasenotes/notes/prometheus-error-8eab9f1793c2280c.yaml diff --git a/cloudkitty/collector/prometheus.py b/cloudkitty/collector/prometheus.py index 8217fef3..cc3adcb3 100644 --- a/cloudkitty/collector/prometheus.py +++ b/cloudkitty/collector/prometheus.py @@ -219,6 +219,11 @@ class PrometheusCollector(collector.BaseCollector): except PrometheusResponseError as e: raise CollectError(*e.args) + if res['status'] == 'error': + error_type = res['errorType'] + error_msg = res['error'] + raise CollectError("%s: %s" % (error_type, error_msg)) + # If the query returns an empty dataset, # return an empty list if not res['data']['result']: diff --git a/releasenotes/notes/prometheus-error-8eab9f1793c2280c.yaml b/releasenotes/notes/prometheus-error-8eab9f1793c2280c.yaml new file mode 100644 index 00000000..4f419092 --- /dev/null +++ b/releasenotes/notes/prometheus-error-8eab9f1793c2280c.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Raises a ``CollectError`` exception with error details when a Prometheus + query returns an error status.