|
| 1 | +//! RED-minimal metric set for st2, per the interview Q5 decision recorded in |
| 2 | +//! `docs/vrs/06-observability/open-questions.md`. |
| 3 | +//! |
| 4 | +//! Every label value comes from a bounded enum (`result`, `driver`, hook registry name + |
| 5 | +//! normalized event); identifiers never become metric labels — those live in span attributes. |
| 6 | +//! |
| 7 | +//! Zero-overhead no-op unless a real meter provider is installed by |
| 8 | +//! [`crate::telemetry::Telemetry::init`]: every record function checks [`enabled`] first and |
| 9 | +//! returns before touching any instrument or allocating a label string. With no provider |
| 10 | +//! installed, `opentelemetry::global` hands out a silent no-op meter anyway — this early-out |
| 11 | +//! just keeps the disabled case allocation-free. |
| 12 | +
|
| 13 | +use std::sync::LazyLock; |
| 14 | +use std::sync::atomic::{AtomicBool, Ordering}; |
| 15 | +use std::time::Duration; |
| 16 | + |
| 17 | +use opentelemetry::global; |
| 18 | +use opentelemetry::metrics::{Counter, Histogram, Meter}; |
| 19 | + |
| 20 | +static ENABLED: AtomicBool = AtomicBool::new(false); |
| 21 | + |
| 22 | +/// Whether a real meter provider is installed. False → recording is a free no-op. |
| 23 | +pub fn enabled() -> bool { |
| 24 | + ENABLED.load(Ordering::Relaxed) |
| 25 | +} |
| 26 | + |
| 27 | +/// Flip recording on when [`crate::telemetry::Telemetry::init`] installs the provider. |
| 28 | +pub fn set_enabled(enabled: bool) { |
| 29 | + ENABLED.store(enabled, Ordering::Relaxed); |
| 30 | +} |
| 31 | + |
| 32 | +static METER: LazyLock<Meter> = LazyLock::new(|| global::meter("st2")); |
| 33 | + |
| 34 | +static RECONCILE_PASSES: LazyLock<Counter<u64>> = LazyLock::new(|| { |
| 35 | + METER |
| 36 | + .u64_counter("reconcile_passes_total") |
| 37 | + .with_description("Reconcile passes by outcome") |
| 38 | + .with_unit("1") |
| 39 | + .build() |
| 40 | +}); |
| 41 | +static RECONCILE_PASS_DURATION: LazyLock<Histogram<f64>> = LazyLock::new(|| { |
| 42 | + METER |
| 43 | + .f64_histogram("reconcile_pass_duration_seconds") |
| 44 | + .with_description("Wall-clock duration of one reconcile pass") |
| 45 | + .with_unit("s") |
| 46 | + .build() |
| 47 | +}); |
| 48 | +static SESSION_START_DURATION: LazyLock<Histogram<f64>> = LazyLock::new(|| { |
| 49 | + METER |
| 50 | + .f64_histogram("session_start_duration_seconds") |
| 51 | + .with_description("Latency of one task session spawn") |
| 52 | + .with_unit("s") |
| 53 | + .build() |
| 54 | +}); |
| 55 | +static TASK_LAUNCHES: LazyLock<Counter<u64>> = LazyLock::new(|| { |
| 56 | + METER |
| 57 | + .u64_counter("task_launches_total") |
| 58 | + .with_description("Task sessions launched, by driver") |
| 59 | + .with_unit("1") |
| 60 | + .build() |
| 61 | +}); |
| 62 | +static TASK_REAPS: LazyLock<Counter<u64>> = LazyLock::new(|| { |
| 63 | + METER |
| 64 | + .u64_counter("task_reaps_total") |
| 65 | + .with_description("Dead sessions reaped for restart, by driver") |
| 66 | + .with_unit("1") |
| 67 | + .build() |
| 68 | +}); |
| 69 | +static HOOK_INVOCATIONS: LazyLock<Counter<u64>> = LazyLock::new(|| { |
| 70 | + METER |
| 71 | + .u64_counter("hook_invocations_total") |
| 72 | + .with_description("Lifecycle hook invocations applied in-process, by hook and event") |
| 73 | + .with_unit("1") |
| 74 | + .build() |
| 75 | +}); |
| 76 | +static MESSAGE_DELIVERIES: LazyLock<Counter<u64>> = LazyLock::new(|| { |
| 77 | + METER |
| 78 | + .u64_counter("message_deliveries_total") |
| 79 | + .with_description("Bus deliveries onto a recipient inbox, by outcome") |
| 80 | + .with_unit("1") |
| 81 | + .build() |
| 82 | +}); |
| 83 | +static CRASH_LOOPS: LazyLock<Counter<u64>> = LazyLock::new(|| { |
| 84 | + METER |
| 85 | + .u64_counter("crash_loops_total") |
| 86 | + .with_description("Tasks parked as crash-looping past their restart budget") |
| 87 | + .with_unit("1") |
| 88 | + .build() |
| 89 | +}); |
| 90 | + |
| 91 | +/// One reconcile pass finished. `failed` = the pass collected errors. |
| 92 | +pub fn record_reconcile_pass(duration: Duration, failed: bool) { |
| 93 | + if !enabled() { |
| 94 | + return; |
| 95 | + } |
| 96 | + RECONCILE_PASS_DURATION.record(duration.as_secs_f64(), &[]); |
| 97 | + RECONCILE_PASSES.add( |
| 98 | + 1, |
| 99 | + &[opentelemetry::KeyValue::new("result", if failed { "fail" } else { "pass" })], |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +/// One task session spawn succeeded. |
| 104 | +pub fn record_session_start(duration: Duration, driver: &'static str) { |
| 105 | + if !enabled() { |
| 106 | + return; |
| 107 | + } |
| 108 | + SESSION_START_DURATION.record(duration.as_secs_f64(), &[]); |
| 109 | + TASK_LAUNCHES.add(1, &[opentelemetry::KeyValue::new("driver", driver)]); |
| 110 | +} |
| 111 | + |
| 112 | +/// One dead session was reaped so its replacement can start. |
| 113 | +pub fn record_task_reap(driver: &'static str) { |
| 114 | + if !enabled() { |
| 115 | + return; |
| 116 | + } |
| 117 | + TASK_REAPS.add(1, &[opentelemetry::KeyValue::new("driver", driver)]); |
| 118 | +} |
| 119 | + |
| 120 | +/// One lifecycle-hook invocation reached its single in-process application point. |
| 121 | +/// Unknown event names collapse to `other` so the label stays bounded. |
| 122 | +pub fn record_hook_invocation(hook: &'static str, event: &str) { |
| 123 | + if !enabled() { |
| 124 | + return; |
| 125 | + } |
| 126 | + HOOK_INVOCATIONS.add( |
| 127 | + 1, |
| 128 | + &[ |
| 129 | + opentelemetry::KeyValue::new("hook", hook), |
| 130 | + opentelemetry::KeyValue::new("event", normalize_hook_event(event)), |
| 131 | + ], |
| 132 | + ); |
| 133 | +} |
| 134 | + |
| 135 | +/// One bus delivery attempt onto a recipient inbox finished. `failed` = the attempt errored. |
| 136 | +pub fn record_message_delivery(failed: bool) { |
| 137 | + if !enabled() { |
| 138 | + return; |
| 139 | + } |
| 140 | + MESSAGE_DELIVERIES.add( |
| 141 | + 1, |
| 142 | + &[opentelemetry::KeyValue::new("result", if failed { "fail" } else { "pass" })], |
| 143 | + ); |
| 144 | +} |
| 145 | + |
| 146 | +/// A task was parked as crash-looping past its restart budget. |
| 147 | +pub fn record_crash_loop() { |
| 148 | + if !enabled() { |
| 149 | + return; |
| 150 | + } |
| 151 | + CRASH_LOOPS.add(1, &[]); |
| 152 | +} |
| 153 | + |
| 154 | +/// The bounded Claude hook-event vocabulary st2 applies; anything else is `other`. |
| 155 | +fn normalize_hook_event(event: &str) -> &'static str { |
| 156 | + match event { |
| 157 | + "SessionStart" => "SessionStart", |
| 158 | + "UserPromptSubmit" => "UserPromptSubmit", |
| 159 | + "PreToolUse" => "PreToolUse", |
| 160 | + "PostToolUse" => "PostToolUse", |
| 161 | + "PermissionRequest" => "PermissionRequest", |
| 162 | + "Stop" => "Stop", |
| 163 | + "SubagentStop" => "SubagentStop", |
| 164 | + "PreCompact" => "PreCompact", |
| 165 | + "Notification" => "Notification", |
| 166 | + _ => "other", |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +#[cfg(test)] |
| 171 | +mod tests { |
| 172 | + use super::*; |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn disabled_by_default_and_recording_is_a_no_op() { |
| 176 | + // No meter provider installed in unit tests: enabled() is false and every record call |
| 177 | + // must return without panicking or touching instruments. |
| 178 | + assert!(!enabled()); |
| 179 | + set_enabled(false); |
| 180 | + record_reconcile_pass(Duration::from_millis(5), false); |
| 181 | + record_session_start(Duration::from_millis(5), "exec"); |
| 182 | + record_task_reap("codex"); |
| 183 | + record_hook_invocation("claude-observe", "SomethingUnheardOf"); |
| 184 | + record_message_delivery(true); |
| 185 | + record_crash_loop(); |
| 186 | + assert!(!enabled()); |
| 187 | + } |
| 188 | + |
| 189 | + #[test] |
| 190 | + fn unknown_hook_events_collapse_to_other() { |
| 191 | + assert_eq!(normalize_hook_event("SessionStart"), "SessionStart"); |
| 192 | + assert_eq!(normalize_hook_event("TotallyNewEvent"), "other"); |
| 193 | + } |
| 194 | +} |
0 commit comments