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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
//! Tracing integration layer.
//!
//! Provides a `tracing::Layer` implementation that applies rate limiting
//! to log events.
use crate::application::{
circuit_breaker::CircuitBreaker,
emitter::EmitterConfig,
limiter::{LimitDecision, RateLimiter},
metrics::Metrics,
ports::{Clock, Storage},
registry::{EventState, SuppressionRegistry},
};
use crate::domain::{policy::Policy, signature::EventSignature};
use crate::infrastructure::clock::SystemClock;
use crate::infrastructure::storage::ShardedStorage;
use crate::infrastructure::visitor::FieldVisitor;
use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;
use std::time::Duration;
use tracing::{Metadata, Subscriber};
use tracing_subscriber::layer::Filter;
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::{layer::Context, Layer};
/// Internal target used for summary emission events.
///
/// Events emitted with this target are automatically exempt from throttling to
/// prevent recursive suppression of summary messages. This target is intentionally
/// internal and not part of the public API.
#[cfg(feature = "async")]
const SUMMARY_TARGET: &str = "tracing_throttle::summary";
#[cfg(feature = "async")]
use crate::application::emitter::{EmitterHandle, SummaryEmitter};
#[cfg(feature = "async")]
use crate::domain::summary::SuppressionSummary;
#[cfg(feature = "async")]
use std::sync::Mutex;
/// Function type for formatting suppression summaries.
///
/// Takes a reference to a `SuppressionSummary` and emits it as a tracing event.
/// The function is responsible for choosing the log level and format.
#[cfg(feature = "async")]
pub type SummaryFormatter = Arc<dyn Fn(&SuppressionSummary) + Send + Sync + 'static>;
/// Error returned when building a TracingRateLimitLayer fails.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BuildError {
/// Maximum signatures must be greater than zero
ZeroMaxSignatures,
/// Emitter configuration validation failed
EmitterConfig(crate::application::emitter::EmitterConfigError),
}
impl std::fmt::Display for BuildError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BuildError::ZeroMaxSignatures => {
write!(f, "max_signatures must be greater than 0")
}
BuildError::EmitterConfig(e) => {
write!(f, "emitter configuration error: {}", e)
}
}
}
}
impl std::error::Error for BuildError {}
impl From<crate::application::emitter::EmitterConfigError> for BuildError {
fn from(e: crate::application::emitter::EmitterConfigError) -> Self {
BuildError::EmitterConfig(e)
}
}
/// Builder for constructing a `TracingRateLimitLayer`.
pub struct TracingRateLimitLayerBuilder {
policy: Policy,
summary_interval: Duration,
clock: Option<Arc<dyn Clock>>,
max_signatures: Option<usize>,
enable_active_emission: bool,
#[cfg(feature = "async")]
summary_formatter: Option<SummaryFormatter>,
span_context_fields: Vec<String>,
excluded_fields: BTreeSet<String>,
eviction_strategy: Option<EvictionStrategy>,
exempt_targets: BTreeSet<String>,
}
/// Eviction strategy configuration for the rate limit layer.
///
/// This enum provides a user-friendly API that internally creates
/// the appropriate EvictionPolicy adapter.
#[derive(Clone)]
pub enum EvictionStrategy {
/// LRU (Least Recently Used) eviction with entry count limit.
Lru {
/// Maximum number of entries
max_entries: usize,
},
/// Priority-based eviction using a custom function.
Priority {
/// Maximum number of entries
max_entries: usize,
/// Priority calculation function
priority_fn: crate::infrastructure::eviction::PriorityFn<EventSignature, EventState>,
},
/// Memory-based eviction with byte limit.
Memory {
/// Maximum memory usage in bytes
max_bytes: usize,
},
/// Combined priority and memory limits.
PriorityWithMemory {
/// Maximum number of entries
max_entries: usize,
/// Priority calculation function
priority_fn: crate::infrastructure::eviction::PriorityFn<EventSignature, EventState>,
/// Maximum memory usage in bytes
max_bytes: usize,
},
}
impl EvictionStrategy {
/// Check if this strategy tracks memory usage.
pub fn tracks_memory(&self) -> bool {
matches!(
self,
EvictionStrategy::Memory { .. } | EvictionStrategy::PriorityWithMemory { .. }
)
}
/// Get the memory limit if this strategy uses one.
pub fn memory_limit(&self) -> Option<usize> {
match self {
EvictionStrategy::Memory { max_bytes } => Some(*max_bytes),
EvictionStrategy::PriorityWithMemory { max_bytes, .. } => Some(*max_bytes),
_ => None,
}
}
/// Check if this strategy uses priority-based eviction.
pub fn uses_priority(&self) -> bool {
matches!(
self,
EvictionStrategy::Priority { .. } | EvictionStrategy::PriorityWithMemory { .. }
)
}
}
impl std::fmt::Debug for EvictionStrategy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
EvictionStrategy::Lru { max_entries } => f
.debug_struct("Lru")
.field("max_entries", max_entries)
.finish(),
EvictionStrategy::Priority {
max_entries,
priority_fn: _,
} => f
.debug_struct("Priority")
.field("max_entries", max_entries)
.field("priority_fn", &"<fn>")
.finish(),
EvictionStrategy::Memory { max_bytes } => f
.debug_struct("Memory")
.field("max_bytes", max_bytes)
.finish(),
EvictionStrategy::PriorityWithMemory {
max_entries,
priority_fn: _,
max_bytes,
} => f
.debug_struct("PriorityWithMemory")
.field("max_entries", max_entries)
.field("priority_fn", &"<fn>")
.field("max_bytes", max_bytes)
.finish(),
}
}
}
impl TracingRateLimitLayerBuilder {
/// Set the rate limiting policy.
pub fn with_policy(mut self, policy: Policy) -> Self {
self.policy = policy;
self
}
/// Set the summary emission interval.
///
/// The interval will be validated when `build()` is called.
pub fn with_summary_interval(mut self, interval: Duration) -> Self {
self.summary_interval = interval;
self
}
/// Set a custom clock (mainly for testing).
pub fn with_clock(mut self, clock: Arc<dyn Clock>) -> Self {
self.clock = Some(clock);
self
}
/// Set the maximum number of unique event signatures to track.
///
/// When this limit is reached, the least recently used signatures will be evicted.
/// This prevents unbounded memory growth in applications with high signature cardinality.
///
/// Default: 10,000 signatures
///
/// The value will be validated when `build()` is called.
pub fn with_max_signatures(mut self, max_signatures: usize) -> Self {
self.max_signatures = Some(max_signatures);
self
}
/// Disable the signature limit, allowing unbounded growth.
///
/// **Warning**: This can lead to unbounded memory usage in applications that generate
/// many unique event signatures. Only use this if you're certain your application has
/// bounded signature cardinality or you have external memory monitoring.
pub fn with_unlimited_signatures(mut self) -> Self {
self.max_signatures = None;
self
}
/// Enable active emission of suppression summaries.
///
/// When enabled, the layer will automatically emit `WARN`-level tracing events
/// containing summaries of suppressed log events at the configured interval.
///
/// **Requires the `async` feature** - this method has no effect without it.
///
/// Default: disabled
///
/// # Example
///
/// ```no_run
/// # use tracing_throttle::TracingRateLimitLayer;
/// # use std::time::Duration;
/// let layer = TracingRateLimitLayer::builder()
/// .with_active_emission(true)
/// .with_summary_interval(Duration::from_secs(60))
/// .build()
/// .unwrap();
/// ```
pub fn with_active_emission(mut self, enabled: bool) -> Self {
self.enable_active_emission = enabled;
self
}
/// Set a custom formatter for suppression summaries.
///
/// The formatter is responsible for emitting summaries as tracing events.
/// This allows full control over log level, message format, and structured fields.
///
/// **Requires the `async` feature.**
///
/// If not set, a default formatter is used that emits at WARN level with
/// `signature` and `count` fields.
///
/// # Example
///
/// ```no_run
/// # use tracing_throttle::TracingRateLimitLayer;
/// # use std::sync::Arc;
/// # use std::time::Duration;
/// let layer = TracingRateLimitLayer::builder()
/// .with_active_emission(true)
/// .with_summary_formatter(Arc::new(|summary| {
/// tracing::info!(
/// signature = %summary.signature,
/// count = summary.count,
/// duration_secs = summary.duration.as_secs(),
/// "Suppression summary"
/// );
/// }))
/// .build()
/// .unwrap();
/// ```
#[cfg(feature = "async")]
pub fn with_summary_formatter(mut self, formatter: SummaryFormatter) -> Self {
self.summary_formatter = Some(formatter);
self
}
/// Include span context fields in event signatures.
///
/// When specified, the layer will extract these fields from the current span
/// context and include them in the event signature. This enables rate limiting
/// per-user, per-tenant, per-request, or any other span-level context.
///
/// Duplicate field names are automatically removed, and empty field names are filtered out.
///
/// # Example
///
/// ```no_run
/// # use tracing_throttle::TracingRateLimitLayer;
/// // Rate limit separately per user
/// let layer = TracingRateLimitLayer::builder()
/// .with_span_context_fields(vec!["user_id".to_string()])
/// .build()
/// .unwrap();
///
/// // Rate limit per user and tenant
/// let layer = TracingRateLimitLayer::builder()
/// .with_span_context_fields(vec!["user_id".to_string(), "tenant_id".to_string()])
/// .build()
/// .unwrap();
/// ```
///
/// # Usage with Spans
///
/// ```no_run
/// # use tracing::{info, info_span};
/// // Create a span with user context
/// let span = info_span!("request", user_id = "alice");
/// let _enter = span.enter();
///
/// // These events will be rate limited separately per user
/// info!("Processing request"); // Limited for user "alice"
/// ```
pub fn with_span_context_fields(mut self, fields: Vec<String>) -> Self {
// Deduplicate and filter out empty field names
let unique_fields: BTreeSet<_> = fields.into_iter().filter(|f| !f.is_empty()).collect();
self.span_context_fields = unique_fields.into_iter().collect();
self
}
/// Exclude specific fields from event signatures.
///
/// By default, ALL event fields are included in signatures. This ensures that
/// events with different field values are treated as distinct events, preventing
/// accidental loss of meaningful log data.
///
/// Use this method to exclude high-cardinality fields that don't change the
/// semantic meaning of the event (e.g., request_id, timestamp, trace_id).
///
/// Duplicate field names are automatically removed, and empty field names are filtered out.
///
/// # Example
///
/// ```no_run
/// # use tracing_throttle::TracingRateLimitLayer;
/// // Exclude request_id so events with same user_id are deduplicated
/// let layer = TracingRateLimitLayer::builder()
/// .with_excluded_fields(vec!["request_id".to_string(), "trace_id".to_string()])
/// .build()
/// .unwrap();
/// ```
///
/// # Default Behavior (ALL fields included)
///
/// ```no_run
/// # use tracing::error;
/// // These are DIFFERENT events (different user_id values)
/// error!(user_id = 123, "Failed to fetch user");
/// error!(user_id = 456, "Failed to fetch user");
/// // Both are logged - they have distinct signatures
/// ```
///
/// # With Exclusions
///
/// ```no_run
/// # use tracing::error;
/// // Exclude request_id from signature
/// error!(user_id = 123, request_id = "abc", "Failed to fetch user");
/// error!(user_id = 123, request_id = "def", "Failed to fetch user");
/// // Second is throttled - same user_id, request_id excluded from signature
/// ```
pub fn with_excluded_fields(mut self, fields: Vec<String>) -> Self {
// Deduplicate and filter out empty field names
let unique_fields: BTreeSet<_> = fields.into_iter().filter(|f| !f.is_empty()).collect();
self.excluded_fields = unique_fields;
self
}
/// Exempt specific targets from rate limiting.
///
/// Events from the specified targets will bypass rate limiting entirely and
/// always be allowed through. This is useful for ensuring critical logs (e.g.,
/// security events, audit logs) are never suppressed.
///
/// Targets are matched exactly against the event's target (module path).
/// Duplicate targets are automatically removed, and empty targets are filtered out.
///
/// # Example
///
/// ```no_run
/// # use tracing_throttle::TracingRateLimitLayer;
/// // Never throttle security or audit logs
/// let layer = TracingRateLimitLayer::builder()
/// .with_exempt_targets(vec![
/// "myapp::security".to_string(),
/// "myapp::audit".to_string(),
/// ])
/// .build()
/// .unwrap();
/// ```
///
/// # Usage with Events
///
/// ```no_run
/// # use tracing::error;
/// // Explicitly set target - this event bypasses throttling
/// error!(target: "myapp::security", "Security breach detected");
///
/// // Or use the module's default target
/// mod security {
/// use tracing::warn;
/// pub fn check() {
/// // Target is automatically "myapp::security" - bypasses throttling
/// warn!("Suspicious activity detected");
/// }
/// }
/// ```
pub fn with_exempt_targets(mut self, targets: Vec<String>) -> Self {
// Deduplicate and filter out empty targets
let unique_targets: BTreeSet<_> = targets.into_iter().filter(|t| !t.is_empty()).collect();
self.exempt_targets = unique_targets;
self
}
/// Set a custom eviction strategy for signature management.
///
/// Controls which signatures are evicted when storage limits are reached.
/// If not set, uses LRU eviction with the configured max_signatures limit.
///
/// # Example: Priority-based eviction
///
/// ```no_run
/// # use tracing_throttle::{TracingRateLimitLayer, EvictionStrategy};
/// # use std::sync::Arc;
/// let layer = TracingRateLimitLayer::builder()
/// .with_eviction_strategy(EvictionStrategy::Priority {
/// max_entries: 5_000,
/// priority_fn: Arc::new(|_sig, state| {
/// // Keep ERROR events longer than INFO events
/// match state.metadata.as_ref().map(|m| m.level.as_str()) {
/// Some("ERROR") => 100,
/// Some("WARN") => 50,
/// Some("INFO") => 10,
/// _ => 5,
/// }
/// })
/// })
/// .build()
/// .unwrap();
/// ```
///
/// # Example: Memory-based eviction
///
/// ```no_run
/// # use tracing_throttle::{TracingRateLimitLayer, EvictionStrategy};
/// // Evict when total memory exceeds 5MB
/// let layer = TracingRateLimitLayer::builder()
/// .with_eviction_strategy(EvictionStrategy::Memory {
/// max_bytes: 5 * 1024 * 1024,
/// })
/// .build()
/// .unwrap();
/// ```
pub fn with_eviction_strategy(mut self, strategy: EvictionStrategy) -> Self {
self.eviction_strategy = Some(strategy);
self
}
/// Build the layer.
///
/// # Errors
/// Returns `BuildError` if the configuration is invalid.
pub fn build(self) -> Result<TracingRateLimitLayer, BuildError> {
// Validate max_signatures if set
if let Some(max) = self.max_signatures {
if max == 0 {
return Err(BuildError::ZeroMaxSignatures);
}
}
// Create shared metrics and circuit breaker
let metrics = Metrics::new();
let circuit_breaker = Arc::new(CircuitBreaker::new());
let clock = self.clock.unwrap_or_else(|| Arc::new(SystemClock::new()));
let mut storage = ShardedStorage::new().with_metrics(metrics.clone());
// Convert eviction strategy to adapter, or use default LRU with max_signatures
let eviction_policy: Option<
Arc<dyn crate::application::ports::EvictionPolicy<EventSignature, EventState>>,
> = match self.eviction_strategy {
Some(EvictionStrategy::Lru { max_entries }) => Some(Arc::new(
crate::infrastructure::eviction::LruEviction::new(max_entries),
)),
Some(EvictionStrategy::Priority {
max_entries,
priority_fn,
}) => Some(Arc::new(
crate::infrastructure::eviction::PriorityEviction::new(max_entries, priority_fn),
)),
Some(EvictionStrategy::Memory { max_bytes }) => Some(Arc::new(
crate::infrastructure::eviction::MemoryEviction::new(max_bytes),
)),
Some(EvictionStrategy::PriorityWithMemory {
max_entries,
priority_fn,
max_bytes,
}) => Some(Arc::new(
crate::infrastructure::eviction::PriorityWithMemoryEviction::new(
max_entries,
priority_fn,
max_bytes,
),
)),
None => {
// Use default LRU with max_signatures if configured
self.max_signatures.map(|max| {
Arc::new(crate::infrastructure::eviction::LruEviction::new(max))
as Arc<
dyn crate::application::ports::EvictionPolicy<
EventSignature,
EventState,
>,
>
})
}
};
if let Some(policy) = eviction_policy {
storage = storage.with_eviction_policy(policy);
}
let storage = Arc::new(storage);
let registry = SuppressionRegistry::new(storage, clock, self.policy);
let limiter = RateLimiter::new(registry.clone(), metrics.clone(), circuit_breaker);
// Let EmitterConfig validate the interval
let emitter_config = EmitterConfig::new(self.summary_interval)?;
#[cfg(feature = "async")]
let emitter_handle = if self.enable_active_emission {
let emitter = SummaryEmitter::new(registry, emitter_config);
// Use custom formatter or default
let formatter = self.summary_formatter.unwrap_or_else(|| {
Arc::new(|summary: &SuppressionSummary| {
tracing::warn!(
target: SUMMARY_TARGET,
signature = %summary.signature,
count = summary.count,
"{}",
summary.format_message()
);
})
});
let handle = emitter.start(
move |summaries| {
for summary in summaries {
formatter(&summary);
}
},
false, // Don't emit final summaries on shutdown
);
Arc::new(Mutex::new(Some(handle)))
} else {
Arc::new(Mutex::new(None))
};
Ok(TracingRateLimitLayer {
limiter,
span_context_fields: Arc::new(self.span_context_fields),
excluded_fields: Arc::new(self.excluded_fields),
exempt_targets: Arc::new(self.exempt_targets),
#[cfg(feature = "async")]
emitter_handle,
#[cfg(not(feature = "async"))]
_emitter_config: emitter_config,
})
}
}
/// A `tracing::Layer` that applies rate limiting to events.
///
/// This layer intercepts events, computes their signature, and decides
/// whether to allow or suppress them based on the configured policy.
///
/// Optionally emits periodic summaries of suppressed events when active
/// emission is enabled (requires `async` feature).
#[derive(Clone)]
pub struct TracingRateLimitLayer<S = Arc<ShardedStorage<EventSignature, EventState>>>
where
S: Storage<EventSignature, EventState> + Clone,
{
limiter: RateLimiter<S>,
span_context_fields: Arc<Vec<String>>,
excluded_fields: Arc<BTreeSet<String>>,
exempt_targets: Arc<BTreeSet<String>>,
#[cfg(feature = "async")]
emitter_handle: Arc<Mutex<Option<EmitterHandle>>>,
#[cfg(not(feature = "async"))]
_emitter_config: EmitterConfig,
}
impl<S> TracingRateLimitLayer<S>
where
S: Storage<EventSignature, EventState> + Clone,
{
/// Extract span context fields from the current span.
fn extract_span_context<Sub>(
&self,
cx: &Context<'_, Sub>,
) -> BTreeMap<Cow<'static, str>, Cow<'static, str>>
where
Sub: Subscriber + for<'lookup> LookupSpan<'lookup>,
{
if self.span_context_fields.is_empty() {
return BTreeMap::new();
}
let mut context_fields = BTreeMap::new();
if let Some(span) = cx.lookup_current() {
for span_ref in span.scope() {
let extensions = span_ref.extensions();
if let Some(stored_fields) =
extensions.get::<BTreeMap<Cow<'static, str>, Cow<'static, str>>>()
{
for field_name in self.span_context_fields.as_ref() {
// Create an owned Cow since we can't guarantee 'static lifetime from the String
let field_key: Cow<'static, str> = Cow::Owned(field_name.clone());
if let std::collections::btree_map::Entry::Vacant(e) =
context_fields.entry(field_key.clone())
{
if let Some(value) = stored_fields.get(&field_key) {
e.insert(value.clone());
}
}
}
}
if context_fields.len() == self.span_context_fields.len() {
break;
}
}
}
context_fields
}
/// Extract event fields from an event.
///
/// Extracts ALL fields from the event, then excludes any fields in the
/// excluded_fields set. This ensures that field values are included in
/// event signatures by default, preventing accidental deduplication of
/// semantically different events.
fn extract_event_fields(
&self,
event: &tracing::Event<'_>,
) -> BTreeMap<Cow<'static, str>, Cow<'static, str>> {
let mut visitor = FieldVisitor::new();
event.record(&mut visitor);
let all_fields = visitor.into_fields();
// Exclude configured fields (e.g., high-cardinality fields like request_id)
if self.excluded_fields.is_empty() {
all_fields
} else {
all_fields
.into_iter()
.filter(|(field_name, _)| !self.excluded_fields.contains(field_name.as_ref()))
.collect()
}
}
/// Compute event signature from tracing metadata, span context, and event fields.
///
/// The signature includes:
/// - Log level (INFO, WARN, ERROR, etc.)
/// - Message template
/// - Target module path
/// - Span context fields (if configured)
/// - Event fields (if configured)
fn compute_signature(
&self,
metadata: &Metadata,
combined_fields: &BTreeMap<Cow<'static, str>, Cow<'static, str>>,
) -> EventSignature {
let level = metadata.level().as_str();
let message = metadata.name();
let target = Some(metadata.target());
// Use combined fields (span context + event fields) in signature
EventSignature::new(level, message, combined_fields, target)
}
/// Check if an event should be allowed through.
pub fn should_allow(&self, signature: EventSignature) -> bool {
matches!(self.limiter.check_event(signature), LimitDecision::Allow)
}
/// Check if an event should be allowed through and capture metadata.
///
/// This method stores event metadata on first occurrence so summaries
/// can show human-readable event details instead of just signature hashes.
///
/// **Note:** Only available with the `human-readable` feature flag.
#[cfg(feature = "human-readable")]
pub fn should_allow_with_metadata(
&self,
signature: EventSignature,
metadata: crate::domain::metadata::EventMetadata,
) -> bool {
matches!(
self.limiter.check_event_with_metadata(signature, metadata),
LimitDecision::Allow
)
}
/// Get a reference to the underlying limiter.
pub fn limiter(&self) -> &RateLimiter<S> {
&self.limiter
}
/// Get a reference to the metrics.
///
/// Returns metrics about rate limiting behavior including:
/// - Events allowed
/// - Events suppressed
/// - Signatures evicted
pub fn metrics(&self) -> &Metrics {
self.limiter.metrics()
}
/// Get the current number of tracked signatures.
pub fn signature_count(&self) -> usize {
self.limiter.registry().len()
}
/// Get a reference to the circuit breaker.
///
/// Use this to check the circuit breaker state and health:
/// - `circuit_breaker().state()` - Current circuit state
/// - `circuit_breaker().consecutive_failures()` - Failure count
pub fn circuit_breaker(&self) -> &Arc<CircuitBreaker> {
self.limiter.circuit_breaker()
}
/// Shutdown the active suppression summary emitter, if running.
///
/// This method gracefully stops the background emission task. If active emission
/// is not enabled, this method does nothing.
///
/// **Requires the `async` feature.**
///
/// # Errors
///
/// Returns an error if the emitter task fails to shut down gracefully.
///
/// # Example
///
/// ```no_run
/// # use tracing_throttle::TracingRateLimitLayer;
/// # async fn example() {
/// let layer = TracingRateLimitLayer::builder()
/// .with_active_emission(true)
/// .build()
/// .unwrap();
///
/// // Use the layer...
///
/// // Shutdown before dropping
/// layer.shutdown().await.expect("shutdown failed");
/// # }
/// ```
#[cfg(feature = "async")]
pub async fn shutdown(&self) -> Result<(), crate::application::emitter::ShutdownError> {
// Take the handle while holding the lock, then release the lock before awaiting
let handle = {
let mut handle_guard = self.emitter_handle.lock().unwrap();
handle_guard.take()
};
if let Some(handle) = handle {
handle.shutdown().await?;
}
Ok(())
}
}
impl TracingRateLimitLayer<Arc<ShardedStorage<EventSignature, EventState>>> {
/// Create a builder for configuring the layer.
///
/// Defaults:
/// - Policy: token bucket (50 burst capacity, 1 token/sec refill rate)
/// - Max signatures: 10,000 (with LRU eviction)
/// - Summary interval: 30 seconds
/// - Active emission: disabled
/// - Summary formatter: default (WARN level with signature and count)
pub fn builder() -> TracingRateLimitLayerBuilder {
TracingRateLimitLayerBuilder {
policy: Policy::token_bucket(50.0, 1.0)
.expect("default policy with 50 capacity and 1/sec refill is always valid"),
summary_interval: Duration::from_secs(30),
clock: None,
max_signatures: Some(10_000),
enable_active_emission: false,
#[cfg(feature = "async")]
summary_formatter: None,
span_context_fields: Vec::new(),
excluded_fields: BTreeSet::new(),
eviction_strategy: None,
exempt_targets: BTreeSet::new(),
}
}
/// Create a layer with default settings.
///
/// Equivalent to `TracingRateLimitLayer::builder().build().unwrap()`.
///
/// Defaults:
/// - Policy: token bucket (50 burst capacity, 1 token/sec refill rate = 60/min)
/// - Max signatures: 10,000 (with LRU eviction)
/// - Summary interval: 30 seconds
///
/// # Panics
/// This method cannot panic because all default values are valid.
pub fn new() -> Self {
Self::builder()
.build()
.expect("default configuration is always valid")
}
/// Create a layer with custom storage backend.
///
/// This allows using alternative storage implementations like Redis for distributed
/// rate limiting across multiple application instances.
///
/// # Arguments
///
/// * `storage` - Custom storage implementation (must implement `Storage<EventSignature, EventState>`)
/// * `policy` - Rate limiting policy to apply
/// * `clock` - Clock implementation (use `SystemClock::new()` for production)
///
/// # Example with Redis
///
/// ```rust,ignore
/// use tracing_throttle::{TracingRateLimitLayer, RedisStorage, Policy, SystemClock};
/// use std::sync::Arc;
///
/// #[tokio::main]
/// async fn main() {
/// let storage = Arc::new(
/// RedisStorage::connect("redis://127.0.0.1/")
/// .await
/// .expect("Failed to connect")
/// );
/// let policy = Policy::token_bucket(100.0, 10.0).unwrap();
/// let clock = Arc::new(SystemClock::new());
///
/// let layer = TracingRateLimitLayer::with_storage(storage, policy, clock);
/// }
/// ```
pub fn with_storage<ST>(
storage: ST,
policy: Policy,
clock: Arc<dyn Clock>,
) -> TracingRateLimitLayer<ST>
where
ST: Storage<EventSignature, EventState> + Clone,
{
let metrics = Metrics::new();
let circuit_breaker = Arc::new(CircuitBreaker::new());
let registry = SuppressionRegistry::new(storage, clock, policy);
let limiter = RateLimiter::new(registry, metrics, circuit_breaker);
TracingRateLimitLayer {
limiter,
span_context_fields: Arc::new(Vec::new()),
excluded_fields: Arc::new(BTreeSet::new()),
exempt_targets: Arc::new(BTreeSet::new()),
#[cfg(feature = "async")]
emitter_handle: Arc::new(Mutex::new(None)),
#[cfg(not(feature = "async"))]
_emitter_config: EmitterConfig::new(Duration::from_secs(30))
.expect("30 seconds is valid"),
}
}
}
impl Default for TracingRateLimitLayer<Arc<ShardedStorage<EventSignature, EventState>>> {
fn default() -> Self {
Self::new()
}
}
// Implement the Filter trait for rate limiting
impl<S, Sub> Filter<Sub> for TracingRateLimitLayer<S>
where
S: Storage<EventSignature, EventState> + Clone,
Sub: Subscriber + for<'lookup> LookupSpan<'lookup>,
{
fn enabled(&self, _meta: &Metadata<'_>, _cx: &Context<'_, Sub>) -> bool {
// Always return true - actual filtering happens in event_enabled
// This prevents double-checking in dual-layer setups
true
}
fn event_enabled(&self, event: &tracing::Event<'_>, cx: &Context<'_, Sub>) -> bool {
let metadata_obj = event.metadata();
let target = metadata_obj.target();
// Exempt our internal summary emission target to prevent recursive throttling.
// Only the default formatter uses this target; custom formatters are the user's
// responsibility and are intentionally subject to normal throttling rules.
#[cfg(feature = "async")]
if target == SUMMARY_TARGET {
return true;
}
// Check if this target is exempt from rate limiting
// Skip the lookup if no exempt targets are configured (common case)
if !self.exempt_targets.is_empty() && self.exempt_targets.contains(target) {
// Exempt targets bypass rate limiting entirely
self.limiter.metrics().record_allowed();
return true;
}
// Combine span context and event fields
let mut combined_fields = self.extract_span_context(cx);
let event_fields = self.extract_event_fields(event);
combined_fields.extend(event_fields);
let signature = self.compute_signature(metadata_obj, &combined_fields);
#[cfg(feature = "human-readable")]
{
// Extract message from event for metadata
let mut visitor = FieldVisitor::new();
event.record(&mut visitor);
let all_fields = visitor.into_fields();
let message = all_fields
.get(&Cow::Borrowed("message"))
.map(|v| v.to_string())
.unwrap_or_else(|| event.metadata().name().to_string());
// Create EventMetadata for this event
let event_metadata = crate::domain::metadata::EventMetadata::new(
metadata_obj.level().as_str().to_string(),
message,
target.to_string(),
combined_fields,
);
self.should_allow_with_metadata(signature, event_metadata)
}
#[cfg(not(feature = "human-readable"))]
{
self.should_allow(signature)
}
}
}
impl<S, Sub> Layer<Sub> for TracingRateLimitLayer<S>
where
S: Storage<EventSignature, EventState> + Clone + 'static,
Sub: Subscriber + for<'lookup> LookupSpan<'lookup>,
{
fn on_new_span(
&self,
attrs: &tracing::span::Attributes<'_>,
id: &tracing::span::Id,
ctx: Context<'_, Sub>,
) {
if self.span_context_fields.is_empty() {
return;
}
let mut visitor = FieldVisitor::new();
attrs.record(&mut visitor);
let fields = visitor.into_fields();
if let Some(span) = ctx.span(id) {
let mut extensions = span.extensions_mut();
extensions.insert(fields);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use tracing::info;
use tracing_subscriber::layer::SubscriberExt;
#[test]
fn test_layer_builder() {
let layer = TracingRateLimitLayer::builder()
.with_policy(Policy::count_based(50).unwrap())
.with_summary_interval(Duration::from_secs(60))
.build()
.unwrap();
assert!(layer.limiter().registry().is_empty());
}
#[test]
fn test_span_context_fields_deduplication() {
let layer = TracingRateLimitLayer::builder()
.with_span_context_fields(vec![
"user_id".to_string(),
"user_id".to_string(), // duplicate
"tenant_id".to_string(),
"".to_string(), // empty, should be filtered
"user_id".to_string(), // another duplicate
])
.build()
.unwrap();
// Should only have 2 unique fields: user_id and tenant_id
assert_eq!(layer.span_context_fields.len(), 2);
assert!(layer.span_context_fields.iter().any(|f| f == "user_id"));
assert!(layer.span_context_fields.iter().any(|f| f == "tenant_id"));
}
#[test]
fn test_excluded_fields_deduplication() {
let layer = TracingRateLimitLayer::builder()
.with_excluded_fields(vec![
"request_id".to_string(),
"request_id".to_string(), // duplicate
"trace_id".to_string(),
"".to_string(), // empty, should be filtered
"request_id".to_string(), // another duplicate
])
.build()
.unwrap();
// Should only have 2 unique fields: request_id and trace_id
assert_eq!(layer.excluded_fields.len(), 2);
assert!(layer.excluded_fields.contains("request_id"));
assert!(layer.excluded_fields.contains("trace_id"));
}
#[test]
fn test_exempt_targets_deduplication() {
let layer = TracingRateLimitLayer::builder()
.with_exempt_targets(vec![
"myapp::security".to_string(),
"myapp::security".to_string(), // duplicate
"myapp::audit".to_string(),
"".to_string(), // empty, should be filtered
"myapp::security".to_string(), // another duplicate
])
.build()
.unwrap();
// Should only have 2 unique targets: security and audit
assert_eq!(layer.exempt_targets.len(), 2);
assert!(layer.exempt_targets.contains("myapp::security"));
assert!(layer.exempt_targets.contains("myapp::audit"));
}
#[test]
fn test_exempt_targets_bypass_rate_limiting() {
let rate_limit = TracingRateLimitLayer::builder()
.with_policy(Policy::count_based(2).unwrap())
.with_exempt_targets(vec!["myapp::security".to_string()])
.build()
.unwrap();
let subscriber = tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_filter(rate_limit.clone()));
tracing::subscriber::with_default(subscriber, || {
// Regular logs get throttled after 2 (same callsite = same signature)
for _ in 0..3 {
info!("Regular log"); // First 2 allowed, 3rd suppressed
}
// Security logs are never throttled (exempt target)
for _ in 0..4 {
info!(target: "myapp::security", "Security event"); // All 4 allowed
}
});
// Verify metrics
let metrics = rate_limit.metrics();
assert_eq!(metrics.events_allowed(), 6); // 2 regular + 4 exempt
assert_eq!(metrics.events_suppressed(), 1); // 1 regular suppressed
}
#[test]
fn test_layer_default() {
let layer = TracingRateLimitLayer::default();
assert!(layer.limiter().registry().is_empty());
}
#[test]
fn test_signature_computation() {
let _layer = TracingRateLimitLayer::new();
// Use a simple signature test without metadata construction
let sig1 = EventSignature::simple("INFO", "test_event");
let sig2 = EventSignature::simple("INFO", "test_event");
// Same inputs should produce same signature
assert_eq!(sig1, sig2);
}
#[test]
fn test_basic_rate_limiting() {
let layer = TracingRateLimitLayer::builder()
.with_policy(Policy::count_based(2).unwrap())
.build()
.unwrap();
let sig = EventSignature::simple("INFO", "test_message");
// First two should be allowed
assert!(layer.should_allow(sig));
assert!(layer.should_allow(sig));
// Third should be suppressed
assert!(!layer.should_allow(sig));
}
#[test]
fn test_layer_integration() {
let layer = TracingRateLimitLayer::builder()
.with_policy(Policy::count_based(3).unwrap())
.build()
.unwrap();
// Clone for use in subscriber, keep original for checking state
let layer_for_check = layer.clone();
let subscriber = tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_filter(layer));
// Test that the layer correctly tracks event signatures
tracing::subscriber::with_default(subscriber, || {
// Emit 10 identical events
for _ in 0..10 {
info!("test event");
}
});
// After emitting 10 events with the same signature, the layer should have
// tracked them and only the first 3 should have been marked as allowed
// The registry should contain one entry for this signature
assert_eq!(layer_for_check.limiter().registry().len(), 1);
}
#[test]
fn test_layer_suppression_logic() {
let layer = TracingRateLimitLayer::builder()
.with_policy(Policy::count_based(3).unwrap())
.build()
.unwrap();
let sig = EventSignature::simple("INFO", "test");
// Verify the suppression logic works correctly
let mut allowed_count = 0;
for _ in 0..10 {
if layer.should_allow(sig) {
allowed_count += 1;
}
}
assert_eq!(allowed_count, 3);
}
#[test]
fn test_builder_zero_summary_interval() {
let result = TracingRateLimitLayer::builder()
.with_summary_interval(Duration::from_secs(0))
.build();
assert!(matches!(
result,
Err(BuildError::EmitterConfig(
crate::application::emitter::EmitterConfigError::ZeroSummaryInterval
))
));
}
#[test]
fn test_builder_zero_max_signatures() {
let result = TracingRateLimitLayer::builder()
.with_max_signatures(0)
.build();
assert!(matches!(result, Err(BuildError::ZeroMaxSignatures)));
}
#[test]
fn test_builder_valid_max_signatures() {
let layer = TracingRateLimitLayer::builder()
.with_max_signatures(100)
.build()
.unwrap();
assert!(layer.limiter().registry().is_empty());
}
#[test]
fn test_metrics_tracking() {
let layer = TracingRateLimitLayer::builder()
.with_policy(Policy::count_based(2).unwrap())
.build()
.unwrap();
let sig = EventSignature::simple("INFO", "test");
// Check initial metrics
assert_eq!(layer.metrics().events_allowed(), 0);
assert_eq!(layer.metrics().events_suppressed(), 0);
// Allow first two events
assert!(layer.should_allow(sig));
assert!(layer.should_allow(sig));
// Check metrics after allowed events
assert_eq!(layer.metrics().events_allowed(), 2);
assert_eq!(layer.metrics().events_suppressed(), 0);
// Suppress third event
assert!(!layer.should_allow(sig));
// Check metrics after suppressed event
assert_eq!(layer.metrics().events_allowed(), 2);
assert_eq!(layer.metrics().events_suppressed(), 1);
}
#[test]
fn test_metrics_snapshot() {
let layer = TracingRateLimitLayer::builder()
.with_policy(Policy::count_based(3).unwrap())
.build()
.unwrap();
let sig = EventSignature::simple("INFO", "test");
// Generate some events
for _ in 0..5 {
layer.should_allow(sig);
}
// Get snapshot
let snapshot = layer.metrics().snapshot();
assert_eq!(snapshot.events_allowed, 3);
assert_eq!(snapshot.events_suppressed, 2);
assert_eq!(snapshot.total_events(), 5);
assert!((snapshot.suppression_rate() - 0.4).abs() < f64::EPSILON);
}
#[test]
fn test_signature_count() {
let layer = TracingRateLimitLayer::builder()
.with_policy(Policy::count_based(2).unwrap())
.build()
.unwrap();
assert_eq!(layer.signature_count(), 0);
let sig1 = EventSignature::simple("INFO", "test1");
let sig2 = EventSignature::simple("INFO", "test2");
layer.should_allow(sig1);
assert_eq!(layer.signature_count(), 1);
layer.should_allow(sig2);
assert_eq!(layer.signature_count(), 2);
// Same signature shouldn't increase count
layer.should_allow(sig1);
assert_eq!(layer.signature_count(), 2);
}
#[test]
fn test_metrics_with_eviction() {
let layer = TracingRateLimitLayer::builder()
.with_policy(Policy::count_based(1).unwrap())
.with_max_signatures(3)
.build()
.unwrap();
// Fill up to capacity
for i in 0..3 {
let sig = EventSignature::simple("INFO", &format!("test{}", i));
layer.should_allow(sig);
}
assert_eq!(layer.signature_count(), 3);
assert_eq!(layer.metrics().signatures_evicted(), 0);
// Add one more, which should trigger eviction
let sig = EventSignature::simple("INFO", "test3");
layer.should_allow(sig);
assert_eq!(layer.signature_count(), 3);
assert_eq!(layer.metrics().signatures_evicted(), 1);
}
#[test]
fn test_circuit_breaker_observability() {
use crate::application::circuit_breaker::CircuitState;
let layer = TracingRateLimitLayer::builder()
.with_policy(Policy::count_based(2).unwrap())
.build()
.unwrap();
// Check initial circuit breaker state
let cb = layer.circuit_breaker();
assert_eq!(cb.state(), CircuitState::Closed);
assert_eq!(cb.consecutive_failures(), 0);
// Circuit breaker should remain closed during normal operation
let sig = EventSignature::simple("INFO", "test");
layer.should_allow(sig);
layer.should_allow(sig);
layer.should_allow(sig);
assert_eq!(cb.state(), CircuitState::Closed);
}
#[test]
fn test_circuit_breaker_fail_open_integration() {
use crate::application::circuit_breaker::{
CircuitBreaker, CircuitBreakerConfig, CircuitState,
};
use std::time::Duration;
// Create a circuit breaker with low threshold for testing
let cb_config = CircuitBreakerConfig {
failure_threshold: 2,
recovery_timeout: Duration::from_secs(1),
};
let circuit_breaker = Arc::new(CircuitBreaker::with_config(cb_config));
// Build layer with custom circuit breaker
let storage = Arc::new(ShardedStorage::new());
let clock = Arc::new(SystemClock::new());
let policy = Policy::count_based(2).unwrap();
let registry = SuppressionRegistry::new(storage, clock, policy);
let metrics = Metrics::new();
let limiter = RateLimiter::new(registry, metrics, circuit_breaker.clone());
let layer = TracingRateLimitLayer {
limiter,
span_context_fields: Arc::new(Vec::new()),
excluded_fields: Arc::new(BTreeSet::new()),
exempt_targets: Arc::new(BTreeSet::new()),
#[cfg(feature = "async")]
emitter_handle: Arc::new(Mutex::new(None)),
#[cfg(not(feature = "async"))]
_emitter_config: crate::application::emitter::EmitterConfig::new(Duration::from_secs(
30,
))
.unwrap(),
};
let sig = EventSignature::simple("INFO", "test");
// Normal operation - first 2 events allowed, third suppressed
assert!(layer.should_allow(sig));
assert!(layer.should_allow(sig));
assert!(!layer.should_allow(sig));
// Circuit should still be closed
assert_eq!(circuit_breaker.state(), CircuitState::Closed);
// Manually trigger circuit breaker failures to test fail-open
circuit_breaker.record_failure();
circuit_breaker.record_failure();
// Circuit should now be open
assert_eq!(circuit_breaker.state(), CircuitState::Open);
// With circuit open, rate limiter should fail open (allow all events)
// even though we've already hit the rate limit
assert!(layer.should_allow(sig));
assert!(layer.should_allow(sig));
assert!(layer.should_allow(sig));
// Metrics should show these as allowed (fail-open behavior)
let snapshot = layer.metrics().snapshot();
assert!(snapshot.events_allowed >= 5); // 2 normal + 3 fail-open
}
#[cfg(feature = "async")]
#[tokio::test]
async fn test_active_emission_integration() {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
// Use an atomic counter to track emissions
let emission_count = Arc::new(AtomicUsize::new(0));
let count_clone = Arc::clone(&emission_count);
// Create a layer with a custom emitter that increments our counter
let storage = Arc::new(ShardedStorage::new());
let clock = Arc::new(SystemClock::new());
let policy = Policy::count_based(2).unwrap();
let registry = SuppressionRegistry::new(storage, clock, policy);
let emitter_config = EmitterConfig::new(Duration::from_millis(100)).unwrap();
let emitter = SummaryEmitter::new(registry.clone(), emitter_config);
// Start emitter with custom callback
let handle = emitter.start(
move |summaries| {
count_clone.fetch_add(summaries.len(), Ordering::SeqCst);
},
false,
);
// Emit events that will be suppressed
let sig = EventSignature::simple("INFO", "test_message");
for _ in 0..10 {
registry.with_event_state(sig, |state, now| {
state.counter.record_suppression(now);
});
}
// Wait for at least two emission intervals
tokio::time::sleep(Duration::from_millis(250)).await;
// Check that summaries were emitted
let count = emission_count.load(Ordering::SeqCst);
assert!(
count > 0,
"Expected at least one suppression summary to be emitted, got {}",
count
);
// Graceful shutdown
handle.shutdown().await.expect("shutdown failed");
}
#[cfg(feature = "async")]
#[tokio::test]
async fn test_active_emission_disabled() {
use crate::infrastructure::mocks::layer::MockCaptureLayer;
use std::time::Duration;
// Create layer with active emission disabled (default)
let layer = TracingRateLimitLayer::builder()
.with_policy(Policy::count_based(2).unwrap())
.with_summary_interval(Duration::from_millis(100))
.build()
.unwrap();
let mock = MockCaptureLayer::new();
let mock_clone = mock.clone();
let subscriber = tracing_subscriber::registry()
.with(mock)
.with(tracing_subscriber::fmt::layer().with_filter(layer.clone()));
tracing::subscriber::with_default(subscriber, || {
let sig = EventSignature::simple("INFO", "test_message");
for _ in 0..10 {
layer.should_allow(sig);
}
});
// Wait to ensure no emissions occur
tokio::time::sleep(Duration::from_millis(250)).await;
// Should not have emitted any summaries
let events = mock_clone.get_captured();
let summary_count = events
.iter()
.filter(|e| e.message.contains("suppressed"))
.count();
assert_eq!(
summary_count, 0,
"Should not emit summaries when active emission is disabled"
);
// Shutdown should succeed even when emitter was never started
layer.shutdown().await.expect("shutdown failed");
}
#[cfg(feature = "async")]
#[tokio::test]
async fn test_shutdown_without_emission() {
// Test that shutdown works when emission was never enabled
let layer = TracingRateLimitLayer::new();
// Should not error
layer
.shutdown()
.await
.expect("shutdown should succeed when emitter not running");
}
#[cfg(feature = "async")]
#[tokio::test]
async fn test_custom_summary_formatter() {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
// Track formatter invocations
let call_count = Arc::new(AtomicUsize::new(0));
let count_clone = Arc::clone(&call_count);
// Track data passed to formatter
let last_count = Arc::new(AtomicUsize::new(0));
let last_count_clone = Arc::clone(&last_count);
// Create layer with custom formatter
let layer = TracingRateLimitLayer::builder()
.with_policy(Policy::count_based(2).unwrap())
.with_active_emission(true)
.with_summary_interval(Duration::from_millis(100))
.with_summary_formatter(Arc::new(move |summary| {
count_clone.fetch_add(1, Ordering::SeqCst);
last_count_clone.store(summary.count, Ordering::SeqCst);
// Custom format: emit at INFO level instead of WARN
tracing::info!(
sig = %summary.signature,
suppressed = summary.count,
"Custom format"
);
}))
.build()
.unwrap();
// Emit events that will be suppressed
let sig = EventSignature::simple("INFO", "test_message");
for _ in 0..10 {
layer.should_allow(sig);
}
// Wait for emission
tokio::time::sleep(Duration::from_millis(250)).await;
// Verify custom formatter was called
let calls = call_count.load(Ordering::SeqCst);
assert!(calls > 0, "Custom formatter should have been called");
// Verify formatter received correct data
let count = last_count.load(Ordering::SeqCst);
assert!(
count >= 8,
"Expected at least 8 suppressions, got {}",
count
);
layer.shutdown().await.expect("shutdown failed");
}
#[cfg(feature = "async")]
#[tokio::test]
async fn test_default_formatter_used() {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
let emission_count = Arc::new(AtomicUsize::new(0));
let count_clone = Arc::clone(&emission_count);
let storage = Arc::new(ShardedStorage::new());
let clock = Arc::new(SystemClock::new());
let policy = Policy::count_based(2).unwrap();
let registry = SuppressionRegistry::new(storage, clock, policy);
let emitter_config = EmitterConfig::new(Duration::from_millis(100)).unwrap();
let emitter = SummaryEmitter::new(registry.clone(), emitter_config);
// Start without custom formatter - should use default
let handle = emitter.start(
move |summaries| {
count_clone.fetch_add(summaries.len(), Ordering::SeqCst);
},
false,
);
let sig = EventSignature::simple("INFO", "test_message");
for _ in 0..10 {
registry.with_event_state(sig, |state, now| {
state.counter.record_suppression(now);
});
}
tokio::time::sleep(Duration::from_millis(250)).await;
let count = emission_count.load(Ordering::SeqCst);
assert!(count > 0, "Default formatter should have emitted summaries");
handle.shutdown().await.expect("shutdown failed");
}
#[cfg(feature = "async")]
#[tokio::test]
async fn test_summary_emission_not_recursively_throttled() {
use std::time::Duration;
// Build a layer with active emission and a very restrictive policy
let layer = TracingRateLimitLayer::builder()
.with_policy(Policy::count_based(1).unwrap())
.with_active_emission(true)
.with_summary_interval(Duration::from_millis(100))
.build()
.unwrap();
let layer_clone = layer.clone();
let subscriber = tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_filter(layer));
// Emit events to trigger suppressions, then wait for multiple summary intervals.
// If summaries were recursively throttled, the second interval would produce
// nested "Suppressed 1 times: ... Suppressed N times: ..." messages and the
// signature count would keep growing.
tracing::subscriber::with_default(subscriber, || {
// Trigger suppressions
for _ in 0..5 {
tracing::info!(target: "myapp", "repetitive event");
}
});
// Wait for two summary intervals
tokio::time::sleep(Duration::from_millis(250)).await;
// Signature count should be exactly 1 (the "myapp" event).
// If summary events were being throttled, additional signatures would appear.
assert_eq!(
layer_clone.signature_count(),
1,
"Summary emissions should not create additional throttle signatures"
);
layer_clone.shutdown().await.expect("shutdown failed");
}
}