1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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};

/// Config used to build a `LogfmtSerializer`.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct LogfmtSerializerConfig;

impl LogfmtSerializerConfig {
    /// Creates a new `LogfmtSerializerConfig`.
    pub const fn new() -> Self {
        Self
    }

    /// Build the `LogfmtSerializer` from this configuration.
    pub const fn build(&self) -> LogfmtSerializer {
        LogfmtSerializer
    }

    /// The data type of events that are accepted by `LogfmtSerializer`.
    pub fn input_type(&self) -> DataType {
        DataType::Log
    }

    /// The schema required by the serializer.
    pub fn schema_requirement(&self) -> schema::Requirement {
        // While technically we support `Value` variants that can't be losslessly serialized to
        // logfmt, we don't want to enforce that limitation to users yet.
        schema::Requirement::empty()
    }
}

/// Serializer that converts an `Event` to bytes using the logfmt format.
#[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");
    }
}