forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
95 lines (87 loc) · 2.69 KB
/
Copy pathmod.rs
File metadata and controls
95 lines (87 loc) · 2.69 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
pub mod adaptive_concurrency;
pub mod batch;
pub mod buffer;
pub mod encoding;
pub mod http;
pub mod retries;
pub mod service;
pub mod sink;
pub mod socket_bytes_sink;
pub mod statistic;
pub mod tcp;
#[cfg(test)]
pub mod test;
pub mod udp;
#[cfg(all(any(feature = "sinks-socket", feature = "sinks-statsd"), unix))]
pub mod unix;
pub mod uri;
use crate::event::Event;
use bytes::Bytes;
use encoding::{EncodingConfig, EncodingConfiguration};
use serde::{Deserialize, Serialize};
use snafu::Snafu;
use std::borrow::Cow;
pub use batch::{Batch, BatchConfig, BatchSettings, BatchSize, PushResult};
pub use buffer::json::{BoxedRawValue, JsonArrayBuffer};
pub use buffer::metrics::{MetricBuffer, MetricEntry};
pub use buffer::partition::Partition;
pub use buffer::vec::{EncodedLength, VecBuffer};
pub use buffer::{Buffer, Compression, PartitionBuffer, PartitionInnerBuffer};
pub use service::{
Concurrency, ServiceBuilderExt, TowerBatchedSink, TowerPartitionSink, TowerRequestConfig,
TowerRequestLayer, TowerRequestSettings,
};
pub use sink::{BatchSink, PartitionBatchSink, StreamSink};
pub use uri::UriSerde;
#[derive(Debug, Snafu)]
enum SinkBuildError {
#[snafu(display("Missing host in address field"))]
MissingHost,
#[snafu(display("Missing port in address field"))]
MissingPort,
}
/**
* Enum representing different ways to encode events as they are sent into a Sink.
*/
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Encoding {
Text,
Json,
}
/**
* Encodes the given event into raw bytes that can be sent into a Sink, according to
* the given encoding. If there are any errors encoding the event, logs a warning
* and returns None.
**/
pub fn encode_event(mut event: Event, encoding: &EncodingConfig<Encoding>) -> Option<Bytes> {
encoding.apply_rules(&mut event);
let log = event.into_log();
let b = match encoding.codec() {
Encoding::Json => serde_json::to_vec(&log),
Encoding::Text => {
let bytes = log
.get(crate::config::log_schema().message_key())
.map(|v| v.as_bytes().to_vec())
.unwrap_or_default();
Ok(bytes)
}
};
b.map(|mut b| {
b.push(b'\n');
Bytes::from(b)
})
.map_err(|error| error!(message = "Unable to encode.", %error))
.ok()
}
/// Joins namespace with name via delimiter if namespace is present.
pub fn encode_namespace<'a>(
namespace: Option<&str>,
delimiter: char,
name: impl Into<Cow<'a, str>>,
) -> String {
let name = name.into();
namespace
.map(|namespace| format!("{}{}{}", namespace, delimiter, name))
.unwrap_or_else(|| name.into_owned())
}