Skip to content

Commit

Permalink
add pretty option to JsonSerializerConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
f1shl3gs committed Jul 13, 2024
1 parent 7f3b6dc commit f6aede1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lib/codecs/src/encoding/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::encoding::format::json::JsonSerializerConfig;
use crate::encoding::TimestampFormat;
use event::log::parse_value_path;

Expand All @@ -188,7 +189,7 @@ except_fields:
- ignore_me
timestamp_format: unix
"##,
SerializerConfig::Json,
SerializerConfig::Json(JsonSerializerConfig { pretty: false }),
Transformer::new(
Some(vec![parse_value_path("a.b[0]").unwrap()]),
Some(vec![parse_value_path("ignore_me").unwrap()]),
Expand Down
37 changes: 29 additions & 8 deletions lib/codecs/src/encoding/format/json.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
use bytes::{BufMut, BytesMut};
use configurable::Configurable;
use event::Event;
use serde::{Deserialize, Serialize};
use tokio_util::codec::Encoder;

use super::SerializeError;

/// Config used to build a `JsonSerializer`
#[derive(Clone, Configurable, Debug, Deserialize, Serialize)]
pub struct JsonSerializerConfig {
/// Whether to use pretty JSON formatting.
#[serde(default)]
pub pretty: bool,
}

/// Serializer that converts an `Event` to bytes using the JSON format.
#[derive(Clone, Debug)]
pub struct JsonSerializer;
pub struct JsonSerializer {
/// Whether to use pretty JSON formatting.
pub pretty: bool,
}

impl JsonSerializer {
/// Creates a new `JsonSerializer`
pub const fn new() -> Self {
JsonSerializer
pub const fn new(pretty: bool) -> Self {
JsonSerializer { pretty }
}
}

Expand All @@ -21,10 +34,18 @@ impl Encoder<Event> for JsonSerializer {
fn encode(&mut self, event: Event, dst: &mut BytesMut) -> Result<(), Self::Error> {
let writer = dst.writer();

match event {
Event::Log(log) => serde_json::to_writer(writer, &log),
Event::Metric(metric) => serde_json::to_writer(writer, &metric),
Event::Trace(trace) => serde_json::to_writer(writer, &trace),
if self.pretty {
match event {
Event::Log(log) => serde_json::to_writer_pretty(writer, &log),
Event::Metric(metric) => serde_json::to_writer_pretty(writer, &metric),
Event::Trace(trace) => serde_json::to_writer_pretty(writer, &trace),
}
} else {
match event {
Event::Log(log) => serde_json::to_writer(writer, &log),
Event::Metric(metric) => serde_json::to_writer(writer, &metric),
Event::Trace(trace) => serde_json::to_writer(writer, &trace),
}
}
.map_err(Into::into)
}
Expand All @@ -41,7 +62,7 @@ mod tests {
let event = Event::from(fields!(
"foo" => "bar"
));
let mut serializer = JsonSerializer;
let mut serializer = JsonSerializer::new(false);
let mut bytes = BytesMut::new();

serializer.encode(event, &mut bytes).unwrap();
Expand Down
8 changes: 6 additions & 2 deletions lib/codecs/src/encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub use framing::{
pub use transformer::{TimestampFormat, Transformer};

use super::FramingError;
pub use crate::encoding::format::json::JsonSerializerConfig;

/// The error returned when serializing a structured event into bytes.
#[derive(Debug)]
Expand Down Expand Up @@ -82,11 +83,14 @@ impl Display for SerializeError {
#[serde(rename_all = "snake_case")]
pub enum SerializerConfig {
/// Configures the `JsonSerializer`
Json,
Json(JsonSerializerConfig),

/// Configures the `LogfmtSerializer`
Logfmt,

/// Configures the `NativeJsonSerializer`
NativeJson,

/// Configures the `TextSerializer`
Text,
}
Expand All @@ -95,7 +99,7 @@ impl SerializerConfig {
/// Build the `Serializer` with this configuration.
pub fn build(&self) -> Serializer {
match self {
SerializerConfig::Json => Serializer::Json(JsonSerializer::new()),
SerializerConfig::Json(config) => Serializer::Json(JsonSerializer::new(config.pretty)),
SerializerConfig::Logfmt => Serializer::Logfmt(LogfmtSerializer::new()),
SerializerConfig::NativeJson => Serializer::Native(NativeJsonSerializer::new()),
SerializerConfig::Text => Serializer::Text(TextSerializer::new()),
Expand Down
2 changes: 1 addition & 1 deletion lib/framework/src/sink/util/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ mod tests {
Transformer::default(),
codecs::Encoder::<Framer>::new(
CharacterDelimitedEncoder::new(b',').into(),
JsonSerializer.into(),
JsonSerializer { pretty: false }.into(),
),
);

Expand Down
10 changes: 5 additions & 5 deletions src/sinks/loki/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ mod tests {
fn encoder_no_labels() {
let mut encoder = EventEncoder {
key_partitioner: KeyPartitioner::new(None),
encoder: Encoder::<()>::new(JsonSerializer::new().into()),
encoder: Encoder::<()>::new(JsonSerializer::new(false).into()),
transformer: Transformer::default(),
labels: HashMap::default(),
remove_label_fields: false,
Expand Down Expand Up @@ -460,7 +460,7 @@ mod tests {

let mut encoder = EventEncoder {
key_partitioner: KeyPartitioner::new(None),
encoder: Encoder::<()>::new(JsonSerializer::new().into()),
encoder: Encoder::<()>::new(JsonSerializer::new(false).into()),
transformer: Transformer::default(),
labels,
remove_label_fields: false,
Expand All @@ -485,7 +485,7 @@ mod tests {
fn encoder_no_ts() {
let mut encoder = EventEncoder {
key_partitioner: KeyPartitioner::new(None),
encoder: Encoder::<()>::new(JsonSerializer::new().into()),
encoder: Encoder::<()>::new(JsonSerializer::new(false).into()),
transformer: Transformer::default(),
labels: HashMap::default(),
remove_label_fields: false,
Expand Down Expand Up @@ -514,7 +514,7 @@ mod tests {

let mut encoder = EventEncoder {
key_partitioner: KeyPartitioner::new(None),
encoder: Encoder::<()>::new(JsonSerializer::new().into()),
encoder: Encoder::<()>::new(JsonSerializer::new(false).into()),
transformer: Transformer::default(),
labels,
remove_label_fields: true,
Expand All @@ -533,7 +533,7 @@ mod tests {
async fn filter_encoder_drop() {
let mut encoder = EventEncoder {
key_partitioner: KeyPartitioner::new(None),
encoder: Encoder::<()>::new(JsonSerializer::new().into()),
encoder: Encoder::<()>::new(JsonSerializer::new(false).into()),
transformer: Transformer::default(),
labels: HashMap::default(),
remove_label_fields: false,
Expand Down
4 changes: 2 additions & 2 deletions tests/syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;

use codecs::encoding::{FramingConfig, SerializerConfig};
use codecs::encoding::{FramingConfig, JsonSerializerConfig, SerializerConfig};
use codecs::EncodingConfigWithFraming;
use framework::sink::util::tcp::TcpSinkConfig;
use framework::testing::CountReceiver;
Expand Down Expand Up @@ -165,7 +165,7 @@ fn tcp_json_sink(address: String) -> Config {
socket::Mode::Tcp(TcpSinkConfig::from_address(address)),
EncodingConfigWithFraming::new(
Some(FramingConfig::NewlineDelimited),
SerializerConfig::Json,
SerializerConfig::Json(JsonSerializerConfig { pretty: false }),
Default::default(),
),
)
Expand Down

0 comments on commit f6aede1

Please sign in to comment.