-
Notifications
You must be signed in to change notification settings - Fork 92
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
fix(breakdown): Account root span in span op breakdown #1213
Closed
Closed
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 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 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 |
---|---|---|
|
@@ -7,7 +7,9 @@ use std::ops::Deref; | |
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::protocol::{Breakdowns, Event, Measurement, Measurements, Timestamp}; | ||
use crate::protocol::{ | ||
Breakdowns, Context, ContextInner, Event, Measurement, Measurements, Span, Timestamp, | ||
}; | ||
use crate::types::Annotated; | ||
|
||
#[derive(Clone, Debug)] | ||
|
@@ -82,6 +84,37 @@ fn get_op_time_spent(mut intervals: Vec<TimeWindowSpan>) -> Option<f64> { | |
Some(op_time_spent) | ||
} | ||
|
||
fn span_from_trace_context(event: &Event) -> Option<Annotated<Span>> { | ||
let contexts = match event.contexts.value() { | ||
Some(contexts) => contexts, | ||
None => return None, | ||
}; | ||
|
||
let trace_context = match contexts.get("trace").map(Annotated::value) { | ||
Some(Some(trace_context)) => trace_context, | ||
_ => return None, | ||
}; | ||
|
||
match trace_context { | ||
ContextInner(Context::Trace(trace_context)) => { | ||
let op_name = match trace_context.op.value() { | ||
None => return None, | ||
Some(op_name) => op_name, | ||
}; | ||
|
||
// Use the trace context to construct the root span that is sufficient for the operation breakdown. | ||
let root_span = Annotated::new(Span { | ||
timestamp: event.timestamp.clone(), | ||
start_timestamp: event.start_timestamp.clone(), | ||
op: Annotated::new(op_name.to_string()), | ||
..Default::default() | ||
}); | ||
Some(root_span) | ||
} | ||
_ => None, | ||
} | ||
} | ||
|
||
/// Emit breakdowns that are derived using information from the given event. | ||
pub trait EmitBreakdowns { | ||
fn emit_breakdowns(&self, event: &Event) -> Option<Measurements>; | ||
|
@@ -105,11 +138,15 @@ impl EmitBreakdowns for SpanOperationsConfig { | |
return None; | ||
} | ||
|
||
let spans = match event.spans.value() { | ||
None => return None, | ||
Some(spans) => spans, | ||
let mut spans = match event.spans.value() { | ||
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. instead of allocating a new vector here, can you extract the loop body into a function, and call it for each entry in |
||
None => vec![], | ||
Some(spans) => spans.to_vec(), | ||
}; | ||
|
||
if let Some(root_span) = span_from_trace_context(event) { | ||
spans.push(root_span); | ||
} | ||
|
||
// Generate span operation breakdowns | ||
let mut intervals = HashMap::new(); | ||
|
||
|
@@ -278,7 +315,7 @@ pub fn normalize_breakdowns(event: &mut Event, breakdowns_config: &BreakdownsCon | |
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::protocol::{EventType, Span, SpanId, SpanStatus, TraceId}; | ||
use crate::protocol::{Contexts, EventType, Span, SpanId, SpanStatus, TraceContext, TraceId}; | ||
use crate::types::Object; | ||
use chrono::{TimeZone, Utc}; | ||
|
||
|
@@ -366,7 +403,24 @@ mod tests { | |
|
||
let mut event = Event { | ||
ty: EventType::Transaction.into(), | ||
start_timestamp: Annotated::new(Utc.ymd(2020, 1, 1).and_hms_nano(0, 0, 0, 0).into()), | ||
timestamp: Annotated::new(Utc.ymd(2020, 1, 1).and_hms_nano(10, 0, 0, 0).into()), | ||
spans: spans.into(), | ||
contexts: Annotated::new(Contexts({ | ||
let mut contexts = Object::new(); | ||
contexts.insert( | ||
"trace".to_owned(), | ||
Annotated::new(ContextInner(Context::Trace(Box::new(TraceContext { | ||
trace_id: Annotated::new(TraceId( | ||
"4c79f60c11214eb38604f4ae0781bfb2".into(), | ||
)), | ||
span_id: Annotated::new(SpanId("fa90fdead5f74053".into())), | ||
op: Annotated::new("db.oracle".to_owned()), | ||
..Default::default() | ||
})))), | ||
); | ||
contexts | ||
})), | ||
..Default::default() | ||
}; | ||
|
||
|
@@ -399,16 +453,16 @@ mod tests { | |
span_ops_breakdown.insert( | ||
"ops.db".to_owned(), | ||
Annotated::new(Measurement { | ||
// 2 hours in milliseconds | ||
value: Annotated::new(7_200_000.0), | ||
// 10 hours in milliseconds | ||
value: Annotated::new(36_000_000.0), | ||
}), | ||
); | ||
|
||
span_ops_breakdown.insert( | ||
"total.time".to_owned(), | ||
Annotated::new(Measurement { | ||
// 4 hours and 10 microseconds in milliseconds | ||
value: Annotated::new(14_400_000.01), | ||
// 12 hours and 10 microseconds in milliseconds | ||
value: Annotated::new(43_200_000.01), | ||
}), | ||
); | ||
|
||
|
This file contains 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 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
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.
Instead of doing this I would create a trait that abstracts over spans and trace contexts, called
SpanLike
. That's probably useful across the codebase.Main reason is re-allocation of strings