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
use constants;
use metric;
use mio;
use protocols::statsd::parse_statsd;
use regex::Regex;
use source;
use std::io::ErrorKind;
use std::net::ToSocketAddrs;
use std::str;
use std::sync;
use std::sync::atomic::{AtomicUsize, Ordering};
use util;
use util::send;
pub static STATSD_GOOD_PACKET: AtomicUsize = AtomicUsize::new(0);
pub static STATSD_BAD_PACKET: AtomicUsize = AtomicUsize::new(0);
pub struct Statsd {
conns: util::TokenSlab<mio::net::UdpSocket>,
parse_config: sync::Arc<StatsdParseConfig>,
}
pub type Mask = Regex;
pub type Bounds = Vec<f64>;
#[derive(Debug, Clone)]
pub struct StatsdParseConfig {
pub histogram_masks: Vec<(Mask, Bounds)>,
pub summarize_error_bound: f64,
}
impl Default for StatsdParseConfig {
fn default() -> StatsdParseConfig {
StatsdParseConfig {
histogram_masks: vec![],
summarize_error_bound: 0.01,
}
}
}
#[derive(Debug, Clone)]
pub struct StatsdConfig {
pub host: String,
pub port: u16,
pub forwards: Vec<String>,
pub config_path: Option<String>,
pub parse_config: StatsdParseConfig,
}
impl Default for StatsdConfig {
fn default() -> StatsdConfig {
StatsdConfig {
host: "localhost".to_string(),
port: 8125,
forwards: Vec::new(),
config_path: None,
parse_config: StatsdParseConfig::default(),
}
}
}
enum StatsdHandlerErr {
Fatal,
}
impl Statsd {
fn handle_datagrams(
&self,
mut chans: &mut util::Channel,
socket: &mio::net::UdpSocket,
mut buf: &mut Vec<u8>,
) -> Result<(), StatsdHandlerErr> {
let mut metrics = Vec::new();
let basic_metric = sync::Arc::new(Some(metric::Telemetry::default()));
loop {
match socket.recv_from(&mut buf) {
Ok((len, _)) => match str::from_utf8(&buf[..len]) {
Ok(val) => if parse_statsd(
val,
&mut metrics,
&basic_metric,
&self.parse_config,
) {
for m in metrics.drain(..) {
send(&mut chans, metric::Event::new_telemetry(m));
}
STATSD_GOOD_PACKET.fetch_add(1, Ordering::Relaxed);
} else {
STATSD_BAD_PACKET.fetch_add(1, Ordering::Relaxed);
error!("BAD PACKET: {:?}", val);
},
Err(e) => {
error!("Payload not valid UTF-8: {:?}", e);
}
},
Err(e) => match e.kind() {
ErrorKind::WouldBlock => {
break;
}
_ => {
error!("Could not read UDP socket with error {:?}", e);
return Err(StatsdHandlerErr::Fatal);
}
},
}
}
Ok(())
}
}
impl source::Source<StatsdConfig> for Statsd {
fn init(config: StatsdConfig) -> Self {
let mut conns = util::TokenSlab::<mio::net::UdpSocket>::new();
let addrs = (config.host.as_str(), config.port).to_socket_addrs();
match addrs {
Ok(ips) => for addr in ips {
let socket = mio::net::UdpSocket::bind(&addr)
.expect("Unable to bind to UDP socket");
conns.insert(socket);
},
Err(e) => {
info!(
"Unable to perform DNS lookup on host {} with error {}",
config.host, e
);
}
};
Statsd {
conns: conns,
parse_config: sync::Arc::new(config.parse_config),
}
}
fn run(self, mut chans: util::Channel, poller: mio::Poll) -> () {
for (idx, socket) in self.conns.iter() {
if let Err(e) = poller.register(
socket,
mio::Token::from(idx),
mio::Ready::readable(),
mio::PollOpt::edge(),
) {
error!("Failed to register {:?} - {:?}!", socket, e);
}
}
let mut buf = vec![0; 16_250];
loop {
let mut events = mio::Events::with_capacity(1024);
match poller.poll(&mut events, None) {
Ok(_num_events) => {
for event in events {
match event.token() {
constants::SYSTEM => {
send(&mut chans, metric::Event::Shutdown);
return;
}
token => {
let mut socket = &self.conns[token];
if let Err(_e) =
self.handle_datagrams(&mut chans, socket, &mut buf)
{
error!("Deregistering {:?} due to unrecoverable error!", *socket);
}
}
}
}
}
Err(e) => panic!(format!("Failed during poll {:?}", e)),
}
}
}
}