Skip to content

Commit edbf22b

Browse files
authored
subscriber: add nested spans in json formatter (#741)
## Motivation Nested spans are not available in json formatter ## Solution I added a field `spans` in the json, with an array of the nested spans. I reused the span representation from the existing `span`, and pushed all the span representation in a `vec` using `ctx.visit_spans`. I didn't remove the `span` field as it can be useful to have direct access to the current span Fixes: #704
1 parent 7dc37dd commit edbf22b

File tree

4 files changed

+342
-50
lines changed

4 files changed

+342
-50
lines changed

tracing-subscriber/src/fmt/fmt_layer.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,40 @@ impl<S, T, W> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> {
351351
_inner: self._inner,
352352
}
353353
}
354+
355+
/// Sets whether or not the formatter will include the current span in
356+
/// formatted events.
357+
///
358+
/// See [`format::Json`](../fmt/format/struct.Json.html)
359+
pub fn with_current_span(
360+
self,
361+
display_current_span: bool,
362+
) -> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> {
363+
Layer {
364+
fmt_event: self.fmt_event.with_current_span(display_current_span),
365+
fmt_fields: format::JsonFields::new(),
366+
fmt_span: self.fmt_span,
367+
make_writer: self.make_writer,
368+
_inner: self._inner,
369+
}
370+
}
371+
372+
/// Sets whether or not the formatter will include a list (from root to leaf)
373+
/// of all currently entered spans in formatted events.
374+
///
375+
/// See [`format::Json`](../fmt/format/struct.Json.html)
376+
pub fn with_span_list(
377+
self,
378+
display_span_list: bool,
379+
) -> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> {
380+
Layer {
381+
fmt_event: self.fmt_event.with_span_list(display_span_list),
382+
fmt_fields: format::JsonFields::new(),
383+
fmt_span: self.fmt_span,
384+
make_writer: self.make_writer,
385+
_inner: self._inner,
386+
}
387+
}
354388
}
355389

356390
impl<S, N, E, W> Layer<S, N, E, W> {

0 commit comments

Comments
 (0)