forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.rs
More file actions
86 lines (74 loc) · 2.03 KB
/
Copy pathvector.rs
File metadata and controls
86 lines (74 loc) · 2.03 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
use crate::{
config::{DataType, GenerateConfig, SinkConfig, SinkContext, SinkDescription},
event::proto,
sinks::util::tcp::TcpSinkConfig,
tls::TlsConfig,
Event,
};
use bytes::{BufMut, Bytes, BytesMut};
use prost::Message;
use serde::{Deserialize, Serialize};
use snafu::Snafu;
#[derive(Deserialize, Serialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct VectorSinkConfig {
pub address: String,
pub tls: Option<TlsConfig>,
}
#[derive(Debug, Snafu)]
enum BuildError {
#[snafu(display("Missing host in address field"))]
MissingHost,
#[snafu(display("Missing port in address field"))]
MissingPort,
}
inventory::submit! {
SinkDescription::new::<VectorSinkConfig>("vector")
}
impl GenerateConfig for VectorSinkConfig {
fn generate_config() -> toml::Value {
toml::Value::try_from(Self {
address: "127.0.0.1:5000".to_string(),
tls: None,
})
.unwrap()
}
}
#[async_trait::async_trait]
#[typetag::serde(name = "vector")]
impl SinkConfig for VectorSinkConfig {
async fn build(
&self,
cx: SinkContext,
) -> crate::Result<(super::VectorSink, super::Healthcheck)> {
let sink_config = TcpSinkConfig::new(self.address.clone(), self.tls.clone());
sink_config.build(cx, encode_event)
}
fn input_type(&self) -> DataType {
DataType::Any
}
fn sink_type(&self) -> &'static str {
"vector"
}
}
#[derive(Debug, Snafu)]
enum HealthcheckError {
#[snafu(display("Connect error: {}", source))]
ConnectError { source: std::io::Error },
}
fn encode_event(event: Event) -> Option<Bytes> {
let event = proto::EventWrapper::from(event);
let event_len = event.encoded_len();
let full_len = event_len + 4;
let mut out = BytesMut::with_capacity(full_len);
out.put_u32(event_len as u32);
event.encode(&mut out).unwrap();
Some(out.into())
}
#[cfg(test)]
mod test {
#[test]
fn generate_config() {
crate::test_util::test_generate_config::<super::VectorSinkConfig>();
}
}