Skip to content

Commit

Permalink
Do not panic on failed librato send. (#53)
Browse files Browse the repository at this point in the history
This commit ensures that if a librato write fails we merely log
out the error and move on. Librato only aggregates so we'd end
up skipping some points but would not lose any real data.

Signed-off-by: Brian L. Troutwine <blt@postmates.com>
  • Loading branch information
blt authored Jul 23, 2016
1 parent d54d439 commit c97a9e5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cernan"
version = "0.2.3"
version = "0.2.4"
authors = ["Brian L. Troutwine <blt@postmates.com>"]
build = "build.rs"

Expand Down
9 changes: 6 additions & 3 deletions src/backends/librato.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,19 @@ impl Backend for Librato {
debug!("librato - {}", payload);
let mime: Mime = "application/json".parse().unwrap();
let uri = url::Url::parse(&(self.host)).expect("malformed url");
client.post(uri)
let res = client.post(uri)
.body(&payload)
.header(ContentType(mime))
.header(Authorization(Basic {
username: self.username.clone(),
password: Some(self.auth_token.clone()),
}))
.header(Connection::keep_alive())
.send()
.unwrap();
.send();
match res {
Ok(_) => trace!("Successfully wrote librato payload"),
Err(e) => debug!("Could not write librato payload with error {}", e),
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum Event {
/// statsd
pub fn udp_server(chan: Sender<Event>, port: u16) {
let addr = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), port);
let socket = UdpSocket::bind(addr).ok().unwrap();
let socket = UdpSocket::bind(addr).ok().expect("Unable to bind to UDP socket");
info!("statsd server started on 127.0.0.1:{}", port);
let mut buf = [0; 8192];
loop {
Expand All @@ -25,13 +25,13 @@ pub fn udp_server(chan: Sender<Event>, port: u16) {
Err(_) => panic!("Could not read UDP socket."),
};
let bytes = Vec::from(&buf[..len]);
chan.send(Event::UdpMessage(bytes)).unwrap();
chan.send(Event::UdpMessage(bytes)).expect("[UDP] Unable to write into chan!");
}
}

pub fn tcp_server(chan: Sender<Event>, port: u16) {
let addr = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), port);
let listener = TcpListener::bind(addr).unwrap();
let listener = TcpListener::bind(addr).expect("Unable to bind to TCP socket");
info!("graphite server started on 127.0.0.1:{}", port);
for stream in listener.incoming() {
let newchan = chan.clone();
Expand All @@ -49,7 +49,7 @@ fn handle_client(chan: Sender<Event>, stream: TcpStream) {
for line in line_reader.lines() {
match line {
Ok(line) => {
chan.send(Event::TcpMessage(line.into_bytes())).unwrap();
chan.send(Event::TcpMessage(line.into_bytes())).expect("[TCP] Unable to write to chan!");
}
Err(_) => break,
}
Expand All @@ -61,6 +61,6 @@ pub fn flush_timer_loop(chan: Sender<Event>, interval: u64) {
let duration = Duration::new(interval, 0);
loop {
sleep(duration);
chan.send(Event::TimerFlush).unwrap();
chan.send(Event::TimerFlush).expect("[FLUSH] Unable to write to chan!");
}
}

0 comments on commit c97a9e5

Please sign in to comment.