Skip to content

Commit

Permalink
fixing cpu, mem query (#1511)
Browse files Browse the repository at this point in the history
* fixing query

* returning none if its negative
  • Loading branch information
Avi-Robusta authored Jul 25, 2024
1 parent 9d61a62 commit 177ab17
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/robusta/core/sinks/robusta/prometheus_discovery_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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
Expand Down

0 comments on commit 177ab17

Please sign in to comment.