use bytes::BytesMut;
use serde::{Deserialize, Serialize};
use tokio_util::codec::Encoder;
use vector_common::encode_logfmt;
use vector_core::{config::DataType, event::Event, schema};
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct LogfmtSerializerConfig;
impl LogfmtSerializerConfig {
pub const fn new() -> Self {
Self
}
pub const fn build(&self) -> LogfmtSerializer {
LogfmtSerializer
}
pub fn input_type(&self) -> DataType {
DataType::Log
}
pub fn schema_requirement(&self) -> schema::Requirement {
schema::Requirement::empty()
}
}
#[derive(Debug, Clone)]
pub struct LogfmtSerializer;
impl Encoder<Event> for LogfmtSerializer {
type Error = vector_common::Error;
fn encode(&mut self, event: Event, buffer: &mut BytesMut) -> Result<(), Self::Error> {
let log = event.as_log();
let string = encode_logfmt::encode_value(log.value())?;
buffer.extend_from_slice(string.as_bytes());
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use bytes::BytesMut;
use vector_core::event::{LogEvent, Value};
use vrl::btreemap;
#[test]
fn serialize_logfmt() {
let event = Event::Log(LogEvent::from(btreemap! {
"foo" => Value::from("bar")
}));
let mut serializer = LogfmtSerializer;
let mut bytes = BytesMut::new();
serializer.encode(event, &mut bytes).unwrap();
assert_eq!(bytes.freeze(), "foo=bar");
}
}