-
-
Notifications
You must be signed in to change notification settings - Fork 165
feat(core): implement Tracing without Performance #811
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
Open
lcian
wants to merge
12
commits into
master
Choose a base branch
from
lcian/feat/twp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4d38f7f
feat(core): implement Tracing without Performance
lcian d4c2f65
split tests
lcian b4b9484
API adjustments
lcian 0f9e369
final adjustments
lcian 618cf88
Update sentry-core/src/performance.rs
lcian f575567
Update sentry-core/src/performance.rs
lcian e255bfe
Update sentry-core/src/performance.rs
lcian 633c697
Update sentry-types/src/protocol/v7.rs
lcian 6f32d18
Update sentry-types/src/protocol/v7.rs
lcian 3f5fd20
cargo fmt
lcian 2ff1a6c
address feedback
lcian 8cf93b8
more improvements
lcian 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -199,14 +199,12 @@ impl TransactionContext { | |
sentry_trace: &SentryTrace, | ||
span_id: Option<SpanId>, | ||
) -> Self { | ||
let (trace_id, parent_span_id, sampled) = | ||
(sentry_trace.0, Some(sentry_trace.1), sentry_trace.2); | ||
Self { | ||
name: name.into(), | ||
op: op.into(), | ||
trace_id, | ||
parent_span_id, | ||
sampled, | ||
trace_id: sentry_trace.trace_id, | ||
parent_span_id: Some(sentry_trace.span_id), | ||
sampled: sentry_trace.sampled, | ||
span_id: span_id.unwrap_or_default(), | ||
custom: None, | ||
} | ||
|
@@ -476,6 +474,8 @@ impl TransactionOrSpan { | |
} | ||
|
||
/// Returns the headers needed for distributed tracing. | ||
/// Use [`crate::Scope::iter_trace_propagation_headers`] to obtain the active | ||
/// span's/transaction's distributed tracing headers. | ||
pub fn iter_headers(&self) -> TraceHeadersIter { | ||
match self { | ||
TransactionOrSpan::Transaction(transaction) => transaction.iter_headers(), | ||
|
@@ -774,9 +774,11 @@ impl Transaction { | |
} | ||
|
||
/// Returns the headers needed for distributed tracing. | ||
/// Use [`crate::Scope::iter_trace_propagation_headers`] to obtain the active | ||
/// span's/transaction's distributed tracing headers. | ||
pub fn iter_headers(&self) -> TraceHeadersIter { | ||
let inner = self.inner.lock().unwrap(); | ||
let trace = SentryTrace( | ||
let trace = SentryTrace::new( | ||
inner.context.trace_id, | ||
inner.context.span_id, | ||
Some(inner.sampled), | ||
|
@@ -1026,9 +1028,11 @@ impl Span { | |
} | ||
|
||
/// Returns the headers needed for distributed tracing. | ||
/// Use [`crate::Scope::iter_trace_propagation_headers`] to obtain the active | ||
/// span's/transaction's distributed tracing headers. | ||
pub fn iter_headers(&self) -> TraceHeadersIter { | ||
let span = self.span.lock().unwrap(); | ||
let trace = SentryTrace(span.trace_id, span.span_id, Some(self.sampled)); | ||
let trace = SentryTrace::new(span.trace_id, span.span_id, Some(self.sampled)); | ||
TraceHeadersIter { | ||
sentry_trace: Some(trace.to_string()), | ||
} | ||
|
@@ -1125,6 +1129,9 @@ impl Span { | |
} | ||
} | ||
|
||
/// Represents a key-value pair such as an HTTP header. | ||
pub type TraceHeader = (&'static str, String); | ||
|
||
/// An Iterator over HTTP header names and values needed for distributed tracing. | ||
/// | ||
/// This currently only yields the `sentry-trace` header, but other headers | ||
|
@@ -1133,6 +1140,15 @@ pub struct TraceHeadersIter { | |
sentry_trace: Option<String>, | ||
} | ||
|
||
impl TraceHeadersIter { | ||
#[cfg_attr(not(feature = "client"), allow(dead_code))] | ||
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. If |
||
pub(crate) fn new(sentry_trace: String) -> Self { | ||
Self { | ||
sentry_trace: Some(sentry_trace), | ||
} | ||
} | ||
} | ||
|
||
impl Iterator for TraceHeadersIter { | ||
type Item = (&'static str, String); | ||
|
||
|
@@ -1143,8 +1159,12 @@ impl Iterator for TraceHeadersIter { | |
|
||
/// A container for distributed tracing metadata that can be extracted from e.g. the `sentry-trace` | ||
/// HTTP header. | ||
#[derive(Debug, PartialEq)] | ||
pub struct SentryTrace(protocol::TraceId, protocol::SpanId, Option<bool>); | ||
#[derive(Debug, PartialEq, Clone, Copy, Default)] | ||
pub struct SentryTrace { | ||
pub(crate) trace_id: protocol::TraceId, | ||
pub(crate) span_id: protocol::SpanId, | ||
pub(crate) sampled: Option<bool>, | ||
} | ||
|
||
impl SentryTrace { | ||
/// Creates a new [`SentryTrace`] from the provided parameters | ||
|
@@ -1153,7 +1173,11 @@ impl SentryTrace { | |
span_id: protocol::SpanId, | ||
sampled: Option<bool>, | ||
) -> Self { | ||
SentryTrace(trace_id, span_id, sampled) | ||
SentryTrace { | ||
trace_id, | ||
span_id, | ||
sampled, | ||
} | ||
} | ||
} | ||
|
||
|
@@ -1169,7 +1193,7 @@ fn parse_sentry_trace(header: &str) -> Option<SentryTrace> { | |
_ => None, | ||
}); | ||
|
||
Some(SentryTrace(trace_id, parent_span_id, parent_sampled)) | ||
Some(SentryTrace::new(trace_id, parent_span_id, parent_sampled)) | ||
} | ||
|
||
/// Extracts distributed tracing metadata from headers (or, generally, key-value pairs), | ||
|
@@ -1189,8 +1213,8 @@ pub fn parse_headers<'a, I: IntoIterator<Item = (&'a str, &'a str)>>( | |
|
||
impl std::fmt::Display for SentryTrace { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}-{}", self.0, self.1)?; | ||
if let Some(sampled) = self.2 { | ||
write!(f, "{}-{}", self.trace_id, self.span_id)?; | ||
if let Some(sampled) = self.sampled { | ||
write!(f, "-{}", if sampled { '1' } else { '0' })?; | ||
} | ||
Ok(()) | ||
|
@@ -1211,10 +1235,10 @@ mod tests { | |
let trace = parse_sentry_trace("09e04486820349518ac7b5d2adbf6ba5-9cf635fa5b870b3a-0"); | ||
assert_eq!( | ||
trace, | ||
Some(SentryTrace(trace_id, parent_trace_id, Some(false))) | ||
Some(SentryTrace::new(trace_id, parent_trace_id, Some(false))) | ||
); | ||
|
||
let trace = SentryTrace(Default::default(), Default::default(), None); | ||
let trace = SentryTrace::new(Default::default(), Default::default(), None); | ||
let parsed = parse_sentry_trace(&trace.to_string()); | ||
assert_eq!(parsed, Some(trace)); | ||
} | ||
|
@@ -1233,8 +1257,11 @@ mod tests { | |
let header = span.iter_headers().next().unwrap().1; | ||
let parsed = parse_sentry_trace(&header).unwrap(); | ||
|
||
assert_eq!(&parsed.0.to_string(), "09e04486820349518ac7b5d2adbf6ba5"); | ||
assert_eq!(parsed.2, Some(true)); | ||
assert_eq!( | ||
&parsed.trace_id.to_string(), | ||
"09e04486820349518ac7b5d2adbf6ba5" | ||
); | ||
assert_eq!(parsed.sampled, Some(true)); | ||
} | ||
|
||
#[test] | ||
|
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
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.
The idea is that this could also return the ids from PropagationContext if there is no ongoing span, not sure if this comment correctly reflects that.
But on the other hand we should also hide PropagationContext as that's an "implementation detail".