Skip to content

Add dynamic encoder and rename encoder -> encoding in console #371

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

Closed
wants to merge 1 commit into from
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
4 changes: 2 additions & 2 deletions src/sinks/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ pub struct ConsoleSinkConfig {
#[serde(default)]
pub target: Target,
#[serde(default = "default_string_encoder")]
pub encoder: Box<dyn EncoderConfig>,
pub encoding: Box<dyn EncoderConfig>,
}

#[typetag::serde(name = "console")]
impl crate::topology::config::SinkConfig for ConsoleSinkConfig {
fn build(&self, acker: Acker) -> Result<(super::RouterSink, super::Healthcheck), String> {
let encoder = self.encoder.build();
let encoder = self.encoding.build();

let output: Box<dyn io::AsyncWrite + Send> = match self.target {
Target::Stdout => Box::new(io::stdout()),
Expand Down
2 changes: 2 additions & 0 deletions src/sinks/encoders.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod dynamic;
mod json;
mod string;

pub use dynamic::DynamicEncoderConfig;
pub use json::JsonEncoderConfig;
pub use string::StringEncoderConfig;

Expand Down
66 changes: 66 additions & 0 deletions src/sinks/encoders/dynamic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use super::{json::JsonEncoder, string::StringEncoder, Encoder, EncoderConfig};
use crate::event::Event;
use bytes::Bytes;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct DynamicEncoderConfig {}

#[typetag::serde(name = "dynamic")]
impl EncoderConfig for DynamicEncoderConfig {
fn build(&self) -> Box<dyn Encoder + Send> {
Box::new(DynamicEncoder {
json: JsonEncoder {},
string: StringEncoder {},
})
}
}

struct DynamicEncoder {
string: StringEncoder,
json: JsonEncoder,
}

impl Encoder for DynamicEncoder {
fn encode(&self, event: Event) -> Bytes {
if event.as_log().is_structured() {
self.json.encode(event)
} else {
self.string.encode(event)
}
}
}

#[cfg(test)]
mod tests {
use super::DynamicEncoderConfig;
use crate::event::Event;
use crate::sinks::encoders::EncoderConfig;
use std::collections::HashMap;

#[test]
fn dynamic_encoder_uses_string() {
let encoder = DynamicEncoderConfig::default().build();
let event = Event::from("hello world");
let bytes = encoder.encode(event);
let msg = String::from_utf8(bytes.to_vec()).unwrap();

assert_eq!(msg, "hello world".to_string());
}

#[test]
fn dynamic_encoder_uses_json() {
let encoder = DynamicEncoderConfig::default().build();
let mut event = Event::from("hello world");

event
.as_mut_log()
.insert_explicit("key".into(), "value".into());

let bytes = encoder.encode(event);
let map = serde_json::from_slice::<HashMap<String, String>>(&bytes[..]).unwrap();

assert_eq!(map["message"], "hello world".to_string());
assert_eq!(map["key"], "value".to_string());
}
}
2 changes: 1 addition & 1 deletion src/sinks/encoders/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl EncoderConfig for StringEncoderConfig {
}
}

struct StringEncoder {}
pub struct StringEncoder {}

impl Encoder for StringEncoder {
fn encode(&self, event: Event) -> Bytes {
Expand Down