-
Notifications
You must be signed in to change notification settings - Fork 742
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
feat[tracing-subscriber]: extra fields #2733
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -304,6 +304,13 @@ where | |
.serialize_entry("threadId", &format!("{:?}", std::thread::current().id()))?; | ||
} | ||
|
||
#[cfg(feature = "extra-fields")] | ||
if let Some((make_extra_fields, current_span)) = self.make_extra_fields.as_ref().zip(current_span) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should also add an option to line 236 so that the current span is present even if it is not to be logged. |
||
for (k, v) in make_extra_fields(current_span.extensions()) { | ||
serializer.serialize_entry(&k, &v)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this needs to be moved to where
This can be used when the fields are flattened but when not, this will most likely need to be merged with I tried to simplify this by calling |
||
} | ||
} | ||
|
||
serializer.end() | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,11 @@ mod pretty; | |
#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] | ||
pub use pretty::*; | ||
|
||
#[cfg(feature = "extra-fields")] | ||
use crate::registry::Extensions; | ||
#[cfg(feature = "extra-fields")] | ||
use std::collections::HashMap; | ||
|
||
use fmt::{Debug, Display}; | ||
|
||
/// A type that can format a tracing [`Event`] to a [`Writer`]. | ||
|
@@ -413,6 +418,43 @@ pub struct Format<F = Full, T = SystemTime> { | |
pub(crate) display_thread_name: bool, | ||
pub(crate) display_filename: bool, | ||
pub(crate) display_line_number: bool, | ||
#[cfg(feature = "extra-fields")] | ||
pub(crate) make_extra_fields: Option<Box<dyn MakeExtraFields>>, | ||
} | ||
|
||
/// Make extra fields. | ||
#[cfg(feature = "extra-fields")] | ||
pub trait MakeExtraFields: Fn(Extensions<'_>) -> HashMap<String, String> + Send + Sync { | ||
/// Clone self into a boxed [MakeTraceId]. | ||
fn clone_box<'a>(&self) -> Box<dyn 'a + MakeExtraFields> | ||
where | ||
Self: 'a; | ||
} | ||
|
||
#[cfg(feature = "extra-fields")] | ||
impl Debug for Box<dyn MakeExtraFields> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "MakeExtraFields") | ||
} | ||
} | ||
|
||
#[cfg(feature = "extra-fields")] | ||
impl<F> MakeExtraFields for F | ||
where | ||
F: Fn(Extensions<'_>) -> HashMap<String, String> + Clone + Send + Sync { | ||
fn clone_box<'a>(&self) -> Box<dyn 'a + MakeExtraFields> | ||
where | ||
Self: 'a, | ||
{ | ||
Box::new(self.clone()) | ||
} | ||
} | ||
|
||
#[cfg(feature = "extra-fields")] | ||
impl<'a> Clone for Box<dyn 'a + MakeExtraFields> { | ||
fn clone(&self) -> Self { | ||
self.clone_box() | ||
} | ||
} | ||
|
||
// === impl Writer === | ||
|
@@ -603,6 +645,8 @@ impl Default for Format<Full, SystemTime> { | |
display_thread_name: false, | ||
display_filename: false, | ||
display_line_number: false, | ||
#[cfg(feature = "extra-fields")] | ||
make_extra_fields: None, | ||
} | ||
} | ||
} | ||
|
@@ -623,6 +667,8 @@ impl<F, T> Format<F, T> { | |
display_thread_name: self.display_thread_name, | ||
display_filename: self.display_filename, | ||
display_line_number: self.display_line_number, | ||
#[cfg(feature = "extra-fields")] | ||
make_extra_fields: None, | ||
} | ||
} | ||
|
||
|
@@ -662,6 +708,8 @@ impl<F, T> Format<F, T> { | |
display_thread_name: self.display_thread_name, | ||
display_filename: true, | ||
display_line_number: true, | ||
#[cfg(feature = "extra-fields")] | ||
make_extra_fields: None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please take the extra fields from If this sets it to |
||
} | ||
} | ||
|
||
|
@@ -694,6 +742,8 @@ impl<F, T> Format<F, T> { | |
display_thread_name: self.display_thread_name, | ||
display_filename: self.display_filename, | ||
display_line_number: self.display_line_number, | ||
#[cfg(feature = "extra-fields")] | ||
make_extra_fields: None, | ||
} | ||
} | ||
|
||
|
@@ -723,6 +773,8 @@ impl<F, T> Format<F, T> { | |
display_thread_name: self.display_thread_name, | ||
display_filename: self.display_filename, | ||
display_line_number: self.display_line_number, | ||
#[cfg(feature = "extra-fields")] | ||
make_extra_fields: None, | ||
} | ||
} | ||
|
||
|
@@ -739,6 +791,8 @@ impl<F, T> Format<F, T> { | |
display_thread_name: self.display_thread_name, | ||
display_filename: self.display_filename, | ||
display_line_number: self.display_line_number, | ||
#[cfg(feature = "extra-fields")] | ||
make_extra_fields: None, | ||
} | ||
} | ||
|
||
|
@@ -841,6 +895,15 @@ impl<F, T> Format<F, T> { | |
Ok(()) | ||
} | ||
|
||
/// Set the function to make extra fields. | ||
#[cfg(feature = "extra-fields")] | ||
pub fn with_make_extra_fields(self, make_trace_id: Box<dyn MakeExtraFields>) -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this function (and the field on the struct) should be moved to the If it were here, it's too easy to misuse because it does nothing for all formatters except for json. |
||
Format { | ||
make_extra_fields: Some(make_trace_id), | ||
hseeberger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
..self | ||
} | ||
} | ||
|
||
#[inline] | ||
fn format_timestamp(&self, writer: &mut Writer<'_>) -> fmt::Result | ||
where | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, the other methods are named
with_make_extra_fields
, maybe it would be better to use just one of the two names.