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

Add support for constant attributes #2

Merged
merged 1 commit into from
Sep 14, 2023
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
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