diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f6d8955..a95c7f33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ - build(panic): Fix build without other dependencies ([#883](https://github.com/getsentry/sentry-rust/pull/883)) by @liskin - The `sentry-panic` crate now builds successfully when used as a standalone dependency. +- fix(transport): add rate limits for logs ([#894](https://github.com/getsentry/sentry-rust/pull/894)) by @giortzisg ## 0.42.0 diff --git a/sentry/src/transports/ratelimit.rs b/sentry/src/transports/ratelimit.rs index 3cadfa05..fbe05359 100644 --- a/sentry/src/transports/ratelimit.rs +++ b/sentry/src/transports/ratelimit.rs @@ -2,6 +2,7 @@ use httpdate::parse_http_date; use std::time::{Duration, SystemTime}; use crate::protocol::EnvelopeItem; +use crate::protocol::ItemContainer; use crate::Envelope; /// A Utility that helps with rate limiting sentry requests. @@ -12,6 +13,7 @@ pub struct RateLimiter { session: Option, transaction: Option, attachment: Option, + log_item: Option, } impl RateLimiter { @@ -56,6 +58,7 @@ impl RateLimiter { "session" => self.session = new_time, "transaction" => self.transaction = new_time, "attachment" => self.attachment = new_time, + "log_item" => self.log_item = new_time, _ => {} } } @@ -89,6 +92,7 @@ impl RateLimiter { RateLimitingCategory::Session => self.session, RateLimitingCategory::Transaction => self.transaction, RateLimitingCategory::Attachment => self.attachment, + RateLimitingCategory::LogItem => self.log_item, }?; time_left.duration_since(SystemTime::now()).ok() } @@ -112,6 +116,9 @@ impl RateLimiter { } EnvelopeItem::Transaction(_) => RateLimitingCategory::Transaction, EnvelopeItem::Attachment(_) => RateLimitingCategory::Attachment, + EnvelopeItem::ItemContainer(ItemContainer::Logs(_)) => { + RateLimitingCategory::LogItem + } _ => RateLimitingCategory::Any, }) }) @@ -131,6 +138,8 @@ pub enum RateLimitingCategory { Transaction, /// Rate Limit pertaining to Attachments. Attachment, + /// Rate Limit pertaining to Log Items. + LogItem, } #[cfg(test)] @@ -145,6 +154,7 @@ mod tests { assert!(rl.is_disabled(RateLimitingCategory::Error).unwrap() <= Duration::from_secs(120)); assert!(rl.is_disabled(RateLimitingCategory::Session).unwrap() <= Duration::from_secs(60)); assert!(rl.is_disabled(RateLimitingCategory::Transaction).is_none()); + assert!(rl.is_disabled(RateLimitingCategory::LogItem).is_none()); assert!(rl.is_disabled(RateLimitingCategory::Any).is_none()); rl.update_from_sentry_header( @@ -161,6 +171,23 @@ mod tests { assert!(rl.is_disabled(RateLimitingCategory::Any).unwrap() <= Duration::from_secs(30)); } + #[test] + fn test_sentry_header_no_categories() { + let mut rl = RateLimiter::new(); + rl.update_from_sentry_header("120::bar"); + + assert!(rl.is_disabled(RateLimitingCategory::Error).unwrap() <= Duration::from_secs(120)); + assert!(rl.is_disabled(RateLimitingCategory::Session).unwrap() <= Duration::from_secs(120)); + assert!( + rl.is_disabled(RateLimitingCategory::Transaction).unwrap() <= Duration::from_secs(120) + ); + assert!(rl.is_disabled(RateLimitingCategory::LogItem).unwrap() <= Duration::from_secs(120)); + assert!( + rl.is_disabled(RateLimitingCategory::Attachment).unwrap() <= Duration::from_secs(120) + ); + assert!(rl.is_disabled(RateLimitingCategory::Any).unwrap() <= Duration::from_secs(120)); + } + #[test] fn test_retry_after() { let mut rl = RateLimiter::new();