Update app.py
Browse files
app.py
CHANGED
|
@@ -134,18 +134,18 @@ def vacuum_db():
|
|
| 134 |
logger.error(f"Vacuum failed: {e}")
|
| 135 |
|
| 136 |
# ----------------------------------------------------------------------
|
| 137 |
-
# Prometheus metrics (
|
| 138 |
# ----------------------------------------------------------------------
|
| 139 |
if PROMETHEUS_AVAILABLE:
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
else:
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
|
| 150 |
# ----------------------------------------------------------------------
|
| 151 |
# Thread‑safe history (in‑memory + DB backup)
|
|
@@ -168,8 +168,8 @@ def update_dashboard_data(decision: dict, risk: float):
|
|
| 168 |
save_decision_to_db(decision, risk)
|
| 169 |
# Update Prometheus metrics
|
| 170 |
if PROMETHEUS_AVAILABLE:
|
| 171 |
-
|
| 172 |
-
|
| 173 |
|
| 174 |
def refresh_history_from_db():
|
| 175 |
"""Load recent history from database (called at startup)."""
|
|
@@ -182,7 +182,7 @@ def refresh_history_from_db():
|
|
| 182 |
decision_history.append((ts, dec, risk))
|
| 183 |
risk_history.append((ts, risk))
|
| 184 |
if PROMETHEUS_AVAILABLE:
|
| 185 |
-
|
| 186 |
|
| 187 |
# ----------------------------------------------------------------------
|
| 188 |
# Memory monitoring (daemon thread with graceful stop)
|
|
@@ -311,7 +311,7 @@ def handle_infra_with_governance(fault_type: str, context_window: int, session_s
|
|
| 311 |
}
|
| 312 |
# Record latency metric
|
| 313 |
if PROMETHEUS_AVAILABLE:
|
| 314 |
-
|
| 315 |
return output, session_state
|
| 316 |
except Exception as e:
|
| 317 |
logger.exception("Error in handle_infra_with_governance")
|
|
@@ -361,7 +361,7 @@ def run_hmc_mcmc(samples: int, warmup: int):
|
|
| 361 |
samples = max(500, min(10000, int(samples)))
|
| 362 |
warmup = max(100, min(2000, int(warmup)))
|
| 363 |
if PROMETHEUS_AVAILABLE:
|
| 364 |
-
|
| 365 |
|
| 366 |
# Generate data: 10 observations with mean 0.5, std 0.2
|
| 367 |
np.random.seed(42) # for reproducibility
|
|
|
|
| 134 |
logger.error(f"Vacuum failed: {e}")
|
| 135 |
|
| 136 |
# ----------------------------------------------------------------------
|
| 137 |
+
# Prometheus metrics (renamed to avoid conflict with Gradio components)
|
| 138 |
# ----------------------------------------------------------------------
|
| 139 |
if PROMETHEUS_AVAILABLE:
|
| 140 |
+
prom_decisions_total = Counter('arf_decisions_total', 'Total decisions made', ['action'])
|
| 141 |
+
prom_risk_gauge = Gauge('arf_current_risk', 'Current risk score')
|
| 142 |
+
prom_decision_latency = Histogram('arf_decision_latency_seconds', 'Time to evaluate intent')
|
| 143 |
+
prom_mcmc_runs = Counter('arf_mcmc_runs_total', 'Total MCMC runs')
|
| 144 |
else:
|
| 145 |
+
prom_decisions_total = None
|
| 146 |
+
prom_risk_gauge = None
|
| 147 |
+
prom_decision_latency = None
|
| 148 |
+
prom_mcmc_runs = None
|
| 149 |
|
| 150 |
# ----------------------------------------------------------------------
|
| 151 |
# Thread‑safe history (in‑memory + DB backup)
|
|
|
|
| 168 |
save_decision_to_db(decision, risk)
|
| 169 |
# Update Prometheus metrics
|
| 170 |
if PROMETHEUS_AVAILABLE:
|
| 171 |
+
prom_decisions_total.labels(action=decision.get("risk_level", "unknown")).inc()
|
| 172 |
+
prom_risk_gauge.set(risk)
|
| 173 |
|
| 174 |
def refresh_history_from_db():
|
| 175 |
"""Load recent history from database (called at startup)."""
|
|
|
|
| 182 |
decision_history.append((ts, dec, risk))
|
| 183 |
risk_history.append((ts, risk))
|
| 184 |
if PROMETHEUS_AVAILABLE:
|
| 185 |
+
prom_risk_gauge.set(risk) # update gauge with latest risk
|
| 186 |
|
| 187 |
# ----------------------------------------------------------------------
|
| 188 |
# Memory monitoring (daemon thread with graceful stop)
|
|
|
|
| 311 |
}
|
| 312 |
# Record latency metric
|
| 313 |
if PROMETHEUS_AVAILABLE:
|
| 314 |
+
prom_decision_latency.observe(time.time() - start_time)
|
| 315 |
return output, session_state
|
| 316 |
except Exception as e:
|
| 317 |
logger.exception("Error in handle_infra_with_governance")
|
|
|
|
| 361 |
samples = max(500, min(10000, int(samples)))
|
| 362 |
warmup = max(100, min(2000, int(warmup)))
|
| 363 |
if PROMETHEUS_AVAILABLE:
|
| 364 |
+
prom_mcmc_runs.inc() # record metric
|
| 365 |
|
| 366 |
# Generate data: 10 observations with mean 0.5, std 0.2
|
| 367 |
np.random.seed(42) # for reproducibility
|