-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
csv.rs
184 lines (165 loc) · 6.62 KB
/
csv.rs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use crate::encoding::BuildError;
use bytes::{BufMut, BytesMut};
use chrono::SecondsFormat;
use lookup::lookup_v2::ConfigTargetPath;
use tokio_util::codec::Encoder;
use vector_core::{
config::DataType,
event::{Event, Value},
schema,
};
/// Config used to build a `CsvSerializer`.
#[crate::configurable_component]
#[derive(Debug, Clone)]
pub struct CsvSerializerConfig {
/// The CSV Serializer Options.
pub csv: CsvSerializerOptions,
}
impl CsvSerializerConfig {
/// Creates a new `CsvSerializerConfig`.
pub const fn new(csv: CsvSerializerOptions) -> Self {
Self { csv }
}
/// Build the `CsvSerializer` from this configuration.
pub fn build(&self) -> Result<CsvSerializer, BuildError> {
if self.csv.fields.is_empty() {
Err("At least one CSV field must be specified".into())
} else {
Ok(CsvSerializer::new(self.csv.fields.clone()))
}
}
/// The data type of events that are accepted by `CsvSerializer`.
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
// CSV, we don't want to enforce that limitation to users yet.
schema::Requirement::empty()
}
}
/// Config used to build a `CsvSerializer`.
#[crate::configurable_component]
#[derive(Debug, Clone)]
pub struct CsvSerializerOptions {
/// Configures the fields that will be encoded, as well as the order in which they
/// appear in the output.
///
/// If a field is not present in the event, the output will be an empty string.
///
/// Values of type `Array`, `Object`, and `Regex` are not supported and the
/// output will be an empty string.
pub fields: Vec<ConfigTargetPath>,
}
/// Serializer that converts an `Event` to bytes using the CSV format.
#[derive(Debug, Clone)]
pub struct CsvSerializer {
fields: Vec<ConfigTargetPath>,
}
impl CsvSerializer {
/// Creates a new `CsvSerializer`.
pub const fn new(fields: Vec<ConfigTargetPath>) -> Self {
Self { fields }
}
}
impl Encoder<Event> for CsvSerializer {
type Error = vector_common::Error;
fn encode(&mut self, event: Event, buffer: &mut BytesMut) -> Result<(), Self::Error> {
let log = event.into_log();
let mut wtr = csv::Writer::from_writer(buffer.writer());
for field in &self.fields {
match log.get(field) {
Some(Value::Bytes(bytes)) => {
wtr.write_field(String::from_utf8_lossy(bytes).to_string())?
}
Some(Value::Integer(int)) => wtr.write_field(int.to_string())?,
Some(Value::Float(float)) => wtr.write_field(float.to_string())?,
Some(Value::Boolean(bool)) => wtr.write_field(bool.to_string())?,
Some(Value::Timestamp(timestamp)) => {
wtr.write_field(timestamp.to_rfc3339_opts(SecondsFormat::AutoSi, true))?
}
Some(Value::Null) => wtr.write_field("")?,
// Other value types: Array, Regex, Object are not supported by the CSV format.
Some(_) => wtr.write_field("")?,
None => wtr.write_field("")?,
}
}
wtr.flush()?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use bytes::BytesMut;
use chrono::DateTime;
use ordered_float::NotNan;
use vector_common::btreemap;
use vector_core::event::{LogEvent, Value};
use super::*;
#[test]
fn build_error_on_empty_fields() {
let opts = CsvSerializerOptions { fields: vec![] };
let config = CsvSerializerConfig::new(opts);
let err = config.build().unwrap_err();
assert_eq!(err.to_string(), "At least one CSV field must be specified");
}
#[test]
fn serialize_fields() {
let event = Event::Log(LogEvent::from(btreemap! {
"foo" => Value::from("bar"),
"int" => Value::from(123),
"comma" => Value::from("abc,bcd"),
"float" => Value::Float(NotNan::new(3.1415925).unwrap()),
"space" => Value::from("sp ace"),
"time" => Value::Timestamp(DateTime::parse_from_rfc3339("2023-02-27T15:04:49.363+08:00").unwrap().into()),
"quote" => Value::from("the \"quote\" should be escaped"),
"bool" => Value::from(true),
"other" => Value::from("data"),
}));
let fields = vec![
ConfigTargetPath::try_from("foo".to_string()).unwrap(),
ConfigTargetPath::try_from("int".to_string()).unwrap(),
ConfigTargetPath::try_from("comma".to_string()).unwrap(),
ConfigTargetPath::try_from("float".to_string()).unwrap(),
ConfigTargetPath::try_from("missing".to_string()).unwrap(),
ConfigTargetPath::try_from("space".to_string()).unwrap(),
ConfigTargetPath::try_from("time".to_string()).unwrap(),
ConfigTargetPath::try_from("quote".to_string()).unwrap(),
ConfigTargetPath::try_from("bool".to_string()).unwrap(),
];
let config = CsvSerializerConfig::new(CsvSerializerOptions { fields });
let mut serializer = config.build().unwrap();
let mut bytes = BytesMut::new();
serializer.encode(event, &mut bytes).unwrap();
assert_eq!(
bytes.freeze(),
b"bar,123,\"abc,bcd\",3.1415925,,sp ace,2023-02-27T07:04:49.363Z,\"the \"\"quote\"\" should be escaped\",true".as_slice()
);
}
#[test]
fn serialize_order() {
let event = Event::Log(LogEvent::from(btreemap! {
"field1" => Value::from("value1"),
"field2" => Value::from("value2"),
"field3" => Value::from("value3"),
"field4" => Value::from("value4"),
"field5" => Value::from("value5"),
}));
let fields = vec![
ConfigTargetPath::try_from("field1".to_string()).unwrap(),
ConfigTargetPath::try_from("field5".to_string()).unwrap(),
ConfigTargetPath::try_from("field5".to_string()).unwrap(),
ConfigTargetPath::try_from("field3".to_string()).unwrap(),
ConfigTargetPath::try_from("field2".to_string()).unwrap(),
];
let config = CsvSerializerConfig::new(CsvSerializerOptions { fields });
let mut serializer = config.build().unwrap();
let mut bytes = BytesMut::new();
serializer.encode(event, &mut bytes).unwrap();
assert_eq!(
bytes.freeze(),
b"value1,value5,value5,value3,value2".as_slice()
);
}
}