|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use crate::services::outcome::{DiscardReason, Outcome, TrackOutcome}; |
| 4 | +use crate::services::processor::{EnvelopeProcessor, SubmitClientReports}; |
| 5 | +use crate::utils::SleepHandle; |
| 6 | +use hashbrown::HashMap; |
| 7 | +use relay_config::Config; |
| 8 | +use relay_event_schema::protocol::{ClientReport, DiscardedEvent}; |
| 9 | +use relay_metrics::{MetricNamespace, UnixTimestamp}; |
| 10 | +use relay_quotas::Scoping; |
| 11 | +use relay_system::{Addr, Service}; |
| 12 | + |
| 13 | +/// Service implementing the [`TrackOutcome`] interface only emitting [`ClientReport`]s. |
| 14 | +/// |
| 15 | +/// This implementation is only useful for Relay instances running in Proxy mode. |
| 16 | +#[derive(Debug)] |
| 17 | +pub struct ClientReportOutcomeProducerService { |
| 18 | + processor: Addr<EnvelopeProcessor>, |
| 19 | + buckets: HashMap<BucketKey, ClientReport>, |
| 20 | + bucket_interval: u32, |
| 21 | + flush_interval: u32, |
| 22 | + flush_handle: SleepHandle, |
| 23 | +} |
| 24 | + |
| 25 | +impl ClientReportOutcomeProducerService { |
| 26 | + pub fn new(config: &Config, processor: Addr<EnvelopeProcessor>) -> Self { |
| 27 | + let agg = &config |
| 28 | + .aggregator_config_for(MetricNamespace::Outcomes) |
| 29 | + .aggregator; |
| 30 | + |
| 31 | + Self { |
| 32 | + processor, |
| 33 | + buckets: Default::default(), |
| 34 | + bucket_interval: agg.bucket_interval, |
| 35 | + flush_interval: agg.initial_delay, |
| 36 | + flush_handle: SleepHandle::idle(), |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + fn handle_message(&mut self, msg: TrackOutcome) { |
| 41 | + let offset = |
| 42 | + u64::try_from(msg.timestamp.timestamp()).unwrap_or(0) / u64::from(self.bucket_interval); |
| 43 | + |
| 44 | + let bucket_key = BucketKey { |
| 45 | + offset, |
| 46 | + scoping: msg.scoping, |
| 47 | + }; |
| 48 | + |
| 49 | + let discarded_events: fn(&mut ClientReport) -> &mut _ = match msg.outcome { |
| 50 | + Outcome::Filtered(_) => |cr| &mut cr.filtered_events, |
| 51 | + Outcome::FilteredSampling(_) => |cr| &mut cr.filtered_sampling_events, |
| 52 | + Outcome::RateLimited(_) => |cr| &mut cr.rate_limited_events, |
| 53 | + Outcome::Invalid(DiscardReason::InvalidSignature | DiscardReason::MissingSignature) => { |
| 54 | + |cr| &mut cr.discarded_events |
| 55 | + } |
| 56 | + _ => { |
| 57 | + relay_log::debug!( |
| 58 | + "Outcome '{}' cannot be converted to client report", |
| 59 | + msg.outcome |
| 60 | + ); |
| 61 | + return; |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + let client_report = self.buckets.entry(bucket_key).or_default(); |
| 66 | + |
| 67 | + discarded_events(client_report).push(DiscardedEvent { |
| 68 | + reason: msg.outcome.to_reason().unwrap_or_default().to_string(), |
| 69 | + category: msg.category, |
| 70 | + quantity: msg.quantity, |
| 71 | + }); |
| 72 | + |
| 73 | + match self.flush_interval { |
| 74 | + 0 => self.do_flush(), |
| 75 | + v => self.flush_handle.set_if_idle(Duration::from_secs(v.into())), |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + fn do_flush(&mut self) { |
| 80 | + for (bucket_key, client_report) in self.buckets.drain() { |
| 81 | + let BucketKey { offset, scoping } = bucket_key; |
| 82 | + |
| 83 | + let timestamp = Some(offset) |
| 84 | + // May be zero as we default timestamps out of range to 0. |
| 85 | + .filter(|offset| *offset > 0) |
| 86 | + .map(|offset| offset * u64::from(self.bucket_interval)) |
| 87 | + .map(UnixTimestamp::from_secs); |
| 88 | + |
| 89 | + let client_report = ClientReport { |
| 90 | + timestamp, |
| 91 | + ..client_report |
| 92 | + }; |
| 93 | + |
| 94 | + self.processor.send(SubmitClientReports { |
| 95 | + client_reports: vec![client_report], |
| 96 | + scoping, |
| 97 | + }); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + fn handle_shutdown(&mut self) { |
| 102 | + self.flush_interval = 0; |
| 103 | + self.do_flush(); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl Service for ClientReportOutcomeProducerService { |
| 108 | + type Interface = TrackOutcome; |
| 109 | + |
| 110 | + async fn run(mut self, mut rx: relay_system::Receiver<Self::Interface>) { |
| 111 | + let mut shutdown = relay_system::Controller::shutdown_handle(); |
| 112 | + relay_log::info!("client report outcome producer started"); |
| 113 | + |
| 114 | + loop { |
| 115 | + tokio::select! { |
| 116 | + biased; |
| 117 | + |
| 118 | + () = &mut self.flush_handle => self.do_flush(), |
| 119 | + Some(message) = rx.recv() => self.handle_message(message), |
| 120 | + _ = shutdown.notified() => self.handle_shutdown(), |
| 121 | + |
| 122 | + else => break, |
| 123 | + } |
| 124 | + } |
| 125 | + self.do_flush(); |
| 126 | + relay_log::info!("client report outcome producer stopped"); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/// Contains everything to construct a `TrackOutcome`, except quantity |
| 131 | +#[derive(Debug, PartialEq, Eq, Hash)] |
| 132 | +struct BucketKey { |
| 133 | + /// The time slot for which outcomes are aggregated. |
| 134 | + /// |
| 135 | + /// The offset follows the formula: `timestamp = offset * bucket_interval`. |
| 136 | + offset: u64, |
| 137 | + /// Scoping of the outcome. |
| 138 | + scoping: Scoping, |
| 139 | +} |
0 commit comments