Skip to content

Commit d0ef365

Browse files
Include SpanContext information in Context Debug (#2365)
Co-authored-by: Cijo Thomas <[email protected]>
1 parent 1da35a9 commit d0ef365

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

opentelemetry/src/context.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,19 @@ impl Context {
327327

328328
impl fmt::Debug for Context {
329329
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
330-
f.debug_struct("Context")
331-
.field("entries", &self.entries.len())
332-
.finish()
330+
let mut dbg = f.debug_struct("Context");
331+
let mut entries = self.entries.len();
332+
#[cfg(feature = "trace")]
333+
{
334+
if let Some(span) = &self.span {
335+
dbg.field("span", &span.span_context());
336+
entries += 1;
337+
} else {
338+
dbg.field("span", &"None");
339+
}
340+
}
341+
342+
dbg.field("entries", &entries).finish()
333343
}
334344
}
335345

opentelemetry/src/trace/context.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ pub(crate) struct SynchronizedSpan {
3232
inner: Option<Mutex<global::BoxedSpan>>,
3333
}
3434

35+
impl SynchronizedSpan {
36+
pub(crate) fn span_context(&self) -> &SpanContext {
37+
&self.span_context
38+
}
39+
}
40+
3541
impl From<SpanContext> for SynchronizedSpan {
3642
fn from(value: SpanContext) -> Self {
3743
Self {

opentelemetry/src/trace/span_context.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ impl SpanContext {
544544
#[cfg(test)]
545545
mod tests {
546546
use super::*;
547+
use crate::{trace::TraceContextExt, Context};
547548

548549
#[rustfmt::skip]
549550
fn trace_id_test_data() -> Vec<(TraceId, &'static str, [u8; 16])> {
@@ -647,4 +648,27 @@ mod tests {
647648
assert!(trace_state.get("testkey").is_none()); // The original state doesn't change
648649
assert_eq!(inserted_trace_state.get("testkey").unwrap(), "testvalue"); //
649650
}
651+
652+
#[test]
653+
fn test_context_span_debug() {
654+
let cx = Context::current();
655+
assert_eq!(
656+
format!("{:?}", cx),
657+
"Context { span: \"None\", entries: 0 }"
658+
);
659+
let cx = Context::current().with_remote_span_context(SpanContext::NONE);
660+
assert_eq!(
661+
format!("{:?}", cx),
662+
"Context { \
663+
span: SpanContext { \
664+
trace_id: 00000000000000000000000000000000, \
665+
span_id: 0000000000000000, \
666+
trace_flags: TraceFlags(0), \
667+
is_remote: false, \
668+
trace_state: TraceState(None) \
669+
}, \
670+
entries: 1 \
671+
}"
672+
);
673+
}
650674
}

0 commit comments

Comments
 (0)