-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathmod.rs
More file actions
289 lines (243 loc) · 8.34 KB
/
mod.rs
File metadata and controls
289 lines (243 loc) · 8.34 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
use std::sync::Arc;
use relay_event_schema::processor::ProcessingAction;
use relay_event_schema::protocol::OurLog;
use relay_filter::FilterStatKey;
use relay_quotas::{DataCategory, RateLimits};
use crate::Envelope;
use crate::envelope::{
ContainerItems, ContainerWriteError, EnvelopeHeaders, Item, ItemContainer, ItemType, Items,
};
use crate::integrations::Integration;
use crate::managed::{
Counted, Managed, ManagedEnvelope, ManagedResult as _, OutcomeError, Quantities,
};
use crate::processing::{
self, Context, CountRateLimited, Forward, Output, QuotaRateLimiter, Rejected,
};
use crate::services::outcome::{DiscardItemType, DiscardReason, Outcome};
mod filter;
mod integrations;
mod process;
#[cfg(feature = "processing")]
mod store;
mod utils;
mod validate;
pub use self::utils::get_calculated_byte_size;
type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// A duplicated item container for logs.
#[error("duplicate log container")]
DuplicateContainer,
/// Received log exceeds the configured size limit.
#[error("log exeeds size limit")]
TooLarge,
/// Logs filtered because of a missing feature flag.
#[error("logs feature flag missing")]
FilterFeatureFlag,
/// Logs filtered due to a filtering rule.
#[error("log filtered")]
Filtered(FilterStatKey),
/// The logs are rate limited.
#[error("rate limited")]
RateLimited(RateLimits),
/// A processor failed to process the logs.
#[error("envelope processor failed")]
ProcessingFailed(#[from] ProcessingAction),
/// The log is invalid.
#[error("invalid: {0}")]
Invalid(DiscardReason),
}
impl OutcomeError for Error {
type Error = Self;
fn consume(self) -> (Option<Outcome>, Self::Error) {
let outcome = match &self {
Self::DuplicateContainer => Some(Outcome::Invalid(DiscardReason::DuplicateItem)),
Self::TooLarge => Some(Outcome::Invalid(DiscardReason::TooLarge(
DiscardItemType::Log,
))),
Self::FilterFeatureFlag => None,
Self::Filtered(f) => Some(Outcome::Filtered(f.clone())),
Self::RateLimited(limits) => {
let reason_code = limits.longest().and_then(|limit| limit.reason_code.clone());
Some(Outcome::RateLimited(reason_code))
}
Self::ProcessingFailed(_) => Some(Outcome::Invalid(DiscardReason::Internal)),
Self::Invalid(reason) => Some(Outcome::Invalid(*reason)),
};
(outcome, self)
}
}
impl From<RateLimits> for Error {
fn from(value: RateLimits) -> Self {
Self::RateLimited(value)
}
}
/// A processor for Logs.
///
/// It processes items of type: [`ItemType::Log`].
#[derive(Debug)]
pub struct LogsProcessor {
limiter: Arc<QuotaRateLimiter>,
}
impl LogsProcessor {
/// Creates a new [`Self`].
pub fn new(limiter: Arc<QuotaRateLimiter>) -> Self {
Self { limiter }
}
}
impl processing::Processor for LogsProcessor {
type Input = SerializedLogs;
type Output = LogOutput;
type Error = Error;
fn prepare_envelope(&self, envelope: &mut ManagedEnvelope) -> Option<Managed<Self::Input>> {
let headers = envelope.envelope().headers().clone();
let logs = envelope
.envelope_mut()
.take_items_by(|item| matches!(*item.ty(), ItemType::Log))
.into_vec();
// TODO: there might be a better way to extract an item and its integration type type safe.
// So later we don't have a fallible conversion for the integration.
//
// Maybe a 2 phase thing where we take items, then grab the integration again and debug
// assert + return if the impossible happens.
let integrations = envelope
.envelope_mut()
.take_items_by(|item| matches!(item.integration(), Some(Integration::Logs(_))))
.into_vec();
let work = SerializedLogs {
headers,
logs,
integrations,
};
Some(Managed::with_meta_from(envelope, work))
}
async fn process(
&self,
logs: Managed<Self::Input>,
ctx: Context<'_>,
) -> Result<Output<Self::Output>, Rejected<Error>> {
validate::container(&logs).reject(&logs)?;
validate::dsc(&logs);
// Fast filters, which do not need expanded logs.
filter::feature_flag(ctx).reject(&logs)?;
let mut logs = process::expand(logs);
validate::size(&mut logs, ctx);
process::normalize(&mut logs, ctx);
filter::filter(&mut logs, ctx);
let mut logs = self.limiter.enforce_quotas(logs, ctx).await?;
process::scrub(&mut logs, ctx);
Ok(Output::just(LogOutput(logs)))
}
}
/// Output produced by [`LogsProcessor`].
#[derive(Debug)]
pub struct LogOutput(Managed<ExpandedLogs>);
impl Forward for LogOutput {
fn serialize_envelope(
self,
_: processing::ForwardContext<'_>,
) -> Result<Managed<Box<Envelope>>, Rejected<()>> {
self.0.try_map(|logs, r| {
r.lenient(DataCategory::LogByte);
logs.serialize_envelope()
.map_err(drop)
.with_outcome(Outcome::Invalid(DiscardReason::Internal))
})
}
#[cfg(feature = "processing")]
fn forward_store(
self,
s: processing::StoreHandle<'_>,
ctx: processing::ForwardContext<'_>,
) -> Result<(), Rejected<()>> {
let Self(logs) = self;
let ctx = store::Context {
scoping: logs.scoping(),
received_at: logs.received_at(),
retention: ctx.retention(|r| r.log.as_ref()),
};
for log in logs.split(|logs| logs.logs) {
if let Ok(log) = log.try_map(|log, _| store::convert(log, &ctx)) {
s.send_to_store(log)
};
}
Ok(())
}
}
/// Logs in their serialized state, as transported in an envelope.
#[derive(Debug)]
pub struct SerializedLogs {
/// Original envelope headers.
headers: EnvelopeHeaders,
/// Logs are sent in item containers, there is specified limit of a single container per
/// envelope.
///
/// But at this point this has not yet been validated.
logs: Vec<Item>,
/// Logs which Relay received from arbitrary integrations.
integrations: Vec<Item>,
}
impl SerializedLogs {
fn items(&self) -> impl Iterator<Item = &Item> {
self.logs.iter().chain(self.integrations.iter())
}
/// Returns the total count of all logs contained.
///
/// This contains all logical log items, not just envelope items and is safe
/// to use for rate limiting.
fn count(&self) -> usize {
self.items()
.map(|item| item.item_count().unwrap_or(1) as usize)
.sum()
}
/// Returns the sum of bytes of all logs contained.
fn bytes(&self) -> usize {
self.items().map(|item| item.len()).sum()
}
}
impl Counted for SerializedLogs {
fn quantities(&self) -> Quantities {
smallvec::smallvec![
(DataCategory::LogItem, self.count()),
(DataCategory::LogByte, self.bytes())
]
}
}
impl CountRateLimited for Managed<SerializedLogs> {
type Error = Error;
}
/// Logs which have been parsed and expanded from their serialized state.
#[derive(Debug)]
pub struct ExpandedLogs {
/// Original envelope headers.
headers: EnvelopeHeaders,
/// Expanded and parsed logs.
logs: ContainerItems<OurLog>,
}
impl Counted for ExpandedLogs {
fn quantities(&self) -> Quantities {
let count = self.logs.len();
let bytes = self.logs.iter().map(get_calculated_byte_size).sum();
smallvec::smallvec![
(DataCategory::LogItem, count),
(DataCategory::LogByte, bytes)
]
}
}
impl ExpandedLogs {
fn serialize_envelope(self) -> Result<Box<Envelope>, ContainerWriteError> {
let mut logs = Vec::new();
if !self.logs.is_empty() {
let mut item = Item::new(ItemType::Log);
ItemContainer::from(self.logs)
.write_to(&mut item)
.inspect_err(|err| relay_log::error!("failed to serialize logs: {err}"))?;
logs.push(item);
}
Ok(Envelope::from_parts(self.headers, Items::from_vec(logs)))
}
}
impl CountRateLimited for Managed<ExpandedLogs> {
type Error = Error;
}