Skip to content

Commit a5932c0

Browse files
giortzisglcian
andauthored
fix(transport): add rate limit for logs (#894)
Co-authored-by: Lorenzo Cian <[email protected]>
1 parent 280dab9 commit a5932c0

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
- build(panic): Fix build without other dependencies ([#883](https://github.com/getsentry/sentry-rust/pull/883)) by @liskin
3535
- The `sentry-panic` crate now builds successfully when used as a standalone dependency.
36+
- fix(transport): add rate limits for logs ([#894](https://github.com/getsentry/sentry-rust/pull/894)) by @giortzisg
3637

3738
## 0.42.0
3839

sentry/src/transports/ratelimit.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use httpdate::parse_http_date;
22
use std::time::{Duration, SystemTime};
33

44
use crate::protocol::EnvelopeItem;
5+
use crate::protocol::ItemContainer;
56
use crate::Envelope;
67

78
/// A Utility that helps with rate limiting sentry requests.
@@ -12,6 +13,7 @@ pub struct RateLimiter {
1213
session: Option<SystemTime>,
1314
transaction: Option<SystemTime>,
1415
attachment: Option<SystemTime>,
16+
log_item: Option<SystemTime>,
1517
}
1618

1719
impl RateLimiter {
@@ -56,6 +58,7 @@ impl RateLimiter {
5658
"session" => self.session = new_time,
5759
"transaction" => self.transaction = new_time,
5860
"attachment" => self.attachment = new_time,
61+
"log_item" => self.log_item = new_time,
5962
_ => {}
6063
}
6164
}
@@ -89,6 +92,7 @@ impl RateLimiter {
8992
RateLimitingCategory::Session => self.session,
9093
RateLimitingCategory::Transaction => self.transaction,
9194
RateLimitingCategory::Attachment => self.attachment,
95+
RateLimitingCategory::LogItem => self.log_item,
9296
}?;
9397
time_left.duration_since(SystemTime::now()).ok()
9498
}
@@ -112,6 +116,9 @@ impl RateLimiter {
112116
}
113117
EnvelopeItem::Transaction(_) => RateLimitingCategory::Transaction,
114118
EnvelopeItem::Attachment(_) => RateLimitingCategory::Attachment,
119+
EnvelopeItem::ItemContainer(ItemContainer::Logs(_)) => {
120+
RateLimitingCategory::LogItem
121+
}
115122
_ => RateLimitingCategory::Any,
116123
})
117124
})
@@ -131,6 +138,8 @@ pub enum RateLimitingCategory {
131138
Transaction,
132139
/// Rate Limit pertaining to Attachments.
133140
Attachment,
141+
/// Rate Limit pertaining to Log Items.
142+
LogItem,
134143
}
135144

136145
#[cfg(test)]
@@ -145,6 +154,7 @@ mod tests {
145154
assert!(rl.is_disabled(RateLimitingCategory::Error).unwrap() <= Duration::from_secs(120));
146155
assert!(rl.is_disabled(RateLimitingCategory::Session).unwrap() <= Duration::from_secs(60));
147156
assert!(rl.is_disabled(RateLimitingCategory::Transaction).is_none());
157+
assert!(rl.is_disabled(RateLimitingCategory::LogItem).is_none());
148158
assert!(rl.is_disabled(RateLimitingCategory::Any).is_none());
149159

150160
rl.update_from_sentry_header(
@@ -161,6 +171,23 @@ mod tests {
161171
assert!(rl.is_disabled(RateLimitingCategory::Any).unwrap() <= Duration::from_secs(30));
162172
}
163173

174+
#[test]
175+
fn test_sentry_header_no_categories() {
176+
let mut rl = RateLimiter::new();
177+
rl.update_from_sentry_header("120::bar");
178+
179+
assert!(rl.is_disabled(RateLimitingCategory::Error).unwrap() <= Duration::from_secs(120));
180+
assert!(rl.is_disabled(RateLimitingCategory::Session).unwrap() <= Duration::from_secs(120));
181+
assert!(
182+
rl.is_disabled(RateLimitingCategory::Transaction).unwrap() <= Duration::from_secs(120)
183+
);
184+
assert!(rl.is_disabled(RateLimitingCategory::LogItem).unwrap() <= Duration::from_secs(120));
185+
assert!(
186+
rl.is_disabled(RateLimitingCategory::Attachment).unwrap() <= Duration::from_secs(120)
187+
);
188+
assert!(rl.is_disabled(RateLimitingCategory::Any).unwrap() <= Duration::from_secs(120));
189+
}
190+
164191
#[test]
165192
fn test_retry_after() {
166193
let mut rl = RateLimiter::new();

0 commit comments

Comments
 (0)