Commit d0fdda8d authored by Robert Czechowski's avatar Robert Czechowski

Measure request processing time and log if request takes longer than a...

Measure request processing time and log if request takes longer than a threshold (currently 50 ms) in order to find slow database requests
parent 21755f8f
Pipeline #1074 passed with stages
in 23 minutes and 11 seconds
......@@ -130,6 +130,32 @@ impl iron_sessionstorage::Value for SessionToken {
}
}
pub struct RequestTimeLogger {}
impl AroundMiddleware for RequestTimeLogger {
fn around(self, handler: Box<dyn Handler>) -> Box<dyn Handler> {
use std::time::{Duration, Instant};
Box::new(move |req: &mut Request| -> IronResult<Response> {
// Begin measurement
let start = Instant::now();
// Process request
let res = handler.handle(req);
// End measurement
let duration = start.elapsed();
let threshold = Duration::from_millis(50);
if duration > threshold {
println!("Request took too long ({:?}) {}: {}", duration, req.method, req.url);
}
res
})
}
}
pub struct CookieDistributor {}
impl AroundMiddleware for CookieDistributor {
......@@ -1497,6 +1523,7 @@ pub fn start_server<C>(conn: C, config: Config) -> iron::error::HttpResult<iron:
ch.link(Write::<SharedDatabaseConnection<C>>::both(conn));
ch.link(Read::<SharedConfiguration>::both(config.clone()));
ch.link_around(RequestTimeLogger {});
ch.link_around(CookieDistributor {});
ch.link_around(SessionStorage::new(SignedCookieBackend::new(config.cookie_signing_secret.expect("Cookie signing secret not found in configuration").into_bytes())));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment