tracing-throttle 0.4.2

High-performance log deduplication and rate limiting for the tracing ecosystem
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//! Suppression summaries and counters.
//!
//! This module tracks how many events have been suppressed and generates
//! periodic summaries for emission.

use crate::domain::{metadata::EventMetadata, signature::EventSignature};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::time::{Duration, Instant};

/// Thread-safe counter for tracking suppressed events.
///
/// Uses atomics for lock-free concurrent updates in high-throughput scenarios.
#[derive(Debug)]
pub struct SuppressionCounter {
    /// Total number of times this event was suppressed
    suppressed_count: AtomicUsize,
    /// Timestamp of first suppression (nanoseconds since epoch)
    first_suppressed_nanos: AtomicU64,
    /// Timestamp of last suppression (nanoseconds since epoch)
    last_suppressed_nanos: AtomicU64,
}

impl Clone for SuppressionCounter {
    fn clone(&self) -> Self {
        Self {
            suppressed_count: AtomicUsize::new(self.suppressed_count.load(Ordering::Relaxed)),
            first_suppressed_nanos: AtomicU64::new(
                self.first_suppressed_nanos.load(Ordering::Relaxed),
            ),
            last_suppressed_nanos: AtomicU64::new(
                self.last_suppressed_nanos.load(Ordering::Relaxed),
            ),
        }
    }
}

impl SuppressionCounter {
    /// Create a new counter (initially zero suppressions).
    pub fn new(initial_timestamp: Instant) -> Self {
        let nanos = Self::instant_to_nanos(initial_timestamp);
        Self {
            suppressed_count: AtomicUsize::new(0),
            first_suppressed_nanos: AtomicU64::new(nanos),
            last_suppressed_nanos: AtomicU64::new(nanos),
        }
    }

    /// Create a counter from a snapshot (for deserialization).
    ///
    /// This is used by storage backends like Redis to reconstruct state.
    #[cfg(feature = "redis-storage")]
    pub fn from_snapshot(
        suppressed_count: usize,
        first_suppressed: Instant,
        last_suppressed: Instant,
    ) -> Self {
        let first_nanos = Self::instant_to_nanos(first_suppressed);
        let last_nanos = Self::instant_to_nanos(last_suppressed);
        Self {
            suppressed_count: AtomicUsize::new(suppressed_count),
            first_suppressed_nanos: AtomicU64::new(first_nanos),
            last_suppressed_nanos: AtomicU64::new(last_nanos),
        }
    }

    /// Record a new suppression event.
    pub fn record_suppression(&self, timestamp: Instant) {
        // Use AcqRel for fetch_add to synchronize with other threads
        self.suppressed_count.fetch_add(1, Ordering::AcqRel);
        let nanos = Self::instant_to_nanos(timestamp);
        // Use Release to ensure timestamp update is visible
        self.last_suppressed_nanos.store(nanos, Ordering::Release);
    }

    /// Get the current suppression count.
    pub fn count(&self) -> usize {
        // Use Acquire to synchronize with Release/AcqRel operations
        self.suppressed_count.load(Ordering::Acquire)
    }

    /// Get the timestamp of the first suppression.
    pub fn first_suppressed(&self) -> Instant {
        // Use Acquire to synchronize with Release stores
        let nanos = self.first_suppressed_nanos.load(Ordering::Acquire);
        Self::nanos_to_instant(nanos)
    }

    /// Get the timestamp of the last suppression.
    pub fn last_suppressed(&self) -> Instant {
        // Use Acquire to synchronize with Release stores
        let nanos = self.last_suppressed_nanos.load(Ordering::Acquire);
        Self::nanos_to_instant(nanos)
    }

    /// Get a snapshot of the current state (for serialization).
    #[cfg(feature = "redis-storage")]
    pub fn snapshot(&self) -> super::summary::SuppressionSnapshot {
        super::summary::SuppressionSnapshot {
            suppressed_count: self.count(),
            first_suppressed: self.first_suppressed(),
            last_suppressed: self.last_suppressed(),
        }
    }

    /// Reset the counter for a new tracking period.
    ///
    /// # Thread Safety
    ///
    /// Note: This method updates multiple fields independently, so there is no
    /// guarantee that a concurrent reader will see all updates atomically. A reader
    /// could observe the count reset to 0 while timestamps still reflect old values,
    /// or vice versa. This is acceptable in practice since reset is typically called
    /// during initialization or between tracking periods when concurrent access is minimal.
    ///
    /// If you need atomic reset semantics, ensure no concurrent access during reset.
    pub fn reset(&self, timestamp: Instant) {
        let nanos = Self::instant_to_nanos(timestamp);
        // Use Release for visibility
        self.suppressed_count.store(0, Ordering::Release);
        self.first_suppressed_nanos.store(nanos, Ordering::Release);
        self.last_suppressed_nanos.store(nanos, Ordering::Release);
    }

    /// Get the shared base instant for timestamp calculations.
    ///
    /// This ensures instant_to_nanos and nanos_to_instant use the same reference point.
    fn base_instant() -> &'static Instant {
        static BASE: std::sync::OnceLock<Instant> = std::sync::OnceLock::new();
        BASE.get_or_init(Instant::now)
    }

    /// Convert Instant to nanoseconds for atomic storage.
    ///
    /// We store relative to a base instant to avoid overflow issues.
    ///
    /// # Overflow Handling
    ///
    /// If the duration exceeds u64::MAX nanoseconds (~584 years), it saturates
    /// at u64::MAX. This is handled gracefully in nanos_to_instant().
    fn instant_to_nanos(instant: Instant) -> u64 {
        let base = Self::base_instant();
        instant
            .saturating_duration_since(*base)
            .as_nanos()
            .min(u64::MAX as u128) as u64
    }

    /// Convert nanoseconds back to Instant.
    ///
    /// # Overflow Handling
    ///
    /// If adding the duration would overflow Instant (practically impossible - requires
    /// ~584 years of uptime), returns the base instant. This ensures timestamps never
    /// panic even in extreme edge cases.
    fn nanos_to_instant(nanos: u64) -> Instant {
        let base = Self::base_instant();
        base.checked_add(Duration::from_nanos(nanos))
            .unwrap_or(*base)
    }
}

/// A snapshot of suppression counter state (for serialization).
#[cfg(feature = "redis-storage")]
#[derive(Debug, Clone)]
pub struct SuppressionSnapshot {
    pub suppressed_count: usize,
    pub first_suppressed: Instant,
    pub last_suppressed: Instant,
}

/// A summary of suppressed events for a particular signature.
///
/// This is emitted periodically to inform about suppression activity.
#[derive(Debug, Clone)]
pub struct SuppressionSummary {
    /// The signature of the suppressed event
    pub signature: EventSignature,
    /// Number of times the event was suppressed
    pub count: usize,
    /// When the first suppression occurred
    pub first_suppressed: Instant,
    /// When the last suppression occurred
    pub last_suppressed: Instant,
    /// Duration of the suppression period
    pub duration: Duration,
    /// Metadata about the event (for human-readable display)
    pub metadata: Option<EventMetadata>,
}

impl SuppressionSummary {
    /// Create a summary from a counter.
    pub fn from_counter(signature: EventSignature, counter: &SuppressionCounter) -> Self {
        let first = counter.first_suppressed();
        let last = counter.last_suppressed();
        let duration = last.saturating_duration_since(first);

        Self {
            signature,
            count: counter.count(),
            first_suppressed: first,
            last_suppressed: last,
            duration,
            metadata: None,
        }
    }

    /// Create a summary from a counter with metadata.
    pub fn from_counter_with_metadata(
        signature: EventSignature,
        counter: &SuppressionCounter,
        metadata: Option<EventMetadata>,
    ) -> Self {
        let first = counter.first_suppressed();
        let last = counter.last_suppressed();
        let duration = last.saturating_duration_since(first);

        Self {
            signature,
            count: counter.count(),
            first_suppressed: first,
            last_suppressed: last,
            duration,
            metadata,
        }
    }

    /// Format the summary as a human-readable message.
    ///
    /// If metadata is available, includes event details.
    /// Otherwise, shows just the signature hash.
    pub fn format_message(&self) -> String {
        if let Some(ref metadata) = self.metadata {
            format!(
                "Suppressed {} times over {:.2}s: {}",
                self.count,
                self.duration.as_secs_f64(),
                metadata.format_brief()
            )
        } else {
            format!(
                "Event suppressed {} times over {:?} (signature: {})",
                self.count, self.duration, self.signature
            )
        }
    }

    /// Format the summary with detailed field information.
    pub fn format_detailed(&self) -> String {
        if let Some(ref metadata) = self.metadata {
            format!(
                "Suppressed {} times over {:.2}s: {}",
                self.count,
                self.duration.as_secs_f64(),
                metadata.format_detailed()
            )
        } else {
            self.format_message()
        }
    }
}

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

    #[test]
    fn test_suppression_counter_basic() {
        let now = Instant::now();
        let counter = SuppressionCounter::new(now);

        assert_eq!(counter.count(), 0);

        counter.record_suppression(now);
        assert_eq!(counter.count(), 1);

        counter.record_suppression(now);
        assert_eq!(counter.count(), 2);
    }

    #[test]
    fn test_suppression_counter_timestamps() {
        let start = Instant::now();
        let counter = SuppressionCounter::new(start);

        thread::sleep(Duration::from_millis(10));
        let later = Instant::now();
        counter.record_suppression(later);

        let first = counter.first_suppressed();
        let last = counter.last_suppressed();

        // First should be approximately start
        assert!(first.saturating_duration_since(start) < Duration::from_millis(5));

        // Last should be approximately later
        assert!(last.saturating_duration_since(later) < Duration::from_millis(5));
    }

    #[test]
    fn test_suppression_counter_reset() {
        let now = Instant::now();
        let counter = SuppressionCounter::new(now);

        counter.record_suppression(now);
        counter.record_suppression(now);
        assert_eq!(counter.count(), 2);

        counter.reset(now);
        assert_eq!(counter.count(), 0);
    }

    #[test]
    fn test_suppression_summary_creation() {
        let sig = EventSignature::simple("INFO", "Test message");
        let start = Instant::now();
        let counter = SuppressionCounter::new(start);

        thread::sleep(Duration::from_millis(10));
        counter.record_suppression(Instant::now());

        let summary = SuppressionSummary::from_counter(sig, &counter);

        assert_eq!(summary.signature, sig);
        assert_eq!(summary.count, 1);
        assert!(summary.duration >= Duration::from_millis(10));
    }

    #[test]
    fn test_suppression_summary_message() {
        let sig = EventSignature::simple("INFO", "Test");
        let now = Instant::now();
        let counter = SuppressionCounter::new(now);
        counter.record_suppression(now);

        let summary = SuppressionSummary::from_counter(sig, &counter);
        let message = summary.format_message();

        assert!(message.contains("suppressed 1 times"));
        assert!(message.contains(&sig.to_string()));
    }

    // Edge case tests
    #[test]
    fn test_very_large_suppression_count() {
        let now = Instant::now();
        let counter = SuppressionCounter::new(now);

        // Simulate a very large number of suppressions
        for _ in 0..10_000 {
            counter.record_suppression(now);
        }

        assert_eq!(counter.count(), 10_000);
    }

    #[test]
    fn test_counter_concurrent_updates() {
        use std::sync::Arc;
        use std::thread;

        let now = Instant::now();
        let counter = Arc::new(SuppressionCounter::new(now));
        let mut handles = vec![];

        // Spawn multiple threads updating counter
        for _ in 0..10 {
            let counter_clone = Arc::clone(&counter);
            let handle = thread::spawn(move || {
                for _ in 0..100 {
                    counter_clone.record_suppression(now);
                }
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }

        // 10 threads * 100 updates = 1000
        assert_eq!(counter.count(), 1000);
    }

    #[test]
    fn test_zero_duration_summary() {
        let sig = EventSignature::simple("INFO", "Test");
        let now = Instant::now();
        let counter = SuppressionCounter::new(now);

        // Immediately create summary (same timestamp)
        let summary = SuppressionSummary::from_counter(sig, &counter);

        assert_eq!(summary.count, 0);
        assert!(summary.duration < Duration::from_millis(1));
    }

    #[test]
    fn test_reset_multiple_times() {
        let now = Instant::now();
        let counter = SuppressionCounter::new(now);

        counter.record_suppression(now);
        assert_eq!(counter.count(), 1);

        counter.reset(now);
        assert_eq!(counter.count(), 0);

        counter.record_suppression(now);
        assert_eq!(counter.count(), 1);

        counter.reset(now);
        assert_eq!(counter.count(), 0);
    }

    // === Edge Case and Overflow Tests ===

    #[test]
    fn test_clone_preserves_state() {
        let now = Instant::now();
        let counter = SuppressionCounter::new(now);

        counter.record_suppression(now);
        counter.record_suppression(now);

        let cloned = counter.clone();

        assert_eq!(counter.count(), cloned.count());
        assert_eq!(counter.first_suppressed(), cloned.first_suppressed());
        assert_eq!(counter.last_suppressed(), cloned.last_suppressed());
    }

    #[test]
    fn test_clone_independence() {
        let now = Instant::now();
        let counter1 = SuppressionCounter::new(now);
        let counter2 = counter1.clone();

        // Modify counter1
        counter1.record_suppression(now);

        // counter2 should not be affected
        assert_eq!(counter1.count(), 1);
        assert_eq!(counter2.count(), 0);
    }

    #[test]
    fn test_concurrent_clone_and_update() {
        use std::sync::Arc;
        use std::thread;

        let now = Instant::now();
        let counter = Arc::new(SuppressionCounter::new(now));

        let mut handles = vec![];

        // Thread 1: Updates counter
        let counter_clone1 = Arc::clone(&counter);
        handles.push(thread::spawn(move || {
            for _ in 0..100 {
                counter_clone1.record_suppression(now);
            }
        }));

        // Thread 2: Clones counter repeatedly
        let counter_clone2 = Arc::clone(&counter);
        handles.push(thread::spawn(move || {
            for _ in 0..100 {
                let _cloned = (*counter_clone2).clone();
            }
        }));

        for handle in handles {
            handle.join().unwrap();
        }

        // Should have at least some updates
        assert!(counter.count() > 1);
    }

    #[test]
    fn test_concurrent_reset_and_read() {
        use std::sync::Arc;
        use std::thread;

        let now = Instant::now();
        let counter = Arc::new(SuppressionCounter::new(now));

        let mut handles = vec![];

        // Thread 1: Repeatedly resets
        let counter_clone1 = Arc::clone(&counter);
        handles.push(thread::spawn(move || {
            for _ in 0..50 {
                counter_clone1.reset(now);
                thread::sleep(Duration::from_micros(10));
            }
        }));

        // Thread 2: Repeatedly records
        let counter_clone2 = Arc::clone(&counter);
        handles.push(thread::spawn(move || {
            for _ in 0..50 {
                counter_clone2.record_suppression(now);
                thread::sleep(Duration::from_micros(10));
            }
        }));

        // Thread 3: Repeatedly reads
        let counter_clone3 = Arc::clone(&counter);
        handles.push(thread::spawn(move || {
            for _ in 0..50 {
                let _count = counter_clone3.count();
                let _first = counter_clone3.first_suppressed();
                let _last = counter_clone3.last_suppressed();
                thread::sleep(Duration::from_micros(10));
            }
        }));

        for handle in handles {
            handle.join().unwrap();
        }

        // No assertions on final state due to race conditions,
        // but test should not panic or produce invalid data
    }

    #[test]
    fn test_very_large_suppression_count_stress() {
        let now = Instant::now();
        let counter = SuppressionCounter::new(now);

        // Simulate a huge number of suppressions
        for _ in 0..100_000 {
            counter.record_suppression(now);
        }

        assert_eq!(counter.count(), 100_000);
    }

    #[test]
    fn test_timestamp_persistence_over_time() {
        let start = Instant::now();
        let counter = SuppressionCounter::new(start);

        // Record suppressions over time
        for i in 1..=10 {
            let timestamp = start + Duration::from_millis(i * 100);
            counter.record_suppression(timestamp);
        }

        // First timestamp should be preserved
        let first = counter.first_suppressed();
        let duration_from_start = first.duration_since(start);
        assert!(duration_from_start < Duration::from_millis(10));

        // Last timestamp should be the most recent
        let last = counter.last_suppressed();
        let expected_last = start + Duration::from_millis(1000);
        let duration_diff = last.duration_since(expected_last);
        assert!(duration_diff < Duration::from_millis(10));
    }

    #[test]
    fn test_epoch_overflow_handling() {
        // Test with duration that would overflow u64 nanoseconds
        let base = SuppressionCounter::base_instant();
        let now = *base;

        let counter = SuppressionCounter::new(now);

        // Try to record at a time far in the future
        // Duration::from_secs(u64::MAX / 1_000_000_000) would be ~584 years
        // This tests saturation behavior
        let far_future = now + Duration::from_secs(600 * 365 * 24 * 3600); // 600 years

        counter.record_suppression(far_future);

        // Should not panic, timestamps should saturate gracefully
        let _first = counter.first_suppressed();
        let _last = counter.last_suppressed();
        assert!(counter.count() > 0);
    }

    #[test]
    fn test_base_instant_consistency() {
        // base_instant should be consistent across calls
        let base1 = SuppressionCounter::base_instant();
        let base2 = SuppressionCounter::base_instant();

        assert_eq!(base1, base2, "base_instant should be consistent");
    }

    #[test]
    fn test_nanos_conversion_roundtrip() {
        let now = Instant::now();
        let counter = SuppressionCounter::new(now);

        // Convert to nanos and back
        let first = counter.first_suppressed();
        let last = counter.last_suppressed();

        // Both should be close to 'now'
        assert!(first.duration_since(now) < Duration::from_millis(10));
        assert!(last.duration_since(now) < Duration::from_millis(10));
    }

    #[test]
    fn test_atomic_ordering_visibility() {
        use std::sync::atomic::{AtomicBool, Ordering};
        use std::sync::Arc;
        use std::thread;

        let now = Instant::now();
        let counter = Arc::new(SuppressionCounter::new(now));
        let done = Arc::new(AtomicBool::new(false));

        let counter_clone = Arc::clone(&counter);
        let done_clone = Arc::clone(&done);

        // Writer thread
        let writer = thread::spawn(move || {
            for i in 1..=100 {
                counter_clone.record_suppression(now + Duration::from_millis(i));
                thread::sleep(Duration::from_micros(10));
            }
            done_clone.store(true, Ordering::Release);
        });

        // Reader thread
        let counter_clone2 = Arc::clone(&counter);
        let done_clone2 = Arc::clone(&done);
        let reader = thread::spawn(move || {
            let mut last_count = 0;
            while !done_clone2.load(Ordering::Acquire) {
                let count = counter_clone2.count();
                // Count should never decrease
                assert!(count >= last_count, "Count should be monotonic");
                last_count = count;
                thread::sleep(Duration::from_micros(10));
            }
        });

        writer.join().unwrap();
        reader.join().unwrap();

        // Final count should be 100
        assert_eq!(counter.count(), 100);
    }

    #[test]
    fn test_summary_with_zero_duration() {
        let sig = EventSignature::simple("INFO", "Test");
        let now = Instant::now();
        let counter = SuppressionCounter::new(now);

        // Create summary immediately - same timestamp for first and last
        let summary = SuppressionSummary::from_counter(sig, &counter);

        assert_eq!(summary.count, 0);
        assert_eq!(summary.duration, Duration::from_secs(0));
        assert_eq!(summary.first_suppressed, summary.last_suppressed);
    }

    #[test]
    #[cfg(feature = "redis-storage")]
    fn test_snapshot_roundtrip() {
        let now = Instant::now();
        let counter = SuppressionCounter::new(now);

        counter.record_suppression(now + Duration::from_secs(1));
        counter.record_suppression(now + Duration::from_secs(2));

        let snapshot = counter.snapshot();

        let restored = SuppressionCounter::from_snapshot(
            snapshot.suppressed_count,
            snapshot.first_suppressed,
            snapshot.last_suppressed,
        );

        assert_eq!(counter.count(), restored.count());
        // Note: timestamps may have slight differences due to serialization precision
        let first_diff = counter
            .first_suppressed()
            .duration_since(restored.first_suppressed());
        let last_diff = counter
            .last_suppressed()
            .duration_since(restored.last_suppressed());

        assert!(first_diff < Duration::from_millis(1));
        assert!(last_diff < Duration::from_millis(1));
    }
}