Skip to content

Commit

Permalink
Add dynamics field
Browse files Browse the repository at this point in the history
  • Loading branch information
killzoner committed Jan 7, 2024
1 parent 5861fa7 commit de5b9d9
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tracing-logstash/src/logstash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct LogstashFormat<SF = DefaultSpanFormat> {
span_format: SF,
span_fields: Arc<FieldConfig>,
constants: Vec<(&'static str, String)>,
dynamics: fn() -> Vec<(&'static str, String)>,
}

/// Converts a `Level` to a numeric value.
Expand Down Expand Up @@ -130,6 +131,26 @@ impl<SF> LogstashFormat<SF> {
Self { constants, ..self }
}

/// Add a dynamic field to every event.
///
/// # Example
/// ```
/// # use tracing_subscriber::prelude::*;
/// # use time::*;
/// #
/// let logger = tracing_logstash::Layer::default().event_format(
/// tracing_logstash::logstash::LogstashFormat::default().with_dynamics(||
/// vec![
/// ("time_format_rfc2822", OffsetDateTime::now_utc().format(&format_description::well_known::Rfc2822).unwrap_or_default()),
/// ]),
/// );
/// #
/// # let collector = tracing_subscriber::Registry::default().with(logger);
/// ```
pub fn with_dynamics(self, dynamics: fn() -> Vec<(&'static str, String)>) -> Self {
Self { dynamics, ..self }
}

pub fn span_format<FS2>(self, span_format: FS2) -> LogstashFormat<FS2> {
LogstashFormat {
display_version: self.display_version,
Expand All @@ -143,6 +164,7 @@ impl<SF> LogstashFormat<SF> {
span_format,
span_fields: self.span_fields,
constants: self.constants,
dynamics: self.dynamics,
}
}
}
Expand All @@ -161,6 +183,7 @@ impl Default for LogstashFormat {
span_format: Default::default(),
span_fields: Default::default(),
constants: Default::default(),
dynamics: || vec![],
}
}
}
Expand Down Expand Up @@ -300,6 +323,10 @@ where
field_visitor.serialize_field(key, value);
}

for (key, value) in &(self.dynamics)() {
field_visitor.serialize_field(key, value);
}

if let Some(filter) = self.display_span_list {
field_visitor.serialize_field(
"spans",
Expand Down

0 comments on commit de5b9d9

Please sign in to comment.