improve logging and error handling

fixes #2
main
Jan Christian Gr??nhage 5 years ago
parent 2c2c5359b6
commit 0e8f270742

@ -39,6 +39,11 @@ async fn main() -> Result<(), ()> {
}; };
tokio::spawn(start_pinging_hosts(config.clone())); tokio::spawn(start_pinging_hosts(config.clone()));
start_serving_metrics(config.clone()).await; match start_serving_metrics(config.clone()).await {
Ok(()) Ok(_) => Ok(()),
Err(error) => {
error!("Couldn't serve metrics: {}", error);
Err(())
}
}
} }

@ -24,6 +24,7 @@ use hyper::{
Body, Request, Response, Server, Body, Request, Response, Server,
}; };
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::info;
use prometheus::*; use prometheus::*;
use prometheus::{Counter, Gauge, HistogramVec, TextEncoder}; use prometheus::{Counter, Gauge, HistogramVec, TextEncoder};
@ -70,12 +71,10 @@ async fn serve_req(_req: Request<Body>) -> std::result::Result<Response<Body>, h
Ok(response) Ok(response)
} }
pub(crate) async fn start_serving_metrics(config: Config) { pub(crate) async fn start_serving_metrics(config: Config) -> std::result::Result<(), hyper::Error> {
let serve_future = Server::bind(&config.listener).serve(make_service_fn(|_| async { let serve_future = Server::bind(&config.listener).serve(make_service_fn(|_| async {
Ok::<_, hyper::Error>(service_fn(serve_req)) Ok::<_, hyper::Error>(service_fn(serve_req))
})); }));
info!("Listening on {}", &config.listener);
if let Err(err) = serve_future.await { serve_future.await
eprintln!("server error: {}", err);
}
} }

@ -20,7 +20,7 @@
use crate::config::Config; use crate::config::Config;
use futures_util::stream::StreamExt; use futures_util::stream::StreamExt;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::{error, info}; use log::{error, info, trace};
use prometheus::*; use prometheus::*;
use std::time::Duration; use std::time::Duration;
use tokio::time::delay_for; use tokio::time::delay_for;
@ -42,7 +42,13 @@ lazy_static! {
pub(crate) async fn start_pinging_hosts( pub(crate) async fn start_pinging_hosts(
config: Config, config: Config,
) -> std::result::Result<(), tokio_ping::Error> { ) -> std::result::Result<(), tokio_ping::Error> {
let pinger = tokio_ping::Pinger::new().await?; let pinger = match tokio_ping::Pinger::new().await {
Ok(pinger) => pinger,
Err(error) => {
error!("Couldn't create pinger: {}", error);
std::process::exit(1);
}
};
for (host, interval) in config.hosts.clone() { for (host, interval) in config.hosts.clone() {
info!("Spawn ping task for {}", host); info!("Spawn ping task for {}", host);
let pingchain = pinger.chain(host).timeout(Duration::from_secs(3)); let pingchain = pinger.chain(host).timeout(Duration::from_secs(3));
@ -51,11 +57,16 @@ pub(crate) async fn start_pinging_hosts(
match ping_result { match ping_result {
Ok(time) => match time { Ok(time) => match time {
Some(time) => { Some(time) => {
let ms = time.as_millis();
trace!("Received pong from {} after {} ms", &host, &ms);
PING_HISTOGRAM PING_HISTOGRAM
.with_label_values(&[&host]) .with_label_values(&[&host])
.observe(time.as_millis() as f64); .observe(ms as f64);
}
None => {
trace!("Received no response from {} within timeout", &host);
PING_HISTOGRAM.with_label_values(&[&host]).observe(3000.0);
} }
None => PING_HISTOGRAM.with_label_values(&[&host]).observe(3000.0),
}, },
Err(error) => error!("Couldn't ping {}: {}", &host, error), Err(error) => error!("Couldn't ping {}: {}", &host, error),
} }

Loading…
Cancel
Save