1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

//! Periodic sampling for logs, metrics, and other use cases through a simple macro

use std::{
    sync::atomic::{AtomicU64, Ordering},
    time::{Duration, SystemTime},
};

/// The rate at which a `sample!` macro will run it's given function
#[derive(Debug)]
pub enum SampleRate {
    /// Only sample a single time during a window of time. This rate only has a resolution in
    /// seconds.
    Duration(Duration),
    /// Sample based on the frequency of the event. The provided u64 is the inverse of the
    /// frequency (1/x), for example Frequency(2) means that 1 out of every 2 events will be
    /// sampled (1/2).
    Frequency(u64),
    /// Always Sample
    Always,
}

/// An internal struct that can be checked if a sample is ready for the `sample!` macro
pub struct Sampling {
    rate: SampleRate,
    state: AtomicU64,
}

impl Sampling {
    pub const fn new(rate: SampleRate) -> Self {
        Self {
            rate,
            state: AtomicU64::new(0),
        }
    }

    pub fn sample(&self) -> bool {
        match &self.rate {
            SampleRate::Duration(rate) => Self::sample_duration(rate, &self.state),
            SampleRate::Frequency(rate) => Self::sample_frequency(*rate, &self.state),
            SampleRate::Always => true,
        }
    }

    fn sample_frequency(rate: u64, count: &AtomicU64) -> bool {
        let previous_count = count
            .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |count| {
                let new_count = if count == 0 {
                    rate.saturating_sub(1)
                } else {
                    count.saturating_sub(1)
                };
                Some(new_count)
            })
            .expect("Closure should always returns 'Some'. This is a Bug.");

        previous_count == 0
    }

    fn sample_duration(rate: &Duration, last_sample: &AtomicU64) -> bool {
        let rate = rate.as_secs();
        // Seconds since Unix Epoch
        let now = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .expect("SystemTime before UNIX EPOCH!")
            .as_secs();

        last_sample
            .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |last_sample| {
                if now.saturating_sub(last_sample) >= rate {
                    Some(now)
                } else {
                    None
                }
            })
            .is_ok()
    }
}

/// Samples a given function at a `SampleRate`, useful for periodically emitting logs or metrics on
/// high throughput pieces of code.
#[macro_export]
macro_rules! sample {
    ($sample_rate:expr, $($args:expr)+ ,) => {
        $crate::sample!($sample_rate, $($args)+);
    };

    ($sample_rate:expr, $($args:tt)+) => {{
        static SAMPLING: Sampling = $crate::sample::Sampling::new($sample_rate);
        if SAMPLING.sample() {
            $($args)+
        }
    }};
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn frequency() {
        // Frequency
        let sampling = Sampling::new(SampleRate::Frequency(10));
        let mut v = Vec::new();
        for i in 0..=25 {
            if sampling.sample() {
                v.push(i);
            }
        }

        assert_eq!(v, vec![0, 10, 20]);
    }

    #[test]
    fn always() {
        // Always
        let sampling = Sampling::new(SampleRate::Always);
        let mut v = Vec::new();
        for i in 0..5 {
            if sampling.sample() {
                v.push(i);
            }
        }

        assert_eq!(v, vec![0, 1, 2, 3, 4]);
    }

    #[ignore]
    #[test]
    fn duration() {
        // Duration
        let sampling = Sampling::new(SampleRate::Duration(Duration::from_secs(1)));
        let mut v = Vec::new();
        for i in 0..5 {
            if sampling.sample() {
                v.push(i);
            }

            std::thread::sleep(Duration::from_millis(500));
        }

        assert_eq!(v.len(), 2);
    }

    #[test]
    fn macro_expansion() {
        for i in 0..10 {
            sample!(
                SampleRate::Frequency(2),
                println!("loooooooooooooooooooooooooong hello {}", i),
            );

            sample!(SampleRate::Frequency(2), {
                println!("hello {}", i);
            });

            sample!(SampleRate::Frequency(2), println!("hello {}", i));

            sample! {
                SampleRate::Frequency(2),

                for j in 10..20 {
                    println!("hello {}", j);
                }
            }
        }
    }

    #[test]
    fn threaded() {
        fn work() -> usize {
            let mut count = 0;

            for _ in 0..1000 {
                sample!(SampleRate::Frequency(5), count += 1);
            }

            count
        }

        let mut handles = Vec::new();
        for _ in 0..10 {
            handles.push(std::thread::spawn(work));
        }

        let mut count = 0;
        for handle in handles {
            count += handle.join().unwrap();
        }

        assert_eq!(count, 2000);
    }
}