-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathmod.rs
More file actions
186 lines (155 loc) · 5.4 KB
/
mod.rs
File metadata and controls
186 lines (155 loc) · 5.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
use std::sync::Arc;
use relay_profiling::ProfileType;
use relay_quotas::{DataCategory, RateLimits};
use crate::Envelope;
use crate::envelope::{EnvelopeHeaders, Item, ItemType, Items};
use crate::managed::{Counted, Managed, ManagedEnvelope, ManagedResult as _, Quantities, Rejected};
use crate::processing::{self, Context, CountRateLimited, Forward, Output, QuotaRateLimiter};
use crate::services::outcome::{DiscardReason, Outcome};
use smallvec::smallvec;
mod filter;
mod process;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Error raised in [`relay_profiling`].
#[error("Profiling Error: {0}")]
Profiling(#[from] relay_profiling::ProfileError),
/// The profile chunks are rate limited.
#[error("rate limited")]
RateLimited(RateLimits),
/// Profile chunks filtered because of a missing feature flag.
#[error("profile chunks feature flag missing")]
FilterFeatureFlag,
}
impl From<RateLimits> for Error {
fn from(value: RateLimits) -> Self {
Self::RateLimited(value)
}
}
impl crate::managed::OutcomeError for Error {
type Error = Self;
fn consume(self) -> (Option<Outcome>, Self::Error) {
let outcome = match &self {
Self::Profiling(relay_profiling::ProfileError::Filtered(f)) => {
Some(Outcome::Filtered(f.clone()))
}
Self::Profiling(err) => Some(Outcome::Invalid(DiscardReason::Profiling(
relay_profiling::discard_reason(err),
))),
Self::RateLimited(limits) => {
let reason_code = limits.longest().and_then(|limit| limit.reason_code.clone());
Some(Outcome::RateLimited(reason_code))
}
Self::FilterFeatureFlag => None,
};
(outcome, self)
}
}
/// A processor for profile chunks.
///
/// It processes items of type: [`ItemType::ProfileChunk`].
#[derive(Debug)]
pub struct ProfileChunksProcessor {
limiter: Arc<QuotaRateLimiter>,
}
impl ProfileChunksProcessor {
/// Creates a new [`Self`].
pub fn new(limiter: Arc<QuotaRateLimiter>) -> Self {
Self { limiter }
}
}
impl processing::Processor for ProfileChunksProcessor {
type Input = SerializedProfileChunks;
type Output = ProfileChunkOutput;
type Error = Error;
fn prepare_envelope(&self, envelope: &mut ManagedEnvelope) -> Option<Managed<Self::Input>> {
let profile_chunks = envelope
.envelope_mut()
.take_items_by(|item| matches!(*item.ty(), ItemType::ProfileChunk))
.into_vec();
if profile_chunks.is_empty() {
return None;
}
Some(Managed::with_meta_from(
envelope,
SerializedProfileChunks {
headers: envelope.envelope().headers().clone(),
profile_chunks,
},
))
}
async fn process(
&self,
mut profile_chunks: Managed<Self::Input>,
ctx: Context<'_>,
) -> Result<Output<Self::Output>, Rejected<Error>> {
filter::feature_flag(ctx).reject(&profile_chunks)?;
process::process(&mut profile_chunks, ctx);
let profile_chunks = self.limiter.enforce_quotas(profile_chunks, ctx).await?;
Ok(Output::just(ProfileChunkOutput(profile_chunks)))
}
}
/// Output produced by [`ProfileChunksProcessor`].
#[derive(Debug)]
pub struct ProfileChunkOutput(Managed<SerializedProfileChunks>);
impl Forward for ProfileChunkOutput {
fn serialize_envelope(
self,
_: processing::ForwardContext<'_>,
) -> Result<Managed<Box<Envelope>>, Rejected<()>> {
let Self(profile_chunks) = self;
Ok(profile_chunks
.map(|pc, _| Envelope::from_parts(pc.headers, Items::from_vec(pc.profile_chunks))))
}
#[cfg(feature = "processing")]
fn forward_store(
self,
s: processing::forward::StoreHandle<'_>,
ctx: processing::ForwardContext<'_>,
) -> Result<(), Rejected<()>> {
use crate::services::store::StoreProfileChunk;
let Self(profile_chunks) = self;
let retention_days = ctx.event_retention().standard;
for item in profile_chunks.split(|pc| pc.profile_chunks) {
s.send_to_store(item.map(|item, _| StoreProfileChunk {
retention_days,
payload: item.payload(),
quantities: item.quantities(),
}));
}
Ok(())
}
}
/// Serialized profile chunks extracted from an envelope.
#[derive(Debug)]
pub struct SerializedProfileChunks {
/// Original envelope headers.
pub headers: EnvelopeHeaders,
/// List of serialized profile chunk items.
pub profile_chunks: Vec<Item>,
}
impl Counted for SerializedProfileChunks {
fn quantities(&self) -> Quantities {
let mut ui = 0;
let mut backend = 0;
for pc in &self.profile_chunks {
match pc.profile_type() {
Some(ProfileType::Ui) => ui += 1,
Some(ProfileType::Backend) => backend += 1,
None => {}
}
}
let mut quantities = smallvec![];
if ui > 0 {
quantities.push((DataCategory::ProfileChunkUi, ui));
}
if backend > 0 {
quantities.push((DataCategory::ProfileChunk, backend));
}
quantities
}
}
impl CountRateLimited for Managed<SerializedProfileChunks> {
type Error = Error;
}