metrics-lib 0.9.1

The fastest metrics library for Rust. Lock-free 0.6ns gauges, 18ns counters, timers, rate meters, async timing, adaptive sampling, and system health. Cross-platform with minimal dependencies.
Documentation
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! Adaptive sampling and backpressure mechanisms
//!
//! Provides automatic load shedding and sampling to maintain performance under pressure

use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};
use std::time::{Duration, Instant};

/// Adaptive sampling strategy
#[derive(Debug, Clone, Copy)]
pub enum SamplingStrategy {
    /// Fixed rate sampling (1 in N)
    ///
    /// # Fields
    /// - `rate`: The fixed sampling rate, where 1 in `rate` samples are taken.
    Fixed {
        /// The fixed sampling rate, where 1 in `rate` samples are taken.
        rate: u32,
    },
    /// Dynamic rate based on load
    ///
    /// # Fields
    /// - `min_rate`: The minimum sampling rate.
    /// - `max_rate`: The maximum sampling rate.
    /// - `target_throughput`: The target throughput to maintain.
    Dynamic {
        /// The minimum sampling rate.
        min_rate: u32,
        /// The maximum sampling rate.
        max_rate: u32,
        /// The target throughput to maintain.
        target_throughput: u64,
    },
    /// Time-based sampling
    ///
    /// # Fields
    /// - `min_interval`: The minimum interval between samples.
    TimeBased {
        /// Duration in nanoseconds
        min_interval: u64,
    },
}

/// Adaptive sampler for load shedding
pub struct AdaptiveSampler {
    strategy: SamplingStrategy,
    current_rate: AtomicU32,
    samples_taken: AtomicU64,
    samples_dropped: AtomicU64,
    last_adjustment: parking_lot::Mutex<Instant>,
    overloaded: AtomicBool,
}

impl AdaptiveSampler {
    /// Create new sampler with strategy
    pub fn new(strategy: SamplingStrategy) -> Self {
        let initial_rate = match strategy {
            SamplingStrategy::Fixed { rate } => rate,
            SamplingStrategy::Dynamic { min_rate, .. } => min_rate,
            SamplingStrategy::TimeBased { .. } => 1,
        };

        Self {
            strategy,
            current_rate: AtomicU32::new(initial_rate),
            samples_taken: AtomicU64::new(0),
            samples_dropped: AtomicU64::new(0),
            last_adjustment: parking_lot::Mutex::new(Instant::now()),
            overloaded: AtomicBool::new(false),
        }
    }

    /// Check if sample should be taken
    #[inline]
    pub fn should_sample(&self) -> bool {
        match self.strategy {
            SamplingStrategy::Fixed { .. } => self.should_sample_fixed(),
            SamplingStrategy::Dynamic { .. } => self.should_sample_dynamic(),
            SamplingStrategy::TimeBased { min_interval } => {
                self.should_sample_time_based(Duration::from_nanos(min_interval))
            }
        }
    }

    #[inline]
    fn should_sample_fixed(&self) -> bool {
        let rate = self.current_rate.load(Ordering::Relaxed);
        if rate == 1 {
            self.samples_taken.fetch_add(1, Ordering::Relaxed);
            return true;
        }

        // Fast thread-local random
        let should_sample = fastrand::u32(1..=rate) == 1;

        if should_sample {
            self.samples_taken.fetch_add(1, Ordering::Relaxed);
        } else {
            self.samples_dropped.fetch_add(1, Ordering::Relaxed);
        }

        should_sample
    }

    fn should_sample_dynamic(&self) -> bool {
        // Check if we need to adjust rate
        let mut last_adjustment = self.last_adjustment.lock();
        let now = Instant::now();

        if now.duration_since(*last_adjustment) > Duration::from_secs(1) {
            self.adjust_dynamic_rate();
            *last_adjustment = now;
        }
        drop(last_adjustment);

        self.should_sample_fixed()
    }

    fn should_sample_time_based(&self, min_interval: Duration) -> bool {
        thread_local! {
            static LAST_SAMPLE: std::cell::RefCell<Option<Instant>> = const { std::cell::RefCell::new(None) };
        }

        LAST_SAMPLE.with(|last| {
            let mut last = last.borrow_mut();
            let now = Instant::now();

            match *last {
                Some(last_time) if now.duration_since(last_time) < min_interval => {
                    self.samples_dropped.fetch_add(1, Ordering::Relaxed);
                    false
                }
                _ => {
                    *last = Some(now);
                    self.samples_taken.fetch_add(1, Ordering::Relaxed);
                    true
                }
            }
        })
    }

    fn adjust_dynamic_rate(&self) {
        if let SamplingStrategy::Dynamic {
            min_rate,
            max_rate,
            target_throughput,
        } = self.strategy
        {
            let taken = self.samples_taken.load(Ordering::Relaxed);
            let current_rate = self.current_rate.load(Ordering::Relaxed);

            let new_rate = if taken > target_throughput {
                // Increase sampling rate (sample less)
                (current_rate * 2).min(max_rate)
            } else if taken < target_throughput / 2 {
                // Decrease sampling rate (sample more)
                (current_rate / 2).max(min_rate)
            } else {
                current_rate
            };

            if new_rate != current_rate {
                self.current_rate.store(new_rate, Ordering::Relaxed);
                self.overloaded
                    .store(new_rate > min_rate * 2, Ordering::Relaxed);
            }

            // Reset counters
            self.samples_taken.store(0, Ordering::Relaxed);
            self.samples_dropped.store(0, Ordering::Relaxed);
        }
    }

    /// Get current sampling rate
    #[inline]
    pub fn current_rate(&self) -> u32 {
        self.current_rate.load(Ordering::Relaxed)
    }

    /// Check if system is overloaded
    #[inline]
    pub fn is_overloaded(&self) -> bool {
        self.overloaded.load(Ordering::Relaxed)
    }

    /// Get sampling statistics
    pub fn stats(&self) -> SamplingStats {
        SamplingStats {
            samples_taken: self.samples_taken.load(Ordering::Relaxed),
            samples_dropped: self.samples_dropped.load(Ordering::Relaxed),
            current_rate: self.current_rate.load(Ordering::Relaxed),
            is_overloaded: self.is_overloaded(),
        }
    }
}

/// Sampling statistics snapshot from an [`AdaptiveSampler`].
#[derive(Debug, Clone)]
pub struct SamplingStats {
    /// Number of samples accepted during the current measurement window.
    pub samples_taken: u64,
    /// Number of samples rejected (dropped) during the current measurement window.
    pub samples_dropped: u64,
    /// Current sampling divisor: 1-in-N events are accepted (`1` = accept all).
    pub current_rate: u32,
    /// `true` when the adaptive rate exceeds twice the configured minimum,
    /// indicating the system is under elevated load.
    pub is_overloaded: bool,
}

impl SamplingStats {
    /// Calculate sampling percentage
    pub fn sampling_percentage(&self) -> f64 {
        let total = self.samples_taken + self.samples_dropped;
        if total == 0 {
            100.0
        } else {
            (self.samples_taken as f64 / total as f64) * 100.0
        }
    }
}

/// Circuit breaker for metric recording
pub struct MetricCircuitBreaker {
    state: AtomicU32,
    failures: AtomicU64,
    successes: AtomicU64,
    last_state_change: parking_lot::Mutex<Instant>,
    config: CircuitBreakerConfig,
}

#[derive(Debug, Clone)]
pub struct CircuitBreakerConfig {
    pub failure_threshold: u64,
    pub success_threshold: u64,
    pub timeout: Duration,
    pub half_open_max_calls: u64,
}

impl Default for CircuitBreakerConfig {
    fn default() -> Self {
        Self {
            failure_threshold: 5,
            success_threshold: 3,
            timeout: Duration::from_secs(30),
            half_open_max_calls: 10,
        }
    }
}

#[repr(u32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CircuitState {
    Closed = 0,
    Open = 1,
    HalfOpen = 2,
}

impl MetricCircuitBreaker {
    /// Create new circuit breaker
    pub fn new(config: CircuitBreakerConfig) -> Self {
        Self {
            state: AtomicU32::new(CircuitState::Closed as u32),
            failures: AtomicU64::new(0),
            successes: AtomicU64::new(0),
            last_state_change: parking_lot::Mutex::new(Instant::now()),
            config,
        }
    }

    /// Check if operation is allowed
    #[inline]
    pub fn is_allowed(&self) -> bool {
        let state = self.get_state();

        match state {
            CircuitState::Closed => true,
            CircuitState::Open => {
                // Check if timeout has passed
                let last_change = *self.last_state_change.lock();
                if Instant::now().duration_since(last_change) > self.config.timeout {
                    self.transition_to(CircuitState::HalfOpen);
                    true
                } else {
                    false
                }
            }
            CircuitState::HalfOpen => {
                // Allow limited calls
                let calls =
                    self.successes.load(Ordering::Relaxed) + self.failures.load(Ordering::Relaxed);
                calls < self.config.half_open_max_calls
            }
        }
    }

    /// Record success
    #[inline]
    pub fn record_success(&self) {
        let state = self.get_state();

        match state {
            CircuitState::Closed => {
                self.failures.store(0, Ordering::Relaxed);
            }
            CircuitState::HalfOpen => {
                let successes = self.successes.fetch_add(1, Ordering::Relaxed) + 1;
                if successes >= self.config.success_threshold {
                    self.transition_to(CircuitState::Closed);
                }
            }
            CircuitState::Open => {} // Shouldn't happen
        }
    }

    /// Record failure
    #[inline]
    pub fn record_failure(&self) {
        let state = self.get_state();

        match state {
            CircuitState::Closed => {
                let failures = self.failures.fetch_add(1, Ordering::Relaxed) + 1;
                if failures >= self.config.failure_threshold {
                    self.transition_to(CircuitState::Open);
                }
            }
            CircuitState::HalfOpen => {
                self.transition_to(CircuitState::Open);
            }
            CircuitState::Open => {} // Already open
        }
    }

    #[inline]
    fn get_state(&self) -> CircuitState {
        match self.state.load(Ordering::Relaxed) {
            0 => CircuitState::Closed,
            1 => CircuitState::Open,
            2 => CircuitState::HalfOpen,
            _ => unreachable!(),
        }
    }

    fn transition_to(&self, new_state: CircuitState) {
        self.state.store(new_state as u32, Ordering::Relaxed);
        self.failures.store(0, Ordering::Relaxed);
        self.successes.store(0, Ordering::Relaxed);
        *self.last_state_change.lock() = Instant::now();
    }
}

/// Backpressure controller
pub struct BackpressureController {
    max_pending: usize,
    pending: AtomicU64,
    rejected: AtomicU64,
}

impl BackpressureController {
    /// Create new controller
    pub fn new(max_pending: usize) -> Self {
        Self {
            max_pending,
            pending: AtomicU64::new(0),
            rejected: AtomicU64::new(0),
        }
    }

    /// Try to acquire slot
    #[inline]
    pub fn try_acquire(&self) -> Option<BackpressureGuard<'_>> {
        let pending = self.pending.fetch_add(1, Ordering::Relaxed);

        if pending >= self.max_pending as u64 {
            self.pending.fetch_sub(1, Ordering::Relaxed);
            self.rejected.fetch_add(1, Ordering::Relaxed);
            None
        } else {
            Some(BackpressureGuard { controller: self })
        }
    }

    /// Get current pending count
    #[inline]
    pub fn pending_count(&self) -> u64 {
        self.pending.load(Ordering::Relaxed)
    }

    /// Get rejected count
    #[inline]
    pub fn rejected_count(&self) -> u64 {
        self.rejected.load(Ordering::Relaxed)
    }
}

/// RAII guard for backpressure
pub struct BackpressureGuard<'a> {
    controller: &'a BackpressureController,
}

impl<'a> Drop for BackpressureGuard<'a> {
    #[inline]
    fn drop(&mut self) {
        self.controller.pending.fetch_sub(1, Ordering::Relaxed);
    }
}

/// Internal pseudo-random number generator used by the adaptive sampler.
///
/// Uses the **Splitmix64** algorithm for good statistical quality without
/// relying on `DefaultHasher` (whose output is not guaranteed to be stable
/// across Rust versions or between compilations). Seeded per-thread from a
/// stack-address/timestamp mix so each thread gets an independent sequence.
///
/// This is **not** a cryptographic RNG — it is used solely for probabilistic
/// sampling decisions.
pub mod fastrand {
    /// Returns a pseudo-random `u32` uniformly distributed in `range` (inclusive).
    #[inline]
    pub fn u32(range: std::ops::RangeInclusive<u32>) -> u32 {
        let start = *range.start();
        let end = *range.end();
        if start == end {
            return start;
        }

        thread_local! {
            /// Per-thread Splitmix64 state. Seeded once from a stack-address /
            /// nanosecond-time mix without using `DefaultHasher`, whose output
            /// is not stable across Rust versions.
            static RNG: std::cell::Cell<u64> = std::cell::Cell::new({
                // Use the address of a stack variable as a low-cost unique seed
                // component; XOR with a nanosecond timestamp for additional entropy.
                let addr = &() as *const () as u64;
                let nanos = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .map(|d| d.subsec_nanos() as u64)
                    .unwrap_or(0xdeadbeef_cafebabe);
                // Ensure the seed is non-zero (required by xorshift-family generators).
                (addr ^ nanos.wrapping_mul(0x9e3779b97f4a7c15)) | 1
            });
        }

        RNG.with(|rng| {
            // Splitmix64 step — excellent statistical properties, no external deps.
            let mut z = rng.get().wrapping_add(0x9e3779b97f4a7c15);
            rng.set(z);
            z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb);
            z ^= z >> 31;
            // Use the high 32 bits for the range mapping to avoid modulo bias
            // on low bits (which have slightly weaker distribution in Splitmix64).
            start + ((z >> 32) as u32 % (end - start + 1))
        })
    }
}

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

    #[test]
    fn test_fixed_sampling() {
        let sampler = AdaptiveSampler::new(SamplingStrategy::Fixed { rate: 10 });

        let mut sampled = 0;
        for _ in 0..1000 {
            if sampler.should_sample() {
                sampled += 1;
            }
        }

        // Should be approximately 100 (10%)
        assert!(sampled > 50 && sampled < 150);
    }

    #[test]
    fn test_circuit_breaker() {
        let breaker = MetricCircuitBreaker::new(CircuitBreakerConfig {
            failure_threshold: 3,
            success_threshold: 2,
            timeout: Duration::from_millis(100),
            half_open_max_calls: 5,
        });

        // Initially closed
        assert!(breaker.is_allowed());

        // Record failures to open
        for _ in 0..3 {
            breaker.record_failure();
        }

        // Should be open
        assert!(!breaker.is_allowed());

        // Wait for timeout
        std::thread::sleep(Duration::from_millis(150));

        // Should transition to half-open
        assert!(breaker.is_allowed());

        // Record successes to close
        breaker.record_success();
        breaker.record_success();

        // Should be closed again
        assert!(breaker.is_allowed());
    }

    #[test]
    fn test_backpressure() {
        let controller = BackpressureController::new(5);

        let mut guards = Vec::new();

        // Acquire up to limit
        for _ in 0..5 {
            guards.push(controller.try_acquire().unwrap());
        }

        // Next should fail
        assert!(controller.try_acquire().is_none());
        assert_eq!(controller.rejected_count(), 1);

        // Drop one guard
        guards.pop();

        // Should be able to acquire again
        assert!(controller.try_acquire().is_some());
    }

    #[test]
    fn test_time_based_sampling_interval() {
        // Min interval 5ms
        let sampler = AdaptiveSampler::new(SamplingStrategy::TimeBased {
            min_interval: 5_000_000,
        });

        // First call should sample (no prior sample)
        assert!(sampler.should_sample());

        // Immediate second call should be dropped due to interval
        assert!(!sampler.should_sample());

        // After waiting for interval, sampling allowed again
        std::thread::sleep(Duration::from_millis(6));
        assert!(sampler.should_sample());
    }

    #[test]
    fn test_sampling_stats_percentage() {
        // When no samples have occurred yet, percentage is 100%
        let sampler_zero = AdaptiveSampler::new(SamplingStrategy::Fixed { rate: 10 });
        let stats_zero = sampler_zero.stats();
        assert_eq!(stats_zero.samples_taken, 0);
        assert_eq!(stats_zero.samples_dropped, 0);
        assert!((stats_zero.sampling_percentage() - 100.0).abs() < f64::EPSILON);

        // Use time-based strategy to deterministically create 1 taken and 1 dropped
        let sampler = AdaptiveSampler::new(SamplingStrategy::TimeBased {
            min_interval: 5_000_000,
        });

        assert!(sampler.should_sample()); // taken += 1
        assert!(!sampler.should_sample()); // dropped += 1 (interval not elapsed)

        let stats = sampler.stats();
        assert_eq!(stats.samples_taken, 1);
        assert_eq!(stats.samples_dropped, 1);
        assert!((stats.sampling_percentage() - 50.0).abs() < 0.0001);
    }
}