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
105 lines (86 loc) · 2.7 KB
/
Copy pathpapertrail.rs
File metadata and controls
105 lines (86 loc) · 2.7 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
use crate::{
event::log_schema,
sinks::util::{
encoding::{EncodingConfig, EncodingConfiguration},
tcp::TcpSink,
Encoding, UriSerde,
},
tls::{MaybeTlsSettings, TlsSettings},
topology::config::{DataType, SinkConfig, SinkContext, SinkDescription},
};
use bytes::Bytes;
use futures01::{stream::iter_ok, Sink};
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>,
}
inventory::submit! {
SinkDescription::new_without_default::<PapertrailConfig>("papertrail")
}
#[typetag::serde(name = "papertrail")]
impl SinkConfig for PapertrailConfig {
fn build(&self, cx: SinkContext) -> crate::Result<(super::RouterSink, super::Healthcheck)> {
let host = self
.endpoint
.host()
.map(str::to_string)
.ok_or_else(|| "A host is required for endpoints".to_string())?;
let port = self
.endpoint
.port_u16()
.ok_or_else(|| "A port is required for endpoints".to_string())?;
let sink = TcpSink::new(
host,
port,
cx.resolver(),
MaybeTlsSettings::Tls(TlsSettings::default()),
);
let healthcheck = sink.healthcheck();
let pid = std::process::id();
let encoding = self.encoding.clone();
let sink = sink.with_flat_map(move |e| iter_ok(encode_event(e, pid, &encoding)));
Ok((Box::new(sink), Box::new(healthcheck)))
}
fn input_type(&self) -> DataType {
DataType::Log
}
fn sink_type(&self) -> &'static str {
"papertrail"
}
}
fn encode_event(
mut event: crate::Event,
pid: u32,
encoding: &EncodingConfig<Encoding>,
) -> Option<Bytes> {
encoding.apply_rules(&mut event);
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();
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))
}