Skip to main content

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/// Zero while `DKOD_RELEASE_ON_SUBMIT=0`; strictly monotonic while on.
13static LOCKS_RELEASED_ON_SUBMIT_TOTAL: AtomicU64 = AtomicU64::new(0);
14
15/// Number of `dk_file_write` calls rejected by the STALE_OVERLAY pre-check.
16/// A non-zero value in the testbed is a signal to inspect the agent prompt
17/// / harness flow, not the engine — the check is a backstop, not a primary
18/// correctness mechanism.
19static STALE_OVERLAY_REJECTED_TOTAL: AtomicU64 = AtomicU64::new(0);
20
21/// Increment the "locks released on submit" counter and emit a structured
22/// tracing event so log-based aggregators can surface it.
23pub fn incr_locks_released_on_submit(count: u64) {
24    if count == 0 {
25        return;
26    }
27    LOCKS_RELEASED_ON_SUBMIT_TOTAL.fetch_add(count, Ordering::Relaxed);
28    tracing::info!(
29        metric = "dkod_engine_locks_released_on_submit_total",
30        increment = count,
31        "metrics counter"
32    );
33}
34
35/// Increment the "STALE_OVERLAY rejected" counter.
36pub fn incr_stale_overlay_rejected() {
37    STALE_OVERLAY_REJECTED_TOTAL.fetch_add(1, Ordering::Relaxed);
38    tracing::info!(
39        metric = "dkod_engine_stale_overlay_rejected_total",
40        increment = 1,
41        "metrics counter"
42    );
43}
44
45/// Snapshot of the "locks released on submit" counter (for tests + future
46/// scrape endpoints).
47pub fn locks_released_on_submit_total() -> u64 {
48    LOCKS_RELEASED_ON_SUBMIT_TOTAL.load(Ordering::Relaxed)
49}
50
51/// Snapshot of the "STALE_OVERLAY rejected" counter.
52pub fn stale_overlay_rejected_total() -> u64 {
53    STALE_OVERLAY_REJECTED_TOTAL.load(Ordering::Relaxed)
54}