|
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,74 @@ 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 | + // Synchronized with: |
| 317 | + // |
| 318 | + match self.profile.platform.as_str() { |
| 319 | + "cocoa" | "android" | "javascript" => ProfileType::Ui, |
| 320 | + _ => ProfileType::Backend, |
| 321 | + } |
303 | 322 | } |
304 | 323 |
|
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) |
| 324 | + /// Applies inbound filters to the profile chunk. |
| 325 | + /// |
| 326 | + /// The profile needs to be filtered (rejected) when this returns an error. |
| 327 | + pub fn filter( |
| 328 | + &self, |
| 329 | + client_ip: Option<IpAddr>, |
| 330 | + filter_settings: &ProjectFiltersConfig, |
| 331 | + global_config: &GlobalConfig, |
| 332 | + ) -> Result<(), ProfileError> { |
| 333 | + relay_filter::should_filter( |
| 334 | + &self.profile, |
| 335 | + client_ip, |
| 336 | + filter_settings, |
| 337 | + global_config.filters(), |
| 338 | + ) |
| 339 | + .map_err(ProfileError::Filtered) |
| 340 | + } |
| 341 | + |
| 342 | + /// Normalizes and 'expands' the profile chunk into its normalized form Sentry expects. |
| 343 | + pub fn expand(&self) -> Result<Vec<u8>, ProfileError> { |
| 344 | + match (self.profile.platform.as_str(), self.profile.version) { |
| 345 | + ("android", _) => android::chunk::parse(&self.payload), |
| 346 | + (_, sample::Version::V2) => { |
| 347 | + let mut profile = sample::v2::parse(&self.payload)?; |
| 348 | + profile.normalize()?; |
| 349 | + Ok(serde_json::to_vec(&profile) |
| 350 | + .map_err(|_| ProfileError::CannotSerializePayload)?) |
| 351 | + } |
| 352 | + (_, _) => Err(ProfileError::PlatformNotSupported), |
311 | 353 | } |
312 | | - (_, _) => Err(ProfileError::PlatformNotSupported), |
313 | 354 | } |
314 | 355 | } |
315 | 356 |
|
|
0 commit comments