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

allow setting hostname. hostname is implicitly skippable #42

Closed
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
13 changes: 10 additions & 3 deletions src/formatting_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn to_bunyan_level(level: &Level) -> u16 {
pub struct BunyanFormattingLayer<W: for<'a> MakeWriter<'a> + 'static> {
make_writer: W,
pid: u32,
hostname: String,
hostname: Option<String>,
bunyan_version: u8,
name: String,
default_fields: HashMap<String, Value>,
Expand Down Expand Up @@ -119,13 +119,18 @@ impl<W: for<'a> MakeWriter<'a> + 'static> BunyanFormattingLayer<W> {
make_writer,
name,
pid: std::process::id(),
hostname: gethostname::gethostname().to_string_lossy().into_owned(),
hostname: Some(gethostname::gethostname().to_string_lossy().into_owned()),
bunyan_version: 0,
default_fields,
skip_fields: HashSet::new(),
}
}

pub fn with_hostname(mut self, hostname: Option<String>) -> Self {
self.hostname = hostname;
self
}

/// Add fields to skip when formatting with this layer.
///
/// It returns an error if you try to skip a required core Bunyan field (e.g. `name`).
Expand Down Expand Up @@ -165,7 +170,9 @@ impl<W: for<'a> MakeWriter<'a> + 'static> BunyanFormattingLayer<W> {
map_serializer.serialize_entry(NAME, &self.name)?;
map_serializer.serialize_entry(MESSAGE, &message)?;
map_serializer.serialize_entry(LEVEL, &to_bunyan_level(level))?;
map_serializer.serialize_entry(HOSTNAME, &self.hostname)?;
if let Some(hostname) = &self.hostname {
map_serializer.serialize_entry(HOSTNAME, hostname)?;
}
map_serializer.serialize_entry(PID, &self.pid)?;
if let Ok(time) = &time::OffsetDateTime::now_utc().format(&Rfc3339) {
map_serializer.serialize_entry(TIME, time)?;
Expand Down
61 changes: 61 additions & 0 deletions tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,67 @@ fn skip_fields() {
}
}

#[test]
fn set_hostname() {
let tracing_output = run_and_get_output(test_action);
let record = tracing_output.first().unwrap();
let default_hostname = record.get("hostname").unwrap().to_string();

let expected_hostname = "banana".to_string();

let buffer = Arc::new(Mutex::new(vec![]));
let buffer_clone = buffer.clone();

let formatting_layer =
BunyanFormattingLayer::new("test".into(), move || MockWriter::new(buffer_clone.clone()))
.with_hostname(Some(expected_hostname.clone()));
let subscriber = Registry::default()
.with(JsonStorageLayer)
.with(formatting_layer);
tracing::subscriber::with_default(subscriber, test_action);

let buffer_guard = buffer.lock().unwrap();
let output = buffer_guard.to_vec();
let output = String::from_utf8(output)
.unwrap()
.lines()
.next()
.map(ToString::to_string)
.unwrap();
let output = output.parse::<Value>().unwrap();
let new_hostname = output["hostname"].as_str().unwrap();

assert_ne!(default_hostname, new_hostname);
assert_eq!(new_hostname, expected_hostname);
}

#[test]
fn skip_hostname() {
let buffer = Arc::new(Mutex::new(vec![]));
let buffer_clone = buffer.clone();

let formatting_layer =
BunyanFormattingLayer::new("test".into(), move || MockWriter::new(buffer_clone.clone()))
.with_hostname(None);

let subscriber = Registry::default()
.with(JsonStorageLayer)
.with(formatting_layer);
tracing::subscriber::with_default(subscriber, test_action);

let buffer_guard = buffer.lock().unwrap();
let output = buffer_guard.to_vec();
let output = String::from_utf8(output)
.unwrap()
.lines()
.next()
.map(ToString::to_string)
.unwrap();
let output = output.parse::<Value>().unwrap();

assert!(output.get("hostname").is_none());
}

#[test]
fn skipping_core_fields_is_not_allowed() {
let skipped_fields = vec!["level"];
Expand Down