Skip to content

Commit acf92ab

Browse files
authored
fix: correct SerializeField definition and doc formatting (#3040)
Clippy in 1.80.0 alerted us to the fact that `SerializeField` was never constructed (and due to its non-`pub` member, it can't be constructed outside the `tracing-serde` crate where it's from). This change fixes the definition to hold a reference to a `Field`, which is what the other `Serialize*` types do. It also implements `AsSerde` for this type and uses it inside the `SerializeFieldSet` type. As a bonus, Clippy is now also happy that the type is constructed. The example collector in the `tracing-serde` crate was also renamed from `JsonSubscriber` to `JsonCollector`. Some additional doc formatting issues in `tracing-subscriber` were fixed so that list items that run to multiple lines are correctly indented.
1 parent 1898311 commit acf92ab

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

tracing-serde/src/lib.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@
7171
//! use tracing_serde::AsSerde;
7272
//! use serde_json::json;
7373
//!
74-
//! pub struct JsonSubscriber {
74+
//! pub struct JsonCollector {
7575
//! next_id: AtomicUsize, // you need to assign span IDs, so you need a counter
7676
//! }
7777
//!
78-
//! impl Collect for JsonSubscriber {
78+
//! impl JsonCollector {
79+
//! fn new() -> Self {
80+
//! Self { next_id: 1.into() }
81+
//! }
82+
//! }
83+
//!
84+
//! impl Collect for JsonCollector {
7985
//!
8086
//! fn new_span(&self, attrs: &Attributes<'_>) -> Id {
8187
//! let id = self.next_id.fetch_add(1, Ordering::Relaxed);
@@ -97,7 +103,7 @@
97103
//! }
98104
//!
99105
//! // ...
100-
//! # fn enabled(&self, _: &Metadata<'_>) -> bool { false }
106+
//! # fn enabled(&self, _: &Metadata<'_>) -> bool { true }
101107
//! # fn enter(&self, _: &Id) {}
102108
//! # fn exit(&self, _: &Id) {}
103109
//! # fn record(&self, _: &Id, _: &Record<'_>) {}
@@ -107,7 +113,7 @@
107113
//! ```
108114
//!
109115
//! After you implement your `Collector`, you can use your `tracing`
110-
//! subscriber (`JsonSubscriber` in the above example) to record serialized
116+
//! collector (`JsonCollector` in the above example) to record serialized
111117
//! trace data.
112118
//!
113119
//! ## Crate Feature Flags
@@ -188,9 +194,9 @@ use tracing_core::{
188194
pub mod fields;
189195

190196
#[derive(Debug)]
191-
pub struct SerializeField(Field);
197+
pub struct SerializeField<'a>(&'a Field);
192198

193-
impl Serialize for SerializeField {
199+
impl<'a> Serialize for SerializeField<'a> {
194200
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
195201
where
196202
S: Serializer,
@@ -209,7 +215,7 @@ impl<'a> Serialize for SerializeFieldSet<'a> {
209215
{
210216
let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
211217
for element in self.0 {
212-
seq.serialize_element(element.name())?;
218+
seq.serialize_element(&SerializeField(&element))?;
213219
}
214220
seq.end()
215221
}
@@ -532,6 +538,14 @@ impl<'a> AsSerde<'a> for Level {
532538
}
533539
}
534540

541+
impl<'a> AsSerde<'a> for Field {
542+
type Serializable = SerializeField<'a>;
543+
544+
fn as_serde(&'a self) -> Self::Serializable {
545+
SerializeField(self)
546+
}
547+
}
548+
535549
impl<'a> AsSerde<'a> for FieldSet {
536550
type Serializable = SerializeFieldSet<'a>;
537551

@@ -552,6 +566,8 @@ impl<'a> self::sealed::Sealed for Record<'a> {}
552566

553567
impl<'a> self::sealed::Sealed for Metadata<'a> {}
554568

569+
impl self::sealed::Sealed for Field {}
570+
555571
impl self::sealed::Sealed for FieldSet {}
556572

557573
mod sealed {

tracing-subscriber/src/fmt/fmt_subscriber.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ where
595595
/// # Options
596596
///
597597
/// - [`Subscriber::flatten_event`] can be used to enable flattening event fields into the root
598-
/// object.
598+
/// object.
599599
///
600600
#[cfg(feature = "json")]
601601
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]

tracing-subscriber/src/fmt/format/json.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ use tracing_log::NormalizeEvent;
6060
/// output JSON objects:
6161
///
6262
/// - [`Json::flatten_event`] can be used to enable flattening event fields into
63-
/// the root
63+
/// the root
6464
/// - [`Json::with_current_span`] can be used to control logging of the current
65-
/// span
65+
/// span
6666
/// - [`Json::with_span_list`] can be used to control logging of the span list
67-
/// object.
67+
/// object.
6868
///
6969
/// By default, event fields are not flattened, and both current span and span
7070
/// list are logged.

tracing-subscriber/src/fmt/format/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ impl<F, T> Format<F, T> {
678678
/// # Options
679679
///
680680
/// - [`Format::flatten_event`] can be used to enable flattening event fields into the root
681-
/// object.
681+
/// object.
682682
///
683683
#[cfg(feature = "json")]
684684
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]

0 commit comments

Comments
 (0)