From 1ac6f4bcd97690183fc6e4006953b2b9af19b30d Mon Sep 17 00:00:00 2001 From: Justin Geibel Date: Sat, 2 Jan 2021 21:56:33 -0500 Subject: [PATCH 1/2] Use new conduit API when computing elapsed time The request start time is now captured by a `conduit` based server. This ensures that the logged elapsed time is accurate, even if the request queues waiting for a background thread to run on. --- Cargo.lock | 12 ++++++------ Cargo.toml | 6 +++--- src/middleware/log_request.rs | 10 +++------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4ebd7c0d2a7..385488bd140 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -385,9 +385,9 @@ dependencies = [ [[package]] name = "civet" -version = "0.12.0-alpha.4" +version = "0.12.0-alpha.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2adfa9a0117908b89125b42475fa45da5cbfae6a32f06ef67043918ad054587a" +checksum = "7b38e18226401dff3e26aa36d5128707af657649a56f134fb29b465daa3bd6d1" dependencies = [ "civet-sys", "conduit", @@ -461,9 +461,9 @@ dependencies = [ [[package]] name = "conduit" -version = "0.9.0-alpha.3" +version = "0.9.0-alpha.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68f55ff2e3601329f850eaa86fe6dff2cd66a58ac1df26188e850162126a0432" +checksum = "6ce8954d399f2fcb0900b06973b4624538b8d8e46d38c12be237dd3cc8e95cfc" dependencies = [ "http 0.2.2", ] @@ -504,9 +504,9 @@ dependencies = [ [[package]] name = "conduit-hyper" -version = "0.3.0-alpha.5" +version = "0.3.0-alpha.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f2fc542431a2f71cdf7897594e180efe0deda7044a52c3bd1e31f12356b4eb" +checksum = "1b49a5700d37b4304284964370b5cd7ee50f413c425f306596294dffaf515bd8" dependencies = [ "conduit", "http 0.2.2", diff --git a/Cargo.toml b/Cargo.toml index 77b6bf1718a..ba6775f5696 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,15 +34,15 @@ anyhow = "1.0" base64 = "0.13" cargo-registry-s3 = { path = "src/s3", version = "0.2.0" } chrono = { version = "0.4.0", features = ["serde"] } -civet = "0.12.0-alpha.4" +civet = "0.12.0-alpha.5" clap = "=3.0.0-beta.2" comrak = { version = "0.9", default-features = false } -conduit = "0.9.0-alpha.3" +conduit = "0.9.0-alpha.5" conduit-conditional-get = "0.9.0-alpha.3" conduit-cookie = "0.9.0-alpha.4" conduit-git-http-backend = "0.9.0-alpha.2" -conduit-hyper = "=0.3.0-alpha.5" +conduit-hyper = "=0.3.0-alpha.5.1" conduit-middleware = "0.9.0-alpha.3" conduit-router = "0.9.0-alpha.3" conduit-static = "0.9.0-alpha.3" diff --git a/src/middleware/log_request.rs b/src/middleware/log_request.rs index ca27b277dd9..f54fc788c27 100644 --- a/src/middleware/log_request.rs +++ b/src/middleware/log_request.rs @@ -4,10 +4,11 @@ use super::prelude::*; use crate::middleware::current_user::TrustedUserId; use crate::util::request_header; + use conduit::{header, Host, RequestExt, Scheme, StatusCode}; use sentry::Level; + use std::fmt::{self, Display, Formatter}; -use std::time::Instant; const SLOW_REQUEST_THRESHOLD_MS: u64 = 1000; @@ -16,22 +17,17 @@ const FILTERED_HEADERS: &[&str] = &["Authorization", "Cookie", "X-Real-Ip", "X-F #[derive(Default)] pub(super) struct LogRequests(); -struct RequestStart(Instant); struct OriginalPath(String); impl Middleware for LogRequests { fn before(&self, req: &mut dyn RequestExt) -> BeforeResult { - req.mut_extensions().insert(RequestStart(Instant::now())); let path = OriginalPath(req.path().to_string()); req.mut_extensions().insert(path); Ok(()) } fn after(&self, req: &mut dyn RequestExt, res: AfterResult) -> AfterResult { - // Unwrap shouldn't panic as no other code has access to the private struct to remove it - let request_start = req.extensions().find::().unwrap().0; - - let response_time = request_start.elapsed(); + let response_time = req.elapsed(); let response_time = response_time.as_secs() * 1000 + u64::from(response_time.subsec_nanos()) / 1_000_000; From a4df38efbd4fd38533746419c16dd613c9fac771 Mon Sep 17 00:00:00 2001 From: Justin Geibel Date: Sat, 2 Jan 2021 22:23:23 -0500 Subject: [PATCH 2/2] Bump a few more conduit crates --- Cargo.lock | 8 ++++---- Cargo.toml | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 385488bd140..4429a2e5c2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -519,9 +519,9 @@ dependencies = [ [[package]] name = "conduit-middleware" -version = "0.9.0-alpha.3" +version = "0.9.0-alpha.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d11aae989fb790857867a76d27cc02304d7ca743b49667e711fdb6b1ee6a5f1f" +checksum = "ddc5fb08579847c817024b06fb144f6ef39f66588f2cd4df1e300c1fd4769d1b" dependencies = [ "conduit", ] @@ -559,9 +559,9 @@ dependencies = [ [[package]] name = "conduit-test" -version = "0.9.0-alpha.3" +version = "0.9.0-alpha.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75b2f3bd0554c5ca215e78d8569b093a9dea8e384c0f2497a30dd0cccafe1ef5" +checksum = "3c4cfaa2c193186237c6b7dd18b2511f36f4bb0926d8fe54cc27746a7ed394d2" dependencies = [ "conduit", ] diff --git a/Cargo.toml b/Cargo.toml index ba6775f5696..71d769bc85b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,7 +43,7 @@ conduit-conditional-get = "0.9.0-alpha.3" conduit-cookie = "0.9.0-alpha.4" conduit-git-http-backend = "0.9.0-alpha.2" conduit-hyper = "=0.3.0-alpha.5.1" -conduit-middleware = "0.9.0-alpha.3" +conduit-middleware = "0.9.0-alpha.4" conduit-router = "0.9.0-alpha.3" conduit-static = "0.9.0-alpha.3" @@ -89,7 +89,7 @@ url = "2.1" [dev-dependencies] claim = "0.4.0" -conduit-test = "0.9.0-alpha.3" +conduit-test = "0.9.0-alpha.4" diesel_migrations = { version = "1.3.0", features = ["postgres"] } hyper-tls = "0.4" lazy_static = "1.0"