From 621a000c8748c565d7b87309479ab17036430749 Mon Sep 17 00:00:00 2001 From: arik Date: Fri, 9 Aug 2024 19:03:39 +0300 Subject: [PATCH] fix bug when adding labels to metrics without label filters (#1524) * fix bug when adding labels to metrics without label filters * fix bug when adding labels to metrics without label filters --- .../core/playbooks/prometheus_enrichment_utils.py | 14 ++++++++++---- .../sinks/robusta/prometheus_discovery_utils.py | 8 ++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/robusta/core/playbooks/prometheus_enrichment_utils.py b/src/robusta/core/playbooks/prometheus_enrichment_utils.py index 5feb5e126..c07d7f833 100644 --- a/src/robusta/core/playbooks/prometheus_enrichment_utils.py +++ b/src/robusta/core/playbooks/prometheus_enrichment_utils.py @@ -1,5 +1,5 @@ -import logging import math +import re from collections import defaultdict, namedtuple from datetime import datetime, timedelta from string import Template @@ -25,6 +25,7 @@ ResourceKey = Tuple[ResourceChartResourceType, ResourceChartItemType] ChartLabelFactory = Callable[[int], str] ChartOptions = namedtuple("ChartOptions", ["query", "values_format"]) +BRACKETS_COMMA_PATTERN = r"\{\s*," class XAxisLine(BaseModel): @@ -87,8 +88,9 @@ def run_prometheus_query_range( prom = get_prometheus_connect(prometheus_params) params = {"timeout": PROMETHEUS_REQUEST_TIMEOUT_SECONDS} prom.check_prometheus_connection(params) - result = prom.safe_custom_query_range(query=promql_query, start_time=starts_at, end_time=ends_at, step=step, - params=params) + result = prom.safe_custom_query_range( + query=promql_query, start_time=starts_at, end_time=ends_at, step=step, params=params + ) return PrometheusQueryResult(data=result) @@ -378,7 +380,11 @@ def run_prometheus_query(prometheus_params: PrometheusParams, query: str) -> Pro def __add_additional_labels(query: str, prometheus_params: PrometheusParams) -> str: if not prometheus_params.prometheus_additional_labels or not prometheus_params.add_additional_labels: return query - return query.replace("}", __get_additional_labels_str(prometheus_params) + "}") + fixed_query = query.replace("}", __get_additional_labels_str(prometheus_params) + "}") + fixed_query = re.sub( + BRACKETS_COMMA_PATTERN, "{", fixed_query + ) # fix the case no labels in query, which results in " {, cluster="bla"} which is illegal + return fixed_query def __get_additional_labels_str(prometheus_params: PrometheusParams) -> str: diff --git a/src/robusta/core/sinks/robusta/prometheus_discovery_utils.py b/src/robusta/core/sinks/robusta/prometheus_discovery_utils.py index a0012d51a..03992e569 100644 --- a/src/robusta/core/sinks/robusta/prometheus_discovery_utils.py +++ b/src/robusta/core/sinks/robusta/prometheus_discovery_utils.py @@ -53,14 +53,14 @@ def _get_query_prometheus_value(self, query: str) -> Optional[float]: global_config = self.__global_config prometheus_params = PrometheusParams(**global_config) query_result = run_prometheus_query(prometheus_params=prometheus_params, query=query) - if query_result.result_type == "error" or query_result.vector_result is None: - logging.error(f"PrometheusDiscoveryUtils failed to get prometheus results.") + if query_result.result_type == "error" or not query_result.vector_result: + logging.error("PrometheusDiscoveryUtils failed to get prometheus results.") return value = query_result.vector_result[0].value.value - return_value = float('%.2f' % float(value)) + return_value = float("%.2f" % float(value)) return return_value if return_value >= 0 else None except: - logging.exception(f"PrometheusDiscoveryUtils failed to get prometheus results.") + logging.exception("PrometheusDiscoveryUtils failed to get prometheus results.") return def __run_checks(self):