From a4d014a8141fd0b4adbeed2e1c5d1b71821e959b Mon Sep 17 00:00:00 2001 From: nyash-codex Date: Sun, 28 Sep 2025 01:39:12 +0900 Subject: [PATCH] debug: add NYASH_DEBUG_SAMPLE_EVERY to sample DebugHub events (dev-only, zero-cost when unset) --- src/debug/hub.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/debug/hub.rs b/src/debug/hub.rs index 8a15f978..9c816108 100644 --- a/src/debug/hub.rs +++ b/src/debug/hub.rs @@ -1,4 +1,7 @@ use std::io::Write; +use std::sync::atomic::{AtomicU64, Ordering}; + +static EMIT_COUNTER: AtomicU64 = AtomicU64::new(0); /// Minimal debug hub: JSONL event emitter (dev-only; default OFF). /// @@ -15,6 +18,15 @@ pub fn emit(cat: &str, kind: &str, fn_name: Option<&str>, region_id: Option<&str return; } } + // Optional sampling: emit every N events (default 1 = no sampling) + let sample_every = std::env::var("NYASH_DEBUG_SAMPLE_EVERY") + .ok() + .and_then(|s| s.parse::().ok()) + .unwrap_or(1); + if sample_every > 1 { + let n = EMIT_COUNTER.fetch_add(1, Ordering::Relaxed) + 1; + if n % sample_every != 0 { return; } + } let sink = match std::env::var("NYASH_DEBUG_SINK") { Ok(s) if !s.is_empty() => s, _ => return, @@ -33,4 +45,3 @@ pub fn emit(cat: &str, kind: &str, fn_name: Option<&str>, region_id: Option<&str let _ = writeln!(f, "{}", obj.to_string()); } } -