|
42 | 42 | use std::error::Error;
|
43 | 43 | use std::net::IpAddr;
|
44 | 44 | use std::time::Duration;
|
| 45 | + |
| 46 | +use bytes::Bytes; |
45 | 47 | use url::Url;
|
46 | 48 |
|
47 | 49 | use relay_base_schema::project::ProjectId;
|
@@ -80,6 +82,12 @@ const MAX_PROFILE_CHUNK_DURATION: Duration = Duration::from_secs(66);
|
80 | 82 | /// Same format as event IDs.
|
81 | 83 | pub type ProfileId = EventId;
|
82 | 84 |
|
| 85 | +#[derive(Debug, Clone, Copy)] |
| 86 | +pub enum ProfileType { |
| 87 | + Backend, |
| 88 | + Ui, |
| 89 | +} |
| 90 | + |
83 | 91 | #[derive(Debug, Deserialize)]
|
84 | 92 | struct MinimalProfile {
|
85 | 93 | #[serde(alias = "profile_id", alias = "chunk_id")]
|
@@ -275,41 +283,72 @@ pub fn expand_profile(
|
275 | 283 | }
|
276 | 284 | }
|
277 | 285 |
|
278 |
| -pub fn expand_profile_chunk( |
279 |
| - payload: &[u8], |
280 |
| - client_ip: Option<IpAddr>, |
281 |
| - filter_settings: &ProjectFiltersConfig, |
282 |
| - global_config: &GlobalConfig, |
283 |
| -) -> Result<Vec<u8>, ProfileError> { |
284 |
| - let profile = match minimal_profile_from_json(payload) { |
285 |
| - Ok(profile) => profile, |
286 |
| - Err(err) => { |
287 |
| - relay_log::warn!( |
288 |
| - error = &err as &dyn Error, |
289 |
| - from = "minimal", |
290 |
| - "invalid profile chunk", |
291 |
| - ); |
292 |
| - return Err(ProfileError::InvalidJson(err)); |
| 286 | +/// Intermediate type for all processing on a profile chunk. |
| 287 | +pub struct ProfileChunk { |
| 288 | + profile: MinimalProfile, |
| 289 | + payload: Bytes, |
| 290 | +} |
| 291 | + |
| 292 | +impl ProfileChunk { |
| 293 | + /// Parses a new [`Self`] from raw bytes. |
| 294 | + pub fn new(payload: Bytes) -> Result<Self, ProfileError> { |
| 295 | + match minimal_profile_from_json(&payload) { |
| 296 | + Ok(profile) => Ok(Self { profile, payload }), |
| 297 | + Err(err) => { |
| 298 | + relay_log::debug!( |
| 299 | + error = &err as &dyn Error, |
| 300 | + from = "minimal", |
| 301 | + "invalid profile chunk", |
| 302 | + ); |
| 303 | + Err(ProfileError::InvalidJson(err)) |
| 304 | + } |
293 | 305 | }
|
294 |
| - }; |
| 306 | + } |
295 | 307 |
|
296 |
| - if let Err(filter_stat_key) = relay_filter::should_filter( |
297 |
| - &profile, |
298 |
| - client_ip, |
299 |
| - filter_settings, |
300 |
| - global_config.filters(), |
301 |
| - ) { |
302 |
| - return Err(ProfileError::Filtered(filter_stat_key)); |
| 308 | + /// Returns the [`ProfileType`] this chunk belongs to. |
| 309 | + /// |
| 310 | + /// The profile type is currently determined based on the contained profile |
| 311 | + /// platform. It determines the data category this profile chunk belongs to. |
| 312 | + /// |
| 313 | + /// This needs to be synchronized with the implementation in Sentry: |
| 314 | + /// <https://github.com/getsentry/sentry/blob/ed2e1c8bcd0d633e6f828fcfbeefbbdd98ef3dba/src/sentry/profiles/task.py#L995> |
| 315 | + pub fn profile_type(&self) -> ProfileType { |
| 316 | + match self.profile.platform.as_str() { |
| 317 | + "cocoa" | "android" | "javascript" => ProfileType::Ui, |
| 318 | + _ => ProfileType::Backend, |
| 319 | + } |
303 | 320 | }
|
304 | 321 |
|
305 |
| - match (profile.platform.as_str(), profile.version) { |
306 |
| - ("android", _) => android::chunk::parse(payload), |
307 |
| - (_, sample::Version::V2) => { |
308 |
| - let mut profile = sample::v2::parse(payload)?; |
309 |
| - profile.normalize()?; |
310 |
| - serde_json::to_vec(&profile).map_err(|_| ProfileError::CannotSerializePayload) |
| 322 | + /// Applies inbound filters to the profile chunk. |
| 323 | + /// |
| 324 | + /// The profile needs to be filtered (rejected) when this returns an error. |
| 325 | + pub fn filter( |
| 326 | + &self, |
| 327 | + client_ip: Option<IpAddr>, |
| 328 | + filter_settings: &ProjectFiltersConfig, |
| 329 | + global_config: &GlobalConfig, |
| 330 | + ) -> Result<(), ProfileError> { |
| 331 | + relay_filter::should_filter( |
| 332 | + &self.profile, |
| 333 | + client_ip, |
| 334 | + filter_settings, |
| 335 | + global_config.filters(), |
| 336 | + ) |
| 337 | + .map_err(ProfileError::Filtered) |
| 338 | + } |
| 339 | + |
| 340 | + /// Normalizes and 'expands' the profile chunk into its normalized form Sentry expects. |
| 341 | + pub fn expand(&self) -> Result<Vec<u8>, ProfileError> { |
| 342 | + match (self.profile.platform.as_str(), self.profile.version) { |
| 343 | + ("android", _) => android::chunk::parse(&self.payload), |
| 344 | + (_, sample::Version::V2) => { |
| 345 | + let mut profile = sample::v2::parse(&self.payload)?; |
| 346 | + profile.normalize()?; |
| 347 | + Ok(serde_json::to_vec(&profile) |
| 348 | + .map_err(|_| ProfileError::CannotSerializePayload)?) |
| 349 | + } |
| 350 | + (_, _) => Err(ProfileError::PlatformNotSupported), |
311 | 351 | }
|
312 |
| - (_, _) => Err(ProfileError::PlatformNotSupported), |
313 | 352 | }
|
314 | 353 | }
|
315 | 354 |
|
|
0 commit comments