From c2c031c60ed0324966747ac01bd741b986c0a9b9 Mon Sep 17 00:00:00 2001 From: David Herberth Date: Tue, 30 Sep 2025 13:41:22 +0200 Subject: [PATCH 1/2] ref(server): Reduce code duplication between ManagedEnvelope and Managed --- relay-server/src/managed/counted.rs | 39 +++++---- relay-server/src/managed/envelope.rs | 125 +-------------------------- 2 files changed, 23 insertions(+), 141 deletions(-) diff --git a/relay-server/src/managed/counted.rs b/relay-server/src/managed/counted.rs index ecdcc34522b..6adb72b7ede 100644 --- a/relay-server/src/managed/counted.rs +++ b/relay-server/src/managed/counted.rs @@ -34,12 +34,16 @@ impl Counted for Item { } impl Counted for Box { + fn quantities(&self) -> Quantities { + EnvelopeSummary::compute(self).quantities() + } +} + +impl Counted for EnvelopeSummary { fn quantities(&self) -> Quantities { let mut quantities = Quantities::new(); - // This matches the implementation of `ManagedEnvelope::reject`. - let summary = EnvelopeSummary::compute(self); - if let Some(category) = summary.event_category { + if let Some(category) = self.event_category { quantities.push((category, 1)); if let Some(category) = category.index_category() { quantities.push((category, 1)); @@ -47,25 +51,22 @@ impl Counted for Box { } let data = [ - (DataCategory::Attachment, summary.attachment_quantity), - (DataCategory::Profile, summary.profile_quantity), - (DataCategory::ProfileIndexed, summary.profile_quantity), - (DataCategory::Span, summary.span_quantity), - (DataCategory::SpanIndexed, summary.span_quantity), + (DataCategory::Attachment, self.attachment_quantity), + (DataCategory::Profile, self.profile_quantity), + (DataCategory::ProfileIndexed, self.profile_quantity), + (DataCategory::Span, self.span_quantity), + (DataCategory::SpanIndexed, self.span_quantity), ( DataCategory::Transaction, - summary.secondary_transaction_quantity, - ), - (DataCategory::Span, summary.secondary_span_quantity), - (DataCategory::Replay, summary.replay_quantity), - (DataCategory::ProfileChunk, summary.profile_chunk_quantity), - ( - DataCategory::ProfileChunkUi, - summary.profile_chunk_ui_quantity, + self.secondary_transaction_quantity, ), - (DataCategory::LogItem, summary.log_item_quantity), - (DataCategory::LogByte, summary.log_byte_quantity), - (DataCategory::Monitor, summary.monitor_quantity), + (DataCategory::Span, self.secondary_span_quantity), + (DataCategory::Replay, self.replay_quantity), + (DataCategory::ProfileChunk, self.profile_chunk_quantity), + (DataCategory::ProfileChunkUi, self.profile_chunk_ui_quantity), + (DataCategory::LogItem, self.log_item_quantity), + (DataCategory::LogByte, self.log_byte_quantity), + (DataCategory::Monitor, self.monitor_quantity), ]; for (category, quantity) in data { diff --git a/relay-server/src/managed/envelope.rs b/relay-server/src/managed/envelope.rs index 8545357d1af..015f814ffe9 100644 --- a/relay-server/src/managed/envelope.rs +++ b/relay-server/src/managed/envelope.rs @@ -10,6 +10,7 @@ use relay_system::Addr; use crate::envelope::{Envelope, Item}; use crate::extractors::RequestMeta; +use crate::managed::Counted; use crate::services::outcome::{DiscardReason, Outcome, TrackOutcome}; use crate::services::processor::{Processed, ProcessingGroup}; use crate::statsd::{RelayCounters, RelayTimers}; @@ -349,128 +350,8 @@ impl ManagedEnvelope { } } - if let Some(category) = self.event_category() { - if let Some(category) = category.index_category() { - self.track_outcome(outcome.clone(), category, 1); - } - self.track_outcome(outcome.clone(), category, 1); - } - - if self.context.summary.attachment_quantity > 0 { - self.track_outcome( - outcome.clone(), - DataCategory::Attachment, - self.context.summary.attachment_quantity, - ); - } - - if self.context.summary.monitor_quantity > 0 { - self.track_outcome( - outcome.clone(), - DataCategory::Monitor, - self.context.summary.monitor_quantity, - ); - } - - if self.context.summary.profile_quantity > 0 { - self.track_outcome( - outcome.clone(), - DataCategory::Profile, - self.context.summary.profile_quantity, - ); - self.track_outcome( - outcome.clone(), - DataCategory::ProfileIndexed, - self.context.summary.profile_quantity, - ); - } - - if self.context.summary.span_quantity > 0 { - self.track_outcome( - outcome.clone(), - DataCategory::Span, - self.context.summary.span_quantity, - ); - self.track_outcome( - outcome.clone(), - DataCategory::SpanIndexed, - self.context.summary.span_quantity, - ); - } - - if self.context.summary.log_item_quantity > 0 { - self.track_outcome( - outcome.clone(), - DataCategory::LogItem, - self.context.summary.log_item_quantity, - ); - } - if self.context.summary.log_byte_quantity > 0 { - self.track_outcome( - outcome.clone(), - DataCategory::LogByte, - self.context.summary.log_byte_quantity, - ); - } - - // Track outcomes for attached secondary transactions, e.g. extracted from metrics. - // - // Primary transaction count is already tracked through the event category - // (see: `Self::event_category()`). - if self.context.summary.secondary_transaction_quantity > 0 { - self.track_outcome( - outcome.clone(), - // Secondary transaction counts are never indexed transactions - DataCategory::Transaction, - self.context.summary.secondary_transaction_quantity, - ); - } - - // Track outcomes for attached secondary spans, e.g. extracted from metrics. - // - // Primary span count is already tracked through `SpanIndexed`. - if self.context.summary.secondary_span_quantity > 0 { - self.track_outcome( - outcome.clone(), - // Secondary transaction counts are never indexed transactions - DataCategory::Span, - self.context.summary.secondary_span_quantity, - ); - } - - if self.context.summary.replay_quantity > 0 { - self.track_outcome( - outcome.clone(), - DataCategory::Replay, - self.context.summary.replay_quantity, - ); - } - - // Track outcomes for user reports, the legacy item type for user feedback. - // - // User reports are not events, but count toward UserReportV2 for quotas and outcomes. - if self.context.summary.user_report_quantity > 0 { - self.track_outcome( - outcome.clone(), - DataCategory::UserReportV2, - self.context.summary.user_report_quantity, - ); - } - - if self.context.summary.profile_chunk_quantity > 0 { - self.track_outcome( - outcome.clone(), - DataCategory::ProfileChunk, - self.context.summary.profile_chunk_quantity, - ); - } - - if self.context.summary.profile_chunk_ui_quantity > 0 { - self.track_outcome( - outcome.clone(), - DataCategory::ProfileChunkUi, - self.context.summary.profile_chunk_ui_quantity, - ); + for (category, quantity) in self.context.summary.quantities() { + self.track_outcome(outcome.clone(), category, quantity); } self.finish(RelayCounters::EnvelopeRejected, handling); From f8ee50533a1d6094f08e87ea95d74d4fac604275 Mon Sep 17 00:00:00 2001 From: David Herberth Date: Tue, 30 Sep 2025 13:51:20 +0200 Subject: [PATCH 2/2] fix missing user report category --- relay-server/src/managed/counted.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/relay-server/src/managed/counted.rs b/relay-server/src/managed/counted.rs index 6adb72b7ede..c42c9945855 100644 --- a/relay-server/src/managed/counted.rs +++ b/relay-server/src/managed/counted.rs @@ -62,6 +62,7 @@ impl Counted for EnvelopeSummary { ), (DataCategory::Span, self.secondary_span_quantity), (DataCategory::Replay, self.replay_quantity), + (DataCategory::UserReportV2, self.user_report_quantity), (DataCategory::ProfileChunk, self.profile_chunk_quantity), (DataCategory::ProfileChunkUi, self.profile_chunk_ui_quantity), (DataCategory::LogItem, self.log_item_quantity),