From 119f2de66607818933456b4337733abb0ff1deb2 Mon Sep 17 00:00:00 2001 From: Giannis Gkiortzis Date: Thu, 11 Sep 2025 11:30:17 +0200 Subject: [PATCH 1/6] add rate limit for logs --- sentry/src/transports/ratelimit.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sentry/src/transports/ratelimit.rs b/sentry/src/transports/ratelimit.rs index 3cadfa057..a47f3ae1a 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,10 @@ impl RateLimiter { } EnvelopeItem::Transaction(_) => RateLimitingCategory::Transaction, EnvelopeItem::Attachment(_) => RateLimitingCategory::Attachment, + EnvelopeItem::ItemContainer(container) => match container { + ItemContainer::Logs(_) => RateLimitingCategory::LogItem, + _ => RateLimitingCategory::Any, + }, _ => RateLimitingCategory::Any, }) }) @@ -131,6 +139,8 @@ pub enum RateLimitingCategory { Transaction, /// Rate Limit pertaining to Attachments. Attachment, + /// Rate Limit pertaining to Log Items. + LogItem, } #[cfg(test)] @@ -145,6 +155,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( From 806c7754db4d3054ccfde15f740980eadf53f825 Mon Sep 17 00:00:00 2001 From: Giannis Gkiortzis Date: Thu, 11 Sep 2025 12:14:55 +0200 Subject: [PATCH 2/6] add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f6d89558..750c57b89 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. +- add rate limits for logs ([#894](https://github.com/getsentry/sentry-rust/pull/894)) by @giortzisg ## 0.42.0 From 09d62841378217781331fcfbd455210a2728e795 Mon Sep 17 00:00:00 2001 From: Giannis Gkiortzis Date: Thu, 11 Sep 2025 12:23:10 +0200 Subject: [PATCH 3/6] fix lint --- sentry/src/transports/ratelimit.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sentry/src/transports/ratelimit.rs b/sentry/src/transports/ratelimit.rs index a47f3ae1a..c9aae3ad7 100644 --- a/sentry/src/transports/ratelimit.rs +++ b/sentry/src/transports/ratelimit.rs @@ -116,10 +116,9 @@ impl RateLimiter { } EnvelopeItem::Transaction(_) => RateLimitingCategory::Transaction, EnvelopeItem::Attachment(_) => RateLimitingCategory::Attachment, - EnvelopeItem::ItemContainer(container) => match container { - ItemContainer::Logs(_) => RateLimitingCategory::LogItem, - _ => RateLimitingCategory::Any, - }, + EnvelopeItem::ItemContainer(ItemContainer::Logs(_)) => { + RateLimitingCategory::LogItem + } _ => RateLimitingCategory::Any, }) }) From f31b59b338b46b788706c2ce0d1d3b76b3cfddd6 Mon Sep 17 00:00:00 2001 From: Lorenzo Cian Date: Thu, 11 Sep 2025 12:32:28 +0200 Subject: [PATCH 4/6] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 750c57b89..a95c7f332 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,7 +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. -- add rate limits for logs ([#894](https://github.com/getsentry/sentry-rust/pull/894)) by @giortzisg +- fix(transport): add rate limits for logs ([#894](https://github.com/getsentry/sentry-rust/pull/894)) by @giortzisg ## 0.42.0 From 3ac889e3b9c20c19b2cd6d3bf5a9231ef5629276 Mon Sep 17 00:00:00 2001 From: Giannis Gkiortzis Date: Thu, 11 Sep 2025 13:20:51 +0200 Subject: [PATCH 5/6] add no categories test --- sentry/src/transports/ratelimit.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/sentry/src/transports/ratelimit.rs b/sentry/src/transports/ratelimit.rs index c9aae3ad7..c799b8647 100644 --- a/sentry/src/transports/ratelimit.rs +++ b/sentry/src/transports/ratelimit.rs @@ -171,6 +171,19 @@ 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(); From 3a3d82e8710621182ae82c175bf6cf13a60ad3a2 Mon Sep 17 00:00:00 2001 From: Giannis Gkiortzis Date: Thu, 11 Sep 2025 13:24:20 +0200 Subject: [PATCH 6/6] fix lint :) --- sentry/src/transports/ratelimit.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sentry/src/transports/ratelimit.rs b/sentry/src/transports/ratelimit.rs index c799b8647..fbe053590 100644 --- a/sentry/src/transports/ratelimit.rs +++ b/sentry/src/transports/ratelimit.rs @@ -178,9 +178,13 @@ mod tests { 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::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::Attachment).unwrap() <= Duration::from_secs(120) + ); assert!(rl.is_disabled(RateLimitingCategory::Any).unwrap() <= Duration::from_secs(120)); }