Skip to content

Commit

Permalink
Merge pull request #2 from danielbcorreia/support-constant-attributes
Browse files Browse the repository at this point in the history
Add support for constant attributes
  • Loading branch information
gsson authored Sep 14, 2023
2 parents 5ce72f0 + 9e1515d commit fd2ef2f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion examples/examples/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ async fn shutdown_signal() {

#[tokio::main]
async fn main() {
let logger = tracing_logstash::Layer::default();
let logger = tracing_logstash::Layer::default()
.event_format(tracing_logstash::logstash::LogstashFormat::default()
.with_constants(vec![
("service.name", "tracing-logstash".to_owned()),
("service.environment", "development".to_owned()),
])
);

let env_filter = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("info"))
Expand Down
13 changes: 13 additions & 0 deletions tracing-logstash/src/logstash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct LogstashFormat<SF = DefaultSpanFormat> {
display_stack_trace: Option<(DisplayLevelFilter, DisplayLevelFilter)>,
span_format: SF,
span_fields: Arc<FieldConfig>,
constants: Vec<(&'static str, String)>,
}

const fn level_value(level: &Level) -> u64 {
Expand Down Expand Up @@ -89,12 +90,18 @@ impl<SF> LogstashFormat<SF> {
..self
}
}

pub fn with_span_fields(self, span_fields: Vec<FieldSpec>) -> Self {
Self {
span_fields: Arc::new(FieldConfig::new(span_fields)),
..self
}
}

pub fn with_constants(self, constants: Vec<(&'static str, String)>) -> Self {
Self { constants, ..self }
}

pub fn span_format<FS2>(self, span_format: FS2) -> LogstashFormat<FS2> {
LogstashFormat {
display_version: self.display_version,
Expand All @@ -107,6 +114,7 @@ impl<SF> LogstashFormat<SF> {
display_span_list: self.display_span_list,
span_format,
span_fields: self.span_fields,
constants: self.constants,
}
}
}
Expand All @@ -124,6 +132,7 @@ impl Default for LogstashFormat {
display_span_list: None,
span_format: Default::default(),
span_fields: Default::default(),
constants: Default::default(),
}
}
}
Expand Down Expand Up @@ -258,6 +267,10 @@ where
}
}

for (key, value) in &self.constants {
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 fd2ef2f

Please sign in to comment.