dk_protocol/metrics.rs
1//! In-process counters for the release-locks-at-submit feature.
2//!
3//! PR1 uses `AtomicU64` counters rather than pulling in a full metrics crate.
4//! The values are emitted through `tracing::info!` events with a standardized
5//! `metric` field so existing log-based tooling can aggregate them, and the
6//! counters themselves are readable from tests and any future Prometheus
7//! exporter we might bolt on.
8
9use std::sync::atomic::{AtomicU64, Ordering};
10
11/// Number of symbol locks released on `dk_submit` (flag-gated site).
12/// Default-on; stays at zero only while an operator has explicitly opted
13/// out via `DKOD_RELEASE_ON_SUBMIT=0`, strictly monotonic otherwise.
14static LOCKS_RELEASED_ON_SUBMIT_TOTAL: AtomicU64 = AtomicU64::new(0);
15
16/// Number of `dk_file_write` calls rejected by the STALE_OVERLAY pre-check.
17/// A non-zero value in the testbed is a signal to inspect the agent prompt
18/// / harness flow, not the engine — the check is a backstop, not a primary
19/// correctness mechanism.
20static STALE_OVERLAY_REJECTED_TOTAL: AtomicU64 = AtomicU64::new(0);
21
22/// Increment the "locks released on submit" counter and emit a structured
23/// tracing event so log-based aggregators can surface it.
24pub fn incr_locks_released_on_submit(count: u64) {
25 if count == 0 {
26 return;
27 }
28 LOCKS_RELEASED_ON_SUBMIT_TOTAL.fetch_add(count, Ordering::Relaxed);
29 tracing::info!(
30 metric = "dkod_engine_locks_released_on_submit_total",
31 increment = count,
32 "metrics counter"
33 );
34}
35
36/// Increment the "STALE_OVERLAY rejected" counter.
37pub fn incr_stale_overlay_rejected() {
38 STALE_OVERLAY_REJECTED_TOTAL.fetch_add(1, Ordering::Relaxed);
39 tracing::info!(
40 metric = "dkod_engine_stale_overlay_rejected_total",
41 increment = 1,
42 "metrics counter"
43 );
44}
45
46/// Snapshot of the "locks released on submit" counter (for tests + future
47/// scrape endpoints).
48pub fn locks_released_on_submit_total() -> u64 {
49 LOCKS_RELEASED_ON_SUBMIT_TOTAL.load(Ordering::Relaxed)
50}
51
52/// Snapshot of the "STALE_OVERLAY rejected" counter.
53pub fn stale_overlay_rejected_total() -> u64 {
54 STALE_OVERLAY_REJECTED_TOTAL.load(Ordering::Relaxed)
55}