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 1 commit
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
Prev Previous commit
Next Next commit
Use FiniteF64 to simplify the code
  • Loading branch information
phacops committed May 14, 2024
commit bc5f39c6a0e8aead49d3b5a324ba21c50714b98c
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions relay-profiling/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ itertools = { workspace = true }
relay-base-schema = { workspace = true }
relay-event-schema = { workspace = true }
relay-log = { workspace = true }
relay-metrics = { workspace = true }
relay-protocol = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
45 changes: 21 additions & 24 deletions relay-profiling/src/sample/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
//!
use std::collections::{BTreeMap, HashSet};

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

use relay_event_schema::protocol::EventId;
use relay_metrics::FiniteF64;

use crate::error::ProfileError;
use crate::measurements::Measurement;
use crate::sample::{DebugMeta, Frame, ThreadMetadata, Version};
Expand Down Expand Up @@ -41,7 +43,7 @@ pub struct ProfileMetadata {
pub struct Sample {
/// Unix timestamp in seconds with millisecond precision when the sample
/// was captured.
pub timestamp: f64,
pub timestamp: FiniteF64,
/// Index of the stack in the `stacks` field of the profile.
pub stack_id: usize,
/// Thread or queue identifier
Expand All @@ -66,7 +68,7 @@ impl ProfileChunk {
}
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct ProfileData {
/// `samples` contains the list of samples referencing a stack and thread identifier.
/// If 2 stack of frames captured at 2 different timestamps are identical, you're expected to
Expand Down Expand Up @@ -96,7 +98,6 @@ impl ProfileData {
return Err(ProfileError::NotEnoughSamples);
}

// Remove NaN as a side-effect
self.sort_samples_by_timestamp();
phacops marked this conversation as resolved.
Show resolved Hide resolved

if !self.all_stacks_referenced_by_samples_exist() {
Expand Down Expand Up @@ -151,9 +152,7 @@ impl ProfileData {
}

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());
self.samples.sort_by_key(|s| s.timestamp);
}
}

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

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

use crate::sample::v2::{parse, ProfileData, Sample};

#[test]
fn test_roundtrip() {
Expand All @@ -182,32 +183,28 @@ mod tests {
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,
thread_id: "1".into(),
timestamp: FiniteF64::new(2000.0).unwrap(),
},
Sample {
stack_id: 0,
thread_id: "1".to_string(),
timestamp: f64::NAN,
timestamp: FiniteF64::new(1000.0).unwrap(),
},
],
stacks: vec![vec![0]],
frames: vec![Frame {
..Default::default()
}],
thread_metadata: Default::default(),
..Default::default()
};

chunk.sort_samples_by_timestamp();
phacops marked this conversation as resolved.
Show resolved Hide resolved

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

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