Skip to content

Commit

Permalink
Include SpanContext information in Context Debug (#2365)
Browse files Browse the repository at this point in the history
Co-authored-by: Cijo Thomas <[email protected]>
  • Loading branch information
bantonsson and cijothomas authored Dec 12, 2024
1 parent 1da35a9 commit d0ef365
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
16 changes: 13 additions & 3 deletions opentelemetry/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,19 @@ impl Context {

impl fmt::Debug for Context {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Context")
.field("entries", &self.entries.len())
.finish()
let mut dbg = f.debug_struct("Context");
let mut entries = self.entries.len();
#[cfg(feature = "trace")]
{
if let Some(span) = &self.span {
dbg.field("span", &span.span_context());
entries += 1;
} else {
dbg.field("span", &"None");
}
}

dbg.field("entries", &entries).finish()
}
}

Expand Down
6 changes: 6 additions & 0 deletions opentelemetry/src/trace/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ pub(crate) struct SynchronizedSpan {
inner: Option<Mutex<global::BoxedSpan>>,
}

impl SynchronizedSpan {
pub(crate) fn span_context(&self) -> &SpanContext {
&self.span_context
}
}

impl From<SpanContext> for SynchronizedSpan {
fn from(value: SpanContext) -> Self {
Self {
Expand Down
24 changes: 24 additions & 0 deletions opentelemetry/src/trace/span_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ impl SpanContext {
#[cfg(test)]
mod tests {
use super::*;
use crate::{trace::TraceContextExt, Context};

#[rustfmt::skip]
fn trace_id_test_data() -> Vec<(TraceId, &'static str, [u8; 16])> {
Expand Down Expand Up @@ -647,4 +648,27 @@ mod tests {
assert!(trace_state.get("testkey").is_none()); // The original state doesn't change
assert_eq!(inserted_trace_state.get("testkey").unwrap(), "testvalue"); //
}

#[test]
fn test_context_span_debug() {
let cx = Context::current();
assert_eq!(
format!("{:?}", cx),
"Context { span: \"None\", entries: 0 }"
);
let cx = Context::current().with_remote_span_context(SpanContext::NONE);
assert_eq!(
format!("{:?}", cx),
"Context { \
span: SpanContext { \
trace_id: 00000000000000000000000000000000, \
span_id: 0000000000000000, \
trace_flags: TraceFlags(0), \
is_remote: false, \
trace_state: TraceState(None) \
}, \
entries: 1 \
}"
);
}
}

0 comments on commit d0ef365

Please sign in to comment.