-
Notifications
You must be signed in to change notification settings - Fork 272
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
add utility function for retrieving a trace id #1663
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7a4c0a6
add utility function for retrieving a trace id
garypen a90c013
cleanup the implementation a little
garypen 21e68cd
add PR number
garypen 17a30d1
make sure lint is happy
garypen ef07f43
clean up the TraceId API a little
garypen d0a9e76
Merge branch 'main' into garypen/1536-trace-id
garypen 2959684
Modify TraceId to return an Option when creating
garypen 3f34de8
fix documentation
garypen c8281ea
fix the tests so they work in parallel
garypen 9fb1d9f
add more tests
garypen 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
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//! Trace Ids for the router. | ||
|
||
#![warn(unreachable_pub)] | ||
#![warn(missing_docs)] | ||
use std::fmt; | ||
|
||
use opentelemetry::trace::TraceContextExt; | ||
use tracing::Span; | ||
use tracing_opentelemetry::OpenTelemetrySpanExt; | ||
|
||
/// Trace ID | ||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct TraceId([u8; 16]); | ||
|
||
impl TraceId { | ||
/// Invalid Trace ID | ||
pub const INVALID: TraceId = TraceId([0; 16]); | ||
|
||
/// Create a TraceId. If called from an invalid context | ||
/// (e.g.: not in a span, or in a disabled span), then | ||
/// the value of the TraceId is [`TraceId::INVALID`]. | ||
pub fn new() -> Self { | ||
TraceId( | ||
Span::current() | ||
.context() | ||
.span() | ||
.span_context() | ||
.trace_id() | ||
.to_bytes(), | ||
) | ||
} | ||
|
||
/// Convert the TraceId to bytes. | ||
pub fn as_bytes(&self) -> &[u8; 16] { | ||
&self.0 | ||
} | ||
|
||
/// Convert the TraceId to u128. | ||
pub fn to_u128(&self) -> u128 { | ||
u128::from_be_bytes(self.0) | ||
} | ||
} | ||
|
||
impl Default for TraceId { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl fmt::Display for TraceId { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.to_u128()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use opentelemetry::sdk::export::trace::stdout; | ||
use tracing::span; | ||
use tracing_subscriber::layer::SubscriberExt; | ||
use tracing_subscriber::Registry; | ||
|
||
use super::TraceId; | ||
|
||
#[test] | ||
fn it_returns_invalid_trace_id() { | ||
let my_id = TraceId::new(); | ||
assert_eq!(my_id, TraceId::INVALID); | ||
assert_eq!(my_id.as_bytes(), &[0; 16]) | ||
} | ||
|
||
#[test] | ||
fn it_returns_valid_trace_id() { | ||
// Create a new OpenTelemetry pipeline | ||
let tracer = stdout::new_pipeline().install_simple(); | ||
// Create a tracing layer with the configured tracer | ||
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer); | ||
// Use the tracing subscriber `Registry`, or any other subscriber | ||
// that impls `LookupSpan` | ||
let subscriber = Registry::default().with(telemetry); | ||
// Trace executed code | ||
tracing::subscriber::with_default(subscriber, || { | ||
// Spans will be sent to the configured OpenTelemetry exporter | ||
let root = span!(tracing::Level::TRACE, "trace test"); | ||
let _enter = root.enter(); | ||
|
||
assert!(TraceId::new() != TraceId::INVALID); | ||
}); | ||
} | ||
} |
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.
Would prefer
tracing
as I could see us adding other trace related things in future.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.
I'll change it.
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.
sigh... If I rename to
tracing
it creates several hundred ambiguity errors with thetracing
crate. Probably best to leave astracer
.