forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpapertrail.rs
More file actions
142 lines (119 loc) · 3.74 KB
/
Copy pathpapertrail.rs
File metadata and controls
142 lines (119 loc) · 3.74 KB
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
use crate::{
config::{log_schema, DataType, GenerateConfig, SinkConfig, SinkContext, SinkDescription},
sinks::util::{
encoding::{EncodingConfig, EncodingConfiguration},
tcp::TcpSinkConfig,
Encoding, UriSerde,
},
tls::TlsConfig,
Event,
};
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use syslog::{Facility, Formatter3164, LogFormat, Severity};
#[derive(Deserialize, Serialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct PapertrailConfig {
endpoint: UriSerde,
encoding: EncodingConfig<Encoding>,
tls: Option<TlsConfig>,
}
inventory::submit! {
SinkDescription::new::<PapertrailConfig>("papertrail")
}
impl GenerateConfig for PapertrailConfig {
fn generate_config() -> toml::Value {
toml::from_str(
r#"endpoint = "logs.papertrailapp.com:12345"
encoding.codec = "json""#,
)
.unwrap()
}
}
#[async_trait::async_trait]
#[typetag::serde(name = "papertrail")]
impl SinkConfig for PapertrailConfig {
async fn build(
&self,
cx: SinkContext,
) -> crate::Result<(super::VectorSink, super::Healthcheck)> {
let host = self
.endpoint
.host()
.map(str::to_string)
.ok_or_else(|| "A host is required for endpoint".to_string())?;
let port = self
.endpoint
.port_u16()
.ok_or_else(|| "A port is required for endpoint".to_string())?;
let address = format!("{}:{}", host, port);
let tls = Some(self.tls.clone().unwrap_or_else(TlsConfig::enabled));
let pid = std::process::id();
let encoding = self.encoding.clone();
let sink_config = TcpSinkConfig::new(address, tls);
sink_config.build(cx, move |event| encode_event(event, pid, &encoding))
}
fn input_type(&self) -> DataType {
DataType::Log
}
fn sink_type(&self) -> &'static str {
"papertrail"
}
}
fn encode_event(mut event: Event, pid: u32, encoding: &EncodingConfig<Encoding>) -> Option<Bytes> {
let host = if let Some(host) = event.as_mut_log().remove(log_schema().host_key()) {
Some(host.to_string_lossy())
} else {
None
};
let formatter = Formatter3164 {
facility: Facility::LOG_USER,
hostname: host,
process: "vector".into(),
pid: pid as i32,
};
let mut s: Vec<u8> = Vec::new();
encoding.apply_rules(&mut event);
let log = event.into_log();
let message = match encoding.codec() {
Encoding::Json => serde_json::to_string(&log).unwrap(),
Encoding::Text => log
.get(log_schema().message_key())
.map(|v| v.to_string_lossy())
.unwrap_or_default(),
};
formatter
.format(&mut s, Severity::LOG_INFO, message)
.unwrap();
s.push(b'\n');
Some(Bytes::from(s))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn generate_config() {
crate::test_util::test_generate_config::<PapertrailConfig>();
}
#[test]
fn encode_event_apply_rules() {
let mut evt = Event::from("vector");
evt.as_mut_log().insert("magic", "key");
let bytes = encode_event(
evt,
0,
&EncodingConfig {
codec: Encoding::Json,
schema: None,
only_fields: None,
except_fields: Some(vec!["magic".into()]),
timestamp_format: None,
},
)
.unwrap();
let msg =
bytes.slice(String::from_utf8_lossy(&bytes).find(": ").unwrap() + 2..bytes.len() - 1);
let value: serde_json::Value = serde_json::from_slice(&msg).unwrap();
assert!(!value.as_object().unwrap().contains_key("magic"));
}
}