-
Notifications
You must be signed in to change notification settings - Fork 103
fix(profiling): Classify profile chunks for rate limits and outcomes #4595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,8 @@ | |
use std::error::Error; | ||
use std::net::IpAddr; | ||
use std::time::Duration; | ||
|
||
use bytes::Bytes; | ||
use url::Url; | ||
|
||
use relay_base_schema::project::ProjectId; | ||
|
@@ -80,6 +82,12 @@ const MAX_PROFILE_CHUNK_DURATION: Duration = Duration::from_secs(66); | |
/// Same format as event IDs. | ||
pub type ProfileId = EventId; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum ProfileType { | ||
Backend, | ||
Ui, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct MinimalProfile { | ||
#[serde(alias = "profile_id", alias = "chunk_id")] | ||
|
@@ -275,41 +283,72 @@ pub fn expand_profile( | |
} | ||
} | ||
|
||
pub fn expand_profile_chunk( | ||
payload: &[u8], | ||
client_ip: Option<IpAddr>, | ||
filter_settings: &ProjectFiltersConfig, | ||
global_config: &GlobalConfig, | ||
) -> Result<Vec<u8>, ProfileError> { | ||
let profile = match minimal_profile_from_json(payload) { | ||
Ok(profile) => profile, | ||
Err(err) => { | ||
relay_log::warn!( | ||
error = &err as &dyn Error, | ||
from = "minimal", | ||
"invalid profile chunk", | ||
); | ||
return Err(ProfileError::InvalidJson(err)); | ||
/// Intermediate type for all processing on a profile chunk. | ||
pub struct ProfileChunk { | ||
profile: MinimalProfile, | ||
payload: Bytes, | ||
} | ||
|
||
impl ProfileChunk { | ||
/// Parses a new [`Self`] from raw bytes. | ||
pub fn new(payload: Bytes) -> Result<Self, ProfileError> { | ||
match minimal_profile_from_json(&payload) { | ||
Ok(profile) => Ok(Self { profile, payload }), | ||
Err(err) => { | ||
relay_log::debug!( | ||
error = &err as &dyn Error, | ||
from = "minimal", | ||
"invalid profile chunk", | ||
); | ||
Err(ProfileError::InvalidJson(err)) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
if let Err(filter_stat_key) = relay_filter::should_filter( | ||
&profile, | ||
client_ip, | ||
filter_settings, | ||
global_config.filters(), | ||
) { | ||
return Err(ProfileError::Filtered(filter_stat_key)); | ||
/// Returns the [`ProfileType`] this chunk belongs to. | ||
/// | ||
/// The profile type is currently determined based on the contained profile | ||
/// platform. It determines the data category this profile chunk belongs to. | ||
/// | ||
/// This needs to be synchronized with the implementation in Sentry: | ||
/// <https://github.com/getsentry/sentry/blob/ed2e1c8bcd0d633e6f828fcfbeefbbdd98ef3dba/src/sentry/profiles/task.py#L995> | ||
pub fn profile_type(&self) -> ProfileType { | ||
match self.profile.platform.as_str() { | ||
"cocoa" | "android" | "javascript" => ProfileType::Ui, | ||
_ => ProfileType::Backend, | ||
} | ||
} | ||
Comment on lines
+315
to
320
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
match (profile.platform.as_str(), profile.version) { | ||
("android", _) => android::chunk::parse(payload), | ||
(_, sample::Version::V2) => { | ||
let mut profile = sample::v2::parse(payload)?; | ||
profile.normalize()?; | ||
serde_json::to_vec(&profile).map_err(|_| ProfileError::CannotSerializePayload) | ||
/// Applies inbound filters to the profile chunk. | ||
/// | ||
/// The profile needs to be filtered (rejected) when this returns an error. | ||
pub fn filter( | ||
&self, | ||
client_ip: Option<IpAddr>, | ||
filter_settings: &ProjectFiltersConfig, | ||
global_config: &GlobalConfig, | ||
) -> Result<(), ProfileError> { | ||
relay_filter::should_filter( | ||
&self.profile, | ||
client_ip, | ||
filter_settings, | ||
global_config.filters(), | ||
) | ||
.map_err(ProfileError::Filtered) | ||
} | ||
|
||
/// Normalizes and 'expands' the profile chunk into its normalized form Sentry expects. | ||
pub fn expand(&self) -> Result<Vec<u8>, ProfileError> { | ||
match (self.profile.platform.as_str(), self.profile.version) { | ||
("android", _) => android::chunk::parse(&self.payload), | ||
(_, sample::Version::V2) => { | ||
let mut profile = sample::v2::parse(&self.payload)?; | ||
profile.normalize()?; | ||
Ok(serde_json::to_vec(&profile) | ||
.map_err(|_| ProfileError::CannotSerializePayload)?) | ||
} | ||
(_, _) => Err(ProfileError::PlatformNotSupported), | ||
} | ||
(_, _) => Err(ProfileError::PlatformNotSupported), | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we can add the
profile_type
in the payload before we send it, this would become the source of truth.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can use add it to the Kafka message https://github.com/getsentry/relay/blob/master/relay-server/src/services/store.rs#L1035-L1041 and the worker will handle it, no need to deserialize/serialize again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that works, let's figure that out in a follow up where we wanna put it (payload / kafka header) and how it should look?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add it to the payload and not the header, it's easier to manipulate later on.