Skip to content
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

ref(profiles): Ensure UUIDs for chunk and profiler and sort samples #3588

Merged
merged 5 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Emit negative outcomes when metrics are rejected because of a disabled namespace. ([#3544](https://github.com/getsentry/relay/pull/3544))
- Add AI model costs to global config. ([#3579](https://github.com/getsentry/relay/pull/3579))
- Add support for `event.` in the `Span` `Getter` implementation. ([#3577](https://github.com/getsentry/relay/pull/3577))
- Ensure `chunk_id` and `profiler_id` are UUIDs and sort samples. ([#3588](https://github.com/getsentry/relay/pull/3588))

## 24.4.2

Expand Down
51 changes: 48 additions & 3 deletions relay-profiling/src/sample/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//!
use std::collections::{BTreeMap, HashSet};

use relay_event_schema::protocol::EventId;
use serde::{Deserialize, Serialize};

use crate::error::ProfileError;
Expand All @@ -20,9 +21,9 @@ use crate::sample::{DebugMeta, Frame, ThreadMetadata, Version};
#[derive(Debug, Serialize, Deserialize)]
pub struct ProfileMetadata {
/// Random UUID identifying a chunk
pub chunk_id: String,
pub chunk_id: EventId,
/// Random UUID for each profiler session
pub profiler_id: String,
pub profiler_id: EventId,

#[serde(default, skip_serializing_if = "DebugMeta::is_empty")]
pub debug_meta: DebugMeta,
Expand Down Expand Up @@ -95,6 +96,9 @@ impl ProfileData {
return Err(ProfileError::NotEnoughSamples);
}

// Remove NaN as a side-effect
self.sort_samples_by_timestamp();

if !self.all_stacks_referenced_by_samples_exist() {
return Err(ProfileError::MalformedSamples);
}
Expand Down Expand Up @@ -145,6 +149,12 @@ impl ProfileData {
self.thread_metadata
.retain(|thread_id, _| thread_ids.contains(thread_id));
}

fn sort_samples_by_timestamp(&mut self) {
self.samples.retain(|s| !s.timestamp.is_nan());
self.samples
.sort_by(|a, b| a.timestamp.partial_cmp(&b.timestamp).unwrap());
}
}

pub fn parse(payload: &[u8]) -> Result<ProfileChunk, ProfileError> {
Expand All @@ -154,7 +164,7 @@ pub fn parse(payload: &[u8]) -> Result<ProfileChunk, ProfileError> {

#[cfg(test)]
mod tests {
use crate::sample::v2::parse;
use crate::sample::v2::{parse, Frame, ProfileData, Sample};

#[test]
fn test_roundtrip() {
Expand All @@ -165,4 +175,39 @@ mod tests {
let second_parse = parse(&second_payload[..]);
assert!(second_parse.is_ok(), "{:#?}", second_parse);
}

#[test]
fn test_samples_sorted() {
let mut chunk = ProfileData {
samples: vec![
Sample {
stack_id: 0,
thread_id: "1".to_string(),
timestamp: 2000.0,
},
Sample {
stack_id: 0,
thread_id: "1".to_string(),
timestamp: 1000.0,
},
Sample {
stack_id: 0,
thread_id: "1".to_string(),
timestamp: f64::NAN,
},
],
stacks: vec![vec![0]],
frames: vec![Frame {
..Default::default()
}],
thread_metadata: Default::default(),
};

chunk.sort_samples_by_timestamp();

let timestamps: Vec<f64> = chunk.samples.iter().map(|s| s.timestamp).collect();

assert_eq!(chunk.samples.len(), 2);
assert_eq!(timestamps, vec![1000.0, 2000.0]);
}
}
Loading