From 177ab17069b0bd73b1aa01c2ac7bc91d81b2a301 Mon Sep 17 00:00:00 2001 From: Avi-Robusta <97387909+Avi-Robusta@users.noreply.github.com> Date: Thu, 25 Jul 2024 13:49:32 +0300 Subject: [PATCH] fixing cpu, mem query (#1511) * fixing query * returning none if its negative --- .../core/sinks/robusta/prometheus_discovery_utils.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/robusta/core/sinks/robusta/prometheus_discovery_utils.py b/src/robusta/core/sinks/robusta/prometheus_discovery_utils.py index 1d3c9930d..a0012d51a 100644 --- a/src/robusta/core/sinks/robusta/prometheus_discovery_utils.py +++ b/src/robusta/core/sinks/robusta/prometheus_discovery_utils.py @@ -40,12 +40,12 @@ def get_status(self) -> PrometheusHealthStatus: def get_cluster_avg_cpu(self) -> Optional[float]: cpu_query = os.getenv("OVERRIDE_CLUSTER_CPU_AVG_QUERY", - f'100 * sum(rate(node_cpu_seconds_total{{mode!="idle"}}[1h])) / sum(machine_cpu_cores{{}})') + f'100 * (sum(rate(node_cpu_seconds_total{{mode!="idle"}}[1h])) / sum(machine_cpu_cores{{}}))') return self._get_query_prometheus_value(query=cpu_query) def get_cluster_avg_memory(self) -> Optional[float]: memory_query = os.getenv("OVERRIDE_CLUSTER_MEM_AVG_QUERY", - f'100 * (1 - sum(avg_over_time(node_memory_MemAvailable_bytes{{}}[1h])) / sum(machine_memory_bytes{{}}))') + f'100 * (1 - (sum(avg_over_time(node_memory_MemAvailable_bytes{{}}[1h])) / sum(machine_memory_bytes{{}})))') return self._get_query_prometheus_value(query=memory_query) def _get_query_prometheus_value(self, query: str) -> Optional[float]: @@ -57,7 +57,8 @@ def _get_query_prometheus_value(self, query: str) -> Optional[float]: logging.error(f"PrometheusDiscoveryUtils failed to get prometheus results.") return value = query_result.vector_result[0].value.value - return 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.") return