-
Notifications
You must be signed in to change notification settings - Fork 99
ref: Use Duration to compute breakdowns and span durations [INGEST-1131] #1260
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1675bde
ref: Use Duration to compute breakdowns and span durations
jan-auer 0060e62
test: Add a test for negative signed_duration_to_millis
jan-auer bda0b73
ref: Simplify
jan-auer a104202
ref: Use nanosecond precision as per test expectation
jan-auer a38e2c8
ref: Rename function
jan-auer 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
use std::collections::HashMap; | ||
use std::ops::Deref; | ||
use std::time::Duration; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
|
@@ -12,76 +13,50 @@ use relay_common::{DurationUnit, MetricUnit}; | |
use crate::protocol::{Breakdowns, Event, Measurement, Measurements, Timestamp}; | ||
use crate::types::Annotated; | ||
|
||
#[derive(Clone, Debug)] | ||
#[derive(Clone, Copy, Debug)] | ||
pub struct TimeWindowSpan { | ||
pub start: Timestamp, | ||
pub end: Timestamp, | ||
} | ||
|
||
impl TimeWindowSpan { | ||
pub fn new(start: Timestamp, end: Timestamp) -> Self { | ||
pub fn new(mut start: Timestamp, mut end: Timestamp) -> Self { | ||
if end < start { | ||
return TimeWindowSpan { | ||
start: end, | ||
end: start, | ||
}; | ||
std::mem::swap(&mut start, &mut end); | ||
} | ||
|
||
TimeWindowSpan { start, end } | ||
} | ||
|
||
pub fn duration(&self) -> f64 { | ||
let delta: f64 = (self.end.timestamp_nanos() - self.start.timestamp_nanos()) as f64; | ||
// convert to milliseconds (1 ms = 1,000,000 nanoseconds) | ||
(delta / 1_000_000.00).abs() | ||
pub fn duration(&self) -> Duration { | ||
// Cannot fail since durations are ordered in the constructor | ||
(self.end - self.start).to_std().unwrap_or_default() | ||
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. nit: We could also make |
||
} | ||
} | ||
|
||
#[derive(PartialEq, Eq, Hash)] | ||
#[derive(Debug, Eq, Hash, PartialEq)] | ||
enum OperationBreakdown<'a> { | ||
Emit(&'a str), | ||
DoNotEmit(&'a str), | ||
} | ||
|
||
fn get_op_time_spent(mut intervals: Vec<TimeWindowSpan>) -> Option<f64> { | ||
if intervals.is_empty() { | ||
return None; | ||
} | ||
|
||
// sort by start timestamp in ascending order | ||
fn get_operation_duration(mut intervals: Vec<TimeWindowSpan>) -> Duration { | ||
intervals.sort_unstable_by_key(|span| span.start); | ||
|
||
let mut op_time_spent = 0.0; | ||
let mut previous_interval: Option<TimeWindowSpan> = None; | ||
|
||
for current_interval in intervals.into_iter() { | ||
match previous_interval.as_mut() { | ||
Some(last_interval) => { | ||
if last_interval.end < current_interval.start { | ||
// if current_interval does not overlap with last_interval, | ||
// then add last_interval to op_time_spent | ||
op_time_spent += last_interval.duration(); | ||
previous_interval = Some(current_interval); | ||
continue; | ||
} | ||
let mut duration = Duration::new(0, 0); | ||
let mut last_end = None; | ||
|
||
// current_interval and last_interval overlaps; so we merge these intervals | ||
|
||
// invariant: last_interval.start <= current_interval.start | ||
|
||
last_interval.end = std::cmp::max(last_interval.end, current_interval.end); | ||
} | ||
None => { | ||
previous_interval = Some(current_interval); | ||
} | ||
}; | ||
} | ||
for mut interval in intervals { | ||
if let Some(cutoff) = last_end { | ||
// ensure the current interval doesn't overlap with the last one | ||
interval = TimeWindowSpan::new(interval.start.max(cutoff), interval.end.max(cutoff)); | ||
} | ||
|
||
if let Some(remaining_interval) = previous_interval { | ||
op_time_spent += remaining_interval.duration(); | ||
duration += interval.duration(); | ||
last_end = Some(interval.end); | ||
} | ||
|
||
Some(op_time_spent) | ||
duration | ||
} | ||
|
||
/// Emit breakdowns that are derived using information from the given event. | ||
|
@@ -101,9 +76,7 @@ pub struct SpanOperationsConfig { | |
|
||
impl EmitBreakdowns for SpanOperationsConfig { | ||
fn emit_breakdowns(&self, event: &Event) -> Option<Measurements> { | ||
let operation_name_breakdowns = &self.matches; | ||
|
||
if operation_name_breakdowns.is_empty() { | ||
if self.matches.is_empty() { | ||
return None; | ||
} | ||
|
||
|
@@ -121,81 +94,58 @@ impl EmitBreakdowns for SpanOperationsConfig { | |
Some(span) => span, | ||
}; | ||
|
||
let operation_name = match span.op.value() { | ||
let name = match span.op.as_str() { | ||
None => continue, | ||
Some(span_op) => span_op, | ||
}; | ||
|
||
let start = match span.start_timestamp.value() { | ||
None => continue, | ||
Some(start) => start, | ||
let interval = match (span.start_timestamp.value(), span.timestamp.value()) { | ||
(Some(start), Some(end)) => TimeWindowSpan::new(*start, *end), | ||
_ => continue, | ||
}; | ||
|
||
let end = match span.timestamp.value() { | ||
None => continue, | ||
Some(end) => end, | ||
}; | ||
|
||
let cover = TimeWindowSpan::new(*start, *end); | ||
|
||
// Only emit an operation breakdown measurement if the operation name matches any | ||
// entries in operation_name_breakdown. | ||
let results = operation_name_breakdowns | ||
.iter() | ||
.find(|maybe| operation_name.starts_with(*maybe)); | ||
|
||
let operation_name = match results { | ||
None => OperationBreakdown::DoNotEmit(operation_name), | ||
Some(operation_name) => OperationBreakdown::Emit(operation_name), | ||
let key = match self.matches.iter().find(|n| name.starts_with(*n)) { | ||
Some(op_name) => OperationBreakdown::Emit(op_name), | ||
None => OperationBreakdown::DoNotEmit(name), | ||
}; | ||
|
||
intervals | ||
.entry(operation_name) | ||
.or_insert_with(Vec::new) | ||
.push(cover); | ||
intervals.entry(key).or_insert_with(Vec::new).push(interval); | ||
} | ||
|
||
if intervals.is_empty() { | ||
return None; | ||
} | ||
|
||
let mut breakdown = Measurements::default(); | ||
let mut total_time = Duration::new(0, 0); | ||
|
||
let mut total_time_spent = 0.0; | ||
|
||
for (operation_name, intervals) in intervals { | ||
// TODO(ja): Convert measurements in here, use `Duration` as typed carrier. | ||
// use `get_metric_measurement_unit` from metric extraction for this (move!) | ||
let op_time_spent = match get_op_time_spent(intervals) { | ||
None => continue, | ||
Some(op_time_spent) => op_time_spent, | ||
}; | ||
for (key, intervals) in intervals { | ||
if intervals.is_empty() { | ||
continue; | ||
} | ||
|
||
total_time_spent += op_time_spent; | ||
let op_duration = get_operation_duration(intervals); | ||
total_time += op_duration; | ||
|
||
let operation_name = match operation_name { | ||
let operation_name = match key { | ||
OperationBreakdown::Emit(name) => name, | ||
OperationBreakdown::DoNotEmit(_) => continue, | ||
OperationBreakdown::Emit(operation_name) => operation_name, | ||
}; | ||
|
||
let time_spent_measurement = Measurement { | ||
value: Annotated::new(op_time_spent), | ||
let op_value = Measurement { | ||
value: Annotated::new(relay_common::duration_to_millis(op_duration)), | ||
unit: Annotated::new(MetricUnit::Duration(DurationUnit::MilliSecond)), | ||
}; | ||
|
||
let op_breakdown_name = format!("ops.{}", operation_name); | ||
|
||
breakdown.insert(op_breakdown_name, Annotated::new(time_spent_measurement)); | ||
breakdown.insert(op_breakdown_name, Annotated::new(op_value)); | ||
} | ||
|
||
let total_time_spent_measurement = Measurement { | ||
value: Annotated::new(total_time_spent), | ||
let total_time_value = Annotated::new(Measurement { | ||
value: Annotated::new(relay_common::duration_to_millis(total_time)), | ||
unit: Annotated::new(MetricUnit::Duration(DurationUnit::MilliSecond)), | ||
}; | ||
breakdown.insert( | ||
"total.time".to_string(), | ||
Annotated::new(total_time_spent_measurement), | ||
); | ||
}); | ||
breakdown.insert("total.time".to_string(), total_time_value); | ||
|
||
Some(breakdown) | ||
} | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.