-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathmod.rs
More file actions
351 lines (297 loc) · 10.4 KB
/
mod.rs
File metadata and controls
351 lines (297 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use std::sync::Arc;
use crate::Envelope;
use crate::envelope::{ContentType, EnvelopeHeaders, Item, ItemType};
use crate::managed::{
Counted, Managed, ManagedEnvelope, ManagedResult as _, OutcomeError, Quantities, Rejected,
};
use crate::processing::errors::errors::SentryError as _;
use crate::processing::utils::event::EventFullyNormalized;
use crate::processing::{self, Context, Forward, Output, QuotaRateLimiter};
use crate::services::outcome::Outcome;
use crate::services::processor::ProcessingError;
use crate::statsd::RelayTimers;
use crate::utils::EnvelopeSummary;
mod dynamic_sampling;
#[allow(
clippy::module_inception,
reason = "all error types of the errors processor"
)]
mod errors;
mod filter;
mod process;
pub use errors::SwitchProcessingError;
use relay_event_normalization::GeoIpLookup;
use relay_event_schema::protocol::{Event, Metrics};
use relay_protocol::Annotated;
use relay_quotas::RateLimits;
type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("envelope processor failed")]
ProcessingFailed(#[from] ProcessingError),
#[error("rate limited")]
RateLimited(RateLimits),
}
impl OutcomeError for Error {
type Error = Error;
fn consume(self) -> (Option<Outcome>, Self::Error) {
let outcome = match &self {
Self::ProcessingFailed(e) => e.to_outcome(),
Self::RateLimited(limits) => {
let reason_code = limits.longest().and_then(|limit| limit.reason_code.clone());
Some(Outcome::RateLimited(reason_code))
}
};
(outcome, self)
}
}
/// A processor for Error Events.
///
/// It processes all kinds of error events, user feedback, crashes, ...
pub struct ErrorsProcessor {
limiter: Arc<QuotaRateLimiter>,
geoip_lookup: GeoIpLookup,
}
impl ErrorsProcessor {
/// Creates a new [`Self`].
pub fn new(limiter: Arc<QuotaRateLimiter>, geoip_lookup: GeoIpLookup) -> Self {
Self {
limiter,
geoip_lookup,
}
}
}
impl processing::Processor for ErrorsProcessor {
type UnitOfWork = SerializedError;
type Output = ErrorOutput;
type Error = Error;
fn prepare_envelope(
&self,
envelope: &mut ManagedEnvelope,
) -> Option<Managed<Self::UnitOfWork>> {
let has_transaction = envelope
.envelope()
.items()
.any(|item| item.ty() == &ItemType::Transaction);
if has_transaction {
return None;
}
let items = envelope
.envelope_mut()
.take_items_by(Item::requires_event)
.into_vec();
if items.is_empty() {
return None;
}
let errors = SerializedError {
headers: envelope.envelope().headers().clone(),
items,
};
Some(Managed::with_meta_from(envelope, errors))
}
async fn process(
&self,
error: Managed<Self::UnitOfWork>,
ctx: Context<'_>,
) -> Result<Output<Self::Output>, Rejected<Self::Error>> {
let mut error = process::expand(error, ctx)?;
process::process(&mut error)?;
process::finalize(&mut error, ctx)?;
process::normalize(&mut error, &self.geoip_lookup, ctx)?;
filter::filter(&error, ctx).reject(&error)?;
dynamic_sampling::apply(&mut error, ctx).await;
let mut error = self.limiter.enforce_quotas(error, ctx).await?;
process::scrub(&mut error, ctx)?;
Ok(Output::just(ErrorOutput(error)))
}
}
#[derive(Debug)]
pub struct SerializedError {
/// Original envelope headers.
headers: EnvelopeHeaders,
/// List of items which can be processed as an error.
///
/// This is a mixture of items all of which return `true` for [`Item::requires_event`].
items: Vec<Item>,
}
impl Counted for SerializedError {
fn quantities(&self) -> Quantities {
EnvelopeSummary::compute_items(self.items.iter()).quantities()
}
}
/// An error envelope which has been expanded.
#[derive(Debug)]
struct ExpandedError {
/// Original envelope headers.
pub headers: EnvelopeHeaders,
/// Whether the payload has been fully normalized.
pub fully_normalized: EventFullyNormalized,
/// Metrics associated with the event.
pub metrics: Metrics,
/// The associated event.
///
/// The event may be [`Annotated::empty`], if expansion of the event is delayed to a later
/// Relay.
///
/// Having no event in a processing Relay must result in an error and the entire event must be
/// discarded.
pub event: Box<Annotated<Event>>,
/// Optional list of attachments sent with the event.
pub attachments: Vec<Item>,
/// Optional list of user reports sent with the event.
pub user_reports: Vec<Item>,
/// Custom event data.
///
/// This may contain elements which are custom to the specific event shape being handled.
pub data: errors::ErrorKind,
/// Forward compatibility, unknown items.
///
/// These items are not rate limited as Relay does not know about the items, so it will also
/// not know how to rate limit them.
/// They are still dropped if the entire event is rate limited/rejected.
///
/// A processing Relay will always discard them, this Relay must know about all item types in
/// use.
pub other: Vec<Item>,
}
impl Counted for ExpandedError {
fn quantities(&self) -> Quantities {
let Self {
headers: _,
fully_normalized: _,
metrics: _,
// Quantity inferred from `data.event_category()`.
event: _,
attachments,
user_reports,
data,
other,
} = self;
let mut quantities = Quantities::default();
quantities.push((data.event_category(), 1));
quantities.extend(attachments.quantities());
quantities.extend(user_reports.quantities());
quantities.extend(data.quantities());
quantities.extend(other.quantities());
quantities
}
}
impl processing::RateLimited for Managed<ExpandedError> {
type Output = Self;
type Error = Error;
async fn enforce<R>(
mut self,
mut rate_limiter: R,
_ctx: processing::Context<'_>,
) -> Result<Self::Output, Rejected<Self::Error>>
where
R: processing::RateLimiter,
{
let scoping = self.scoping();
let limits = rate_limiter
.try_consume(scoping.item(self.data.event_category()), 1)
.await;
if !limits.is_empty() {
return Err(self.reject_err(Error::RateLimited(limits)));
}
for (category, quantity) in self.data.quantities() {
let limits = rate_limiter
.try_consume(scoping.item(category), quantity)
.await;
if !limits.is_empty() {
self.try_modify(|this, records| {
this.data.apply_rate_limit(category, limits, records)
})?;
break;
}
}
for (category, quantity) in self.attachments.quantities() {
let limits = rate_limiter
.try_consume(scoping.item(category), quantity)
.await;
if !limits.is_empty() {
self.modify(|this, record_keeper| {
record_keeper.reject_err(
Error::RateLimited(limits),
std::mem::take(&mut this.attachments),
);
});
break;
}
}
for (category, quantity) in self.user_reports.quantities() {
let limits = rate_limiter
.try_consume(scoping.item(category), quantity)
.await;
if !limits.is_empty() {
self.modify(|this, record_keeper| {
record_keeper.reject_err(
Error::RateLimited(limits),
std::mem::take(&mut this.user_reports),
);
});
break;
}
}
Ok(self)
}
}
#[derive(Debug)]
pub struct ErrorOutput(Managed<ExpandedError>);
impl Forward for ErrorOutput {
fn serialize_envelope(
self,
ctx: processing::ForwardContext<'_>,
) -> Result<Managed<Box<Envelope>>, Rejected<()>> {
self.0
.try_map(|errors, _records| {
let ExpandedError {
headers,
fully_normalized,
metrics: _,
event,
attachments,
user_reports,
data,
other,
} = errors;
let mut items = Vec::with_capacity(1 + attachments.len() + user_reports.len());
if let Some(ev) = event.value() {
let event_type = ev.ty.value().copied().unwrap_or_default();
let mut item = Item::new(ItemType::from_event_type(event_type));
item.set_payload(
ContentType::Json,
relay_statsd::metric!(timer(RelayTimers::EventProcessingSerialization), {
event.to_json().map_err(ProcessingError::SerializeFailed)?
}),
);
if fully_normalized.0 {
item.set_fully_normalized(true);
}
items.push(item);
}
data.serialize_into(&mut items, ctx)?;
items.extend(attachments);
items.extend(user_reports);
if !ctx.config.processing_enabled() {
items.extend(other);
} else {
debug_assert!(other.is_empty());
}
let envelope = Envelope::from_parts(headers, items.into());
Ok::<_, Error>(envelope)
})
.map_err(|err| err.map(|_| ()))
}
#[cfg(feature = "processing")]
fn forward_store(
self,
s: processing::StoreHandle<'_>,
ctx: processing::ForwardContext<'_>,
) -> Result<(), Rejected<()>> {
let envelope = self.serialize_envelope(ctx)?;
let envelope = ManagedEnvelope::from(envelope).into_processed();
processing::utils::store::forward_envelope(envelope, s, ctx.global_config);
Ok(())
}
}