forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatsd.rs
More file actions
403 lines (359 loc) · 12.6 KB
/
Copy pathstatsd.rs
File metadata and controls
403 lines (359 loc) · 12.6 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use crate::{
buffers::Acker,
event::metric::{MetricKind, MetricValue},
event::Event,
sinks::util::{
service2::TowerCompat, BatchConfig, BatchSettings, BatchSink, Buffer, Compression,
},
topology::config::{DataType, SinkConfig, SinkContext, SinkDescription},
};
use futures::{future, FutureExt, TryFutureExt};
use futures01::{stream::iter_ok, Sink};
use serde::{Deserialize, Serialize};
use snafu::{ResultExt, Snafu};
use std::collections::BTreeMap;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
use std::task::{Context, Poll};
use tower03::{Service, ServiceBuilder};
#[derive(Debug, Snafu)]
enum BuildError {
#[snafu(display("failed to bind to udp listener socket, error = {:?}", source))]
SocketBindError { source: std::io::Error },
}
pub struct StatsdSvc {
client: Client,
}
pub struct Client {
socket: UdpSocket,
address: SocketAddr,
}
impl Client {
pub fn new(address: SocketAddr) -> crate::Result<Self> {
let from = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0);
let socket = UdpSocket::bind(&from).context(SocketBindError)?;
Ok(Client { socket, address })
}
pub fn send(&self, buf: &[u8]) -> usize {
self.socket
.send_to(buf, &self.address)
.map_err(|e| error!("error sending datagram: {:?}", e))
.unwrap_or_default()
}
}
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct StatsdSinkConfig {
pub namespace: String,
#[serde(default = "default_address")]
pub address: SocketAddr,
#[serde(default)]
pub batch: BatchConfig,
}
pub fn default_address() -> SocketAddr {
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8125)
}
inventory::submit! {
SinkDescription::new_without_default::<StatsdSinkConfig>("statsd")
}
#[typetag::serde(name = "statsd")]
impl SinkConfig for StatsdSinkConfig {
fn build(&self, cx: SinkContext) -> crate::Result<(super::RouterSink, super::Healthcheck)> {
let sink = StatsdSvc::new(self.clone(), cx.acker())?;
let healthcheck = StatsdSvc::healthcheck(self.clone()).boxed().compat();
Ok((sink, Box::new(healthcheck)))
}
fn input_type(&self) -> DataType {
DataType::Metric
}
fn sink_type(&self) -> &'static str {
"statsd"
}
}
impl StatsdSvc {
pub fn new(config: StatsdSinkConfig, acker: Acker) -> crate::Result<super::RouterSink> {
// 1432 bytes is a recommended packet size to fit into MTU
// https://github.com/statsd/statsd/blob/master/docs/metric_types.md#multi-metric-packets
// However we need to leave some space for +1 extra trailing event in the buffer.
// Also one might keep an eye on server side limitations, like
// mentioned here https://github.com/DataDog/dd-agent/issues/2638
let batch = config
.batch
.use_size_as_bytes()?
.get_settings_or_default(BatchSettings::default().bytes(1300).events(1000).timeout(1));
let namespace = config.namespace.clone();
let client = Client::new(config.address)?;
let service = StatsdSvc { client };
let svc = ServiceBuilder::new().service(service);
let sink = BatchSink::new(
TowerCompat::new(svc),
Buffer::new(batch.size, Compression::None),
batch.timeout,
acker,
)
.sink_map_err(|e| error!("Fatal statsd sink error: {}", e))
.with_flat_map(move |event| iter_ok(encode_event(event, &namespace)));
Ok(Box::new(sink))
}
async fn healthcheck(_config: StatsdSinkConfig) -> crate::Result<()> {
Ok(())
}
}
fn encode_tags(tags: &BTreeMap<String, String>) -> String {
let mut parts: Vec<_> = tags
.iter()
.map(|(name, value)| {
if value == "true" {
name.to_string()
} else {
format!("{}:{}", name, value)
}
})
.collect();
parts.sort();
parts.join(",")
}
fn encode_event(event: Event, namespace: &str) -> Option<Vec<u8>> {
let mut buf = Vec::new();
let metric = event.as_metric();
match metric.kind {
MetricKind::Incremental => match &metric.value {
MetricValue::Counter { value } => {
buf.push(format!("{}:{}", metric.name, value));
buf.push("c".to_string());
if let Some(t) = &metric.tags {
buf.push(format!("#{}", encode_tags(t)));
};
}
MetricValue::Gauge { value } => {
buf.push(format!("{}:{:+}", metric.name, value));
buf.push("g".to_string());
if let Some(t) = &metric.tags {
buf.push(format!("#{}", encode_tags(t)));
};
}
MetricValue::Distribution {
values,
sample_rates,
} => {
for (val, sample_rate) in values.iter().zip(sample_rates.iter()) {
buf.push(format!("{}:{}", metric.name, val));
buf.push("h".to_string());
if *sample_rate != 1 {
buf.push(format!("@{}", 1.0 / f64::from(*sample_rate)));
};
if let Some(t) = &metric.tags {
buf.push(format!("#{}", encode_tags(t)));
};
}
}
MetricValue::Set { values } => {
for val in values {
buf.push(format!("{}:{}", metric.name, val));
buf.push("s".to_string());
if let Some(t) = &metric.tags {
buf.push(format!("#{}", encode_tags(t)));
};
}
}
_ => {}
},
MetricKind::Absolute => {
if let MetricValue::Gauge { value } = &metric.value {
buf.push(format!("{}:{}", metric.name, value));
buf.push("g".to_string());
if let Some(t) = &metric.tags {
buf.push(format!("#{}", encode_tags(t)));
};
};
}
}
let mut message: String = buf.join("|");
if !namespace.is_empty() {
message = format!("{}.{}", namespace, message);
};
let mut body: Vec<u8> = message.into_bytes();
body.push(b'\n');
Some(body)
}
impl Service<Vec<u8>> for StatsdSvc {
type Response = ();
type Error = tokio::io::Error;
type Future = future::Ready<Result<(), Self::Error>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, mut frame: Vec<u8>) -> Self::Future {
// remove trailing delimiter
if let Some(b'\n') = frame.last() {
frame.pop();
};
self.client.send(frame.as_ref());
future::ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::{
buffers::Acker,
event::{metric::MetricKind, metric::MetricValue, Metric},
test_util::{collect_n, runtime},
Event,
};
use bytes::Bytes;
use futures01::{future, stream::Stream, sync::mpsc, Future, Sink};
use std::time::{Duration, Instant};
use tokio01::{
self,
codec::BytesCodec,
net::{UdpFramed, UdpSocket},
};
#[cfg(feature = "sources-statsd")]
use {crate::sources::statsd::parser::parse, std::str::from_utf8};
fn tags() -> BTreeMap<String, String> {
vec![
("normal_tag".to_owned(), "value".to_owned()),
("true_tag".to_owned(), "true".to_owned()),
("empty_tag".to_owned(), "".to_owned()),
]
.into_iter()
.collect()
}
#[test]
fn test_encode_tags() {
assert_eq!(
&encode_tags(&tags()),
"empty_tag:,normal_tag:value,true_tag"
);
}
#[cfg(feature = "sources-statsd")]
#[test]
fn test_encode_counter() {
let metric1 = Metric {
name: "counter".to_owned(),
timestamp: None,
tags: Some(tags()),
kind: MetricKind::Incremental,
value: MetricValue::Counter { value: 1.5 },
};
let event = Event::Metric(metric1.clone());
let frame = &encode_event(event, "").unwrap();
let metric2 = parse(from_utf8(&frame).unwrap().trim()).unwrap();
assert_eq!(metric1, metric2);
}
#[cfg(feature = "sources-statsd")]
#[test]
fn test_encode_gauge() {
let metric1 = Metric {
name: "gauge".to_owned(),
timestamp: None,
tags: Some(tags()),
kind: MetricKind::Incremental,
value: MetricValue::Gauge { value: -1.5 },
};
let event = Event::Metric(metric1.clone());
let frame = &encode_event(event, "").unwrap();
let metric2 = parse(from_utf8(&frame).unwrap().trim()).unwrap();
assert_eq!(metric1, metric2);
}
#[cfg(feature = "sources-statsd")]
#[test]
fn test_encode_distribution() {
let metric1 = Metric {
name: "distribution".to_owned(),
timestamp: None,
tags: Some(tags()),
kind: MetricKind::Incremental,
value: MetricValue::Distribution {
values: vec![1.5],
sample_rates: vec![1],
},
};
let event = Event::Metric(metric1.clone());
let frame = &encode_event(event, "").unwrap();
let metric2 = parse(from_utf8(&frame).unwrap().trim()).unwrap();
assert_eq!(metric1, metric2);
}
#[cfg(feature = "sources-statsd")]
#[test]
fn test_encode_set() {
let metric1 = Metric {
name: "set".to_owned(),
timestamp: None,
tags: Some(tags()),
kind: MetricKind::Incremental,
value: MetricValue::Set {
values: vec!["abc".to_owned()].into_iter().collect(),
},
};
let event = Event::Metric(metric1.clone());
let frame = &encode_event(event, "").unwrap();
let metric2 = parse(from_utf8(&frame).unwrap().trim()).unwrap();
assert_eq!(metric1, metric2);
}
#[test]
fn test_send_to_statsd() {
crate::test_util::trace_init();
let config = StatsdSinkConfig {
namespace: "vector".into(),
address: default_address(),
batch: BatchConfig {
max_bytes: Some(512),
timeout_secs: Some(1),
..Default::default()
},
};
let mut rt = runtime();
let sink = StatsdSvc::new(config, Acker::Null).unwrap();
let mut events = Vec::new();
let event = Event::Metric(Metric {
name: "counter".to_owned(),
timestamp: None,
tags: Some(tags()),
kind: MetricKind::Incremental,
value: MetricValue::Counter { value: 1.5 },
});
events.push(event);
let event = Event::Metric(Metric {
name: "histogram".to_owned(),
timestamp: None,
tags: None,
kind: MetricKind::Incremental,
value: MetricValue::Distribution {
values: vec![2.0],
sample_rates: vec![100],
},
});
events.push(event);
let stream = iter_ok(events.into_iter());
let sender = sink.send_all(stream);
let deadline = Instant::now() + Duration::from_millis(100);
// Add a delay to the write side to let the read side
// poll for read interest. Otherwise, this could cause
// a race condition in noisy environments.
let sender = tokio01::timer::Delay::new(deadline)
.map_err(drop)
.and_then(|_| sender);
let (tx, rx) = mpsc::channel(1);
let receiver = Box::new(
future::lazy(|| {
let socket = UdpSocket::bind(&default_address()).unwrap();
future::ok(socket)
})
.and_then(|socket| {
UdpFramed::new(socket, BytesCodec::new())
.map_err(|e| error!("error reading line: {:?}", e))
.map(|(bytes, _addr)| bytes)
.forward(tx.sink_map_err(|e| error!("error sending event: {:?}", e)))
.map(|_| ())
}),
);
rt.spawn(receiver);
let _ = rt.block_on(sender).unwrap();
let messages = rt.block_on(collect_n(rx, 1)).ok().unwrap();
assert_eq!(
messages[0],
Bytes::from("vector.counter:1.5|c|#empty_tag:,normal_tag:value,true_tag\nvector.histogram:2|h|@0.01")
);
}
}