|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import datetime |
| 4 | +import logging |
| 5 | +from typing import Dict, Any, List, Optional |
| 6 | + |
| 7 | +from krkn_lib.prometheus.krkn_prometheus import KrknPrometheus |
| 8 | + |
| 9 | + |
| 10 | +# ----------------------------------------------------------------------------- |
| 11 | +# SLO evaluation helpers (used by krkn.resiliency) |
| 12 | +# ----------------------------------------------------------------------------- |
| 13 | + |
| 14 | + |
| 15 | +def slo_passed(prometheus_result: List[Any]) -> Optional[bool]: |
| 16 | + if not prometheus_result: |
| 17 | + return None |
| 18 | + has_samples = False |
| 19 | + for series in prometheus_result: |
| 20 | + if "values" in series: |
| 21 | + has_samples = True |
| 22 | + for _ts, val in series["values"]: |
| 23 | + try: |
| 24 | + if float(val) > 0: |
| 25 | + return False |
| 26 | + except (TypeError, ValueError): |
| 27 | + continue |
| 28 | + elif "value" in series: |
| 29 | + has_samples = True |
| 30 | + try: |
| 31 | + return float(series["value"][1]) == 0 |
| 32 | + except (TypeError, ValueError): |
| 33 | + return False |
| 34 | + |
| 35 | + # If we reached here and never saw any samples, skip |
| 36 | + return None if not has_samples else True |
| 37 | + |
| 38 | + |
| 39 | +def evaluate_slos( |
| 40 | + prom_cli: KrknPrometheus, |
| 41 | + slo_list: List[Dict[str, Any]], |
| 42 | + start_time: datetime.datetime, |
| 43 | + end_time: datetime.datetime, |
| 44 | +) -> Dict[str, bool]: |
| 45 | + """Evaluate a list of SLO expressions against Prometheus. |
| 46 | +
|
| 47 | + Args: |
| 48 | + prom_cli: Configured Prometheus client. |
| 49 | + slo_list: List of dicts with keys ``name``, ``expr``. |
| 50 | + start_time: Start timestamp. |
| 51 | + end_time: End timestamp. |
| 52 | + granularity: Step in seconds for range queries. |
| 53 | + Returns: |
| 54 | + Mapping name -> bool indicating pass status. |
| 55 | + True means good we passed the SLO test otherwise failed the SLO |
| 56 | + """ |
| 57 | + results: Dict[str, bool] = {} |
| 58 | + logging.info("Evaluating %d SLOs over window %s – %s", len(slo_list), start_time, end_time) |
| 59 | + for slo in slo_list: |
| 60 | + expr = slo["expr"] |
| 61 | + name = slo["name"] |
| 62 | + try: |
| 63 | + response = prom_cli.process_prom_query_in_range( |
| 64 | + expr, |
| 65 | + start_time=start_time, |
| 66 | + end_time=end_time, |
| 67 | + ) |
| 68 | + |
| 69 | + passed = slo_passed(response) |
| 70 | + if passed is None: |
| 71 | + # Absence of data indicates the condition did not trigger; treat as pass. |
| 72 | + logging.debug("SLO '%s' query returned no data; assuming pass.", name) |
| 73 | + results[name] = True |
| 74 | + else: |
| 75 | + results[name] = passed |
| 76 | + except Exception as exc: |
| 77 | + logging.error("PromQL query failed for SLO '%s': %s", name, exc) |
| 78 | + results[name] = False |
| 79 | + return results |
0 commit comments