Skip to content
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

fix: correct SerializeField definition #3040

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions tracing-serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,17 @@
//! use tracing_serde::AsSerde;
//! use serde_json::json;
//!
//! pub struct JsonSubscriber {
//! pub struct JsonCollector {
//! next_id: AtomicUsize, // you need to assign span IDs, so you need a counter
//! }
//!
//! impl Collect for JsonSubscriber {
//! impl JsonCollector {
//! fn new() -> Self {
//! Self { next_id: 1.into() }
//! }
//! }
//!
//! impl Collect for JsonCollector {
//!
//! fn new_span(&self, attrs: &Attributes<'_>) -> Id {
//! let id = self.next_id.fetch_add(1, Ordering::Relaxed);
Expand All @@ -97,7 +103,7 @@
//! }
//!
//! // ...
//! # fn enabled(&self, _: &Metadata<'_>) -> bool { false }
//! # fn enabled(&self, _: &Metadata<'_>) -> bool { true }
//! # fn enter(&self, _: &Id) {}
//! # fn exit(&self, _: &Id) {}
//! # fn record(&self, _: &Id, _: &Record<'_>) {}
Expand All @@ -107,7 +113,7 @@
//! ```
//!
//! After you implement your `Collector`, you can use your `tracing`
//! subscriber (`JsonSubscriber` in the above example) to record serialized
//! collector (`JsonCollector` in the above example) to record serialized
//! trace data.
//!
//! ## Crate Feature Flags
Expand Down Expand Up @@ -188,9 +194,9 @@ use tracing_core::{
pub mod fields;

#[derive(Debug)]
pub struct SerializeField(Field);
pub struct SerializeField<'a>(&'a Field);

impl Serialize for SerializeField {
impl<'a> Serialize for SerializeField<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand All @@ -209,7 +215,7 @@ impl<'a> Serialize for SerializeFieldSet<'a> {
{
let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
for element in self.0 {
seq.serialize_element(element.name())?;
seq.serialize_element(&SerializeField(&element))?;
}
seq.end()
}
Expand Down Expand Up @@ -532,6 +538,14 @@ impl<'a> AsSerde<'a> for Level {
}
}

impl<'a> AsSerde<'a> for Field {
type Serializable = SerializeField<'a>;

fn as_serde(&'a self) -> Self::Serializable {
SerializeField(self)
}
}

impl<'a> AsSerde<'a> for FieldSet {
type Serializable = SerializeFieldSet<'a>;

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

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

impl self::sealed::Sealed for Field {}

impl self::sealed::Sealed for FieldSet {}

mod sealed {
Expand Down
2 changes: 1 addition & 1 deletion tracing-subscriber/src/fmt/fmt_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ where
/// # Options
///
/// - [`Subscriber::flatten_event`] can be used to enable flattening event fields into the root
/// object.
/// object.
///
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
Expand Down
6 changes: 3 additions & 3 deletions tracing-subscriber/src/fmt/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ use tracing_log::NormalizeEvent;
/// output JSON objects:
///
/// - [`Json::flatten_event`] can be used to enable flattening event fields into
/// the root
/// the root
/// - [`Json::with_current_span`] can be used to control logging of the current
/// span
/// span
/// - [`Json::with_span_list`] can be used to control logging of the span list
/// object.
/// object.
///
/// By default, event fields are not flattened, and both current span and span
/// list are logged.
Expand Down
2 changes: 1 addition & 1 deletion tracing-subscriber/src/fmt/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ impl<F, T> Format<F, T> {
/// # Options
///
/// - [`Format::flatten_event`] can be used to enable flattening event fields into the root
/// object.
/// object.
///
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
Expand Down
Loading