adk_website/
observability.rs1use std::sync::Arc;
20
21use adk_core::{AdkError, Result};
22use adk_harness::website::SiteRuntime;
23use adk_kernel::PartitionId;
24use adk_kernel::charter::{
25 AutonomyStore, KpiReview, KpiSource, KpiTracker, SignalFilter, SignalStore,
26};
27use awp_types::AutonomyLevel;
28use chrono::{DateTime, Datelike, Utc};
29use serde::{Deserialize, Serialize};
30
31use crate::incident::{Incident, StatusRecord, StatusSink};
32use crate::wallet::WalletKpi;
33
34pub mod spans {
36 pub const ONBOARDING: &str = "website.onboarding";
38 pub const SITE_RELEASE: &str = adk_site::pipeline::RELEASE_SPAN;
40 pub const SITE_PROBE: &str = "site.probe";
42 pub const AWP_REQUEST: &str = "awp.request";
44 pub const AWP_CAPABILITY_CALL: &str = "awp.capability.call";
46 pub const KERNEL_SIGNAL: &str = "kernel.signal";
48 pub const JOB: &str = "website.job";
50}
51
52pub fn record_kpi(source: &str, kpi: &str, value: f64) {
54 tracing::info!(
55 website.kpi.source = source,
56 website.kpi = kpi,
57 histogram.website.kpi.value = value,
58 "website.kpi"
59 );
60}
61
62pub fn record_incident(
65 incident: &Incident,
66 drift_since: DateTime<Utc>,
67 restored_at: Option<DateTime<Utc>>,
68) {
69 let detect = (incident.opened_at - drift_since).num_milliseconds().max(0);
70 tracing::info!(website.incident = %incident.id, histogram.website.incident.time_to_detect_ms = detect, "website.incident.detected");
71 if let Some(restored) = restored_at {
72 let restore = (restored - incident.opened_at).num_milliseconds().max(0);
73 tracing::info!(website.incident = %incident.id, histogram.website.incident.time_to_restore_ms = restore, "website.incident.restored");
74 }
75}
76
77pub fn record_held_decision(tool: &str, latency: Option<chrono::Duration>) {
79 tracing::info!(
80 website.tool = tool,
81 monotonic_counter.website.decisions.held = 1u64,
82 "website.decision.held"
83 );
84 if let Some(latency) = latency {
85 tracing::info!(
86 website.tool = tool,
87 histogram.website.decisions.latency_ms = latency.num_milliseconds().max(0),
88 "website.decision.answered"
89 );
90 }
91}
92
93pub fn record_autonomy_change(job: &str, from: Option<AutonomyLevel>, to: AutonomyLevel) {
95 tracing::info!(website.job = job, from = ?from, to = ?to, monotonic_counter.website.autonomy.changes = 1u64, "website.autonomy.changed");
96}
97
98pub fn record_spend(category: &str, amount: f64) {
100 tracing::info!(
101 website.spend.category = category,
102 counter.website.spend = amount,
103 "website.spend"
104 );
105}
106
107#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
109#[serde(rename_all = "camelCase")]
110pub struct SiteView {
111 #[serde(default, skip_serializing_if = "Option::is_none")]
113 pub status: Option<StatusRecord>,
114 #[serde(default, skip_serializing_if = "Option::is_none")]
116 pub release: Option<u64>,
117 pub kpis: Vec<KpiReview>,
119 pub jobs: Vec<(String, AutonomyLevel)>,
121 pub open_signals: usize,
123 #[serde(default, skip_serializing_if = "Option::is_none")]
125 pub spend_this_month: Option<f64>,
126}
127
128pub struct SiteViewSources {
130 pub runtime: Arc<SiteRuntime>,
132 pub status: Arc<dyn StatusSink>,
134 pub tracker: KpiTracker,
136 pub autonomy: Arc<dyn AutonomyStore>,
138 pub signals: (Arc<dyn SignalStore>, PartitionId),
140 pub wallet: Option<Arc<WalletKpi>>,
142}
143
144impl SiteView {
145 pub async fn read(sources: &SiteViewSources, now: DateTime<Utc>) -> Result<Self> {
151 let store = |e: adk_kernel::KernelError| AdkError::config(e.to_string());
152 let site = sources.runtime.site().id;
153 let today = now.date_naive();
154 let monday =
155 today - chrono::Duration::days(i64::from(today.weekday().num_days_from_monday()));
156 let jobs = sources
157 .autonomy
158 .list(site)
159 .await
160 .map_err(store)?
161 .into_iter()
162 .map(|job| (job.job, job.level))
163 .collect();
164 let (signals, partition) = &sources.signals;
165 let open_signals = signals
166 .list(&SignalFilter::default().partition(partition.clone()))
167 .await
168 .map_err(store)?
169 .iter()
170 .filter(|record| record.open)
171 .count();
172 let spend_this_month = match &sources.wallet {
173 Some(wallet) => {
174 let start = today
175 .with_day(1)
176 .and_then(|day| day.and_hms_opt(0, 0, 0))
177 .map_or(now, |start| start.and_utc());
178 wallet.read(site, "spend", start, now).await.map_err(store)?
179 }
180 None => None,
181 };
182 Ok(Self {
183 status: sources.status.get(site).await?,
184 release: sources.runtime.pipeline().previous().await.map(|p| p.manifest.seq),
185 kpis: sources.tracker.review(monday).await.map_err(store)?,
186 jobs,
187 open_signals,
188 spend_this_month,
189 })
190 }
191}