Skip to main content

candle_graph/instrument/
selector.rs

1//! Constant-cost capture selection for keeping instrumentation off the hot path.
2
3/// Select exactly one one-based workload invocation for profiling.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub struct CaptureSelector {
6    selected_invocation: u64,
7}
8
9impl CaptureSelector {
10    pub fn new(selected_invocation: u64) -> anyhow::Result<Self> {
11        anyhow::ensure!(
12            selected_invocation > 0,
13            "selected capture invocation must be one-based"
14        );
15        Ok(Self {
16            selected_invocation,
17        })
18    }
19
20    #[inline]
21    pub fn is_selected(self, invocation: u64) -> bool {
22        invocation == self.selected_invocation
23    }
24
25    pub fn selected_invocation(self) -> u64 {
26        self.selected_invocation
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn selects_only_the_configured_one_based_invocation() {
36        let selector = CaptureSelector::new(3).unwrap();
37        assert!(!selector.is_selected(2));
38        assert!(selector.is_selected(3));
39        assert!(!selector.is_selected(4));
40        assert!(CaptureSelector::new(0).is_err());
41    }
42}