split timings, remove useless kvs

This commit is contained in:
2026-04-10 07:10:11 +04:00
parent a6d0596c9e
commit 8c3cc3739a
9 changed files with 46 additions and 58 deletions
+30 -10
View File
@@ -160,7 +160,6 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
});
let start = Instant::now();
let method = monitor.method.as_deref().unwrap_or("GET").to_uppercase();
let timeout = std::time::Duration::from_millis(monitor.timeout_ms.unwrap_or(30000));
let is_https = monitor.url.starts_with("https://");
@@ -170,7 +169,7 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
let req_body = monitor.request_body.clone();
let max_redirects = monitor.max_redirects;
let (tx, rx) = tokio::sync::oneshot::channel::<Result<(u16, HashMap<String, String>, String), String>>();
let (tx, rx) = tokio::sync::oneshot::channel::<Result<(u16, HashMap<String, String>, String, u64, u64), String>>();
std::thread::spawn(move || {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
run_check_blocking(&url, &method, req_headers.as_ref(), req_body.as_deref(), timeout, max_redirects)
@@ -187,10 +186,9 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
.and_then(|r| r.map_err(|_| "check thread dropped".to_string()))
.unwrap_or_else(|e| Err(e));
let latency_ms = start.elapsed().as_millis() as u64;
match result {
Err(ref e) => {
let latency_ms = start.elapsed().as_millis() as u64;
debug!("{} check error: {e}", monitor.url);
PingResult {
monitor_id: monitor.id.clone(),
@@ -209,7 +207,7 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
run_id: Some(run_id.to_string()),
}
},
Ok((status, headers, body)) => {
Ok((status, headers, body, total_ms, dns_ms)) => {
let cert_handle = if is_https {
let cert_url = monitor.url.clone();
@@ -233,7 +231,7 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
status,
body: body.clone(),
headers: headers.clone(),
latency_ms: Some(latency_ms),
latency_ms: Some(total_ms.saturating_sub(dns_ms)),
cert_expiry_days: None,
cert_issuer: None,
};
@@ -253,14 +251,18 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
None => None,
};
let cert_expiry_days = cert_info.as_ref().map(|c| c.expiry_days);
let tls_ms = cert_info.as_ref().map(|c| c.tls_ms).unwrap_or(0);
let cert_issuer = cert_info.map(|c| c.issuer);
// Subtract DNS and TLS time from total to get server response time only
let latency_ms = total_ms.saturating_sub(dns_ms).saturating_sub(tls_ms);
let meta = json!({
"headers": headers,
"body_preview": &body[..body.len().min(25_000)],
});
debug!("{} → {status} {latency_ms}ms up={up}", monitor.url);
debug!("{} → {status} {latency_ms}ms (total={total_ms} dns={dns_ms} tls={tls_ms}) up={up}", monitor.url);
PingResult {
monitor_id: monitor.id.clone(),
@@ -282,6 +284,7 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
}
}
// Returns (status, headers, body, total_ms, dns_ms)
fn run_check_blocking(
url: &str,
method: &str,
@@ -289,7 +292,18 @@ fn run_check_blocking(
body: Option<&str>,
timeout: std::time::Duration,
max_redirects: u32,
) -> Result<(u16, HashMap<String, String>, String), String> {
) -> Result<(u16, HashMap<String, String>, String, u64, u64), String> {
// Measure DNS resolution time separately (lookup only, no connection)
let dns_ms = {
let parsed = reqwest::Url::parse(url).map_err(|e| e.to_string())?;
let host = parsed.host_str().unwrap_or("");
let port = parsed.port().unwrap_or(if parsed.scheme() == "https" { 443 } else { 80 });
let addr = format!("{host}:{port}");
let dns_start = Instant::now();
let _ = std::net::ToSocketAddrs::to_socket_addrs(&addr as &str);
dns_start.elapsed().as_millis() as u64
};
let root_certs = ROOT_CERTS.with(|c| Arc::clone(c));
let tls = ureq::tls::TlsConfig::builder()
@@ -306,6 +320,8 @@ fn run_check_blocking(
.build()
.new_agent();
let request_start = Instant::now();
let mut builder = ureq::http::Request::builder()
.method(method)
.uri(url);
@@ -368,12 +384,14 @@ fn run_check_blocking(
}
};
Ok((status, resp_headers, body_out))
let total_ms = request_start.elapsed().as_millis() as u64;
Ok((status, resp_headers, body_out, total_ms, dns_ms))
}
struct CertInfo {
expiry_days: i64,
issuer: String,
tls_ms: u64,
}
async fn check_cert(url: &str) -> Result<Option<CertInfo>> {
@@ -398,7 +416,9 @@ async fn check_cert(url: &str) -> Result<Option<CertInfo>> {
let server_name = ServerName::try_from(host.to_string())?;
let stream = TcpStream::connect(format!("{host}:{port}")).await?;
let tls_start = Instant::now();
let tls_stream = connector.connect(server_name, stream).await?;
let tls_ms = tls_start.elapsed().as_millis() as u64;
let (_, conn) = tls_stream.get_ref();
let certs = conn.peer_certificates().unwrap_or(&[]);
@@ -412,7 +432,7 @@ async fn check_cert(url: &str) -> Result<Option<CertInfo>> {
.as_secs() as i64;
let days = (not_after - now) / 86400;
let issuer = cert.issuer().to_string();
return Ok(Some(CertInfo { expiry_days: days, issuer }));
return Ok(Some(CertInfo { expiry_days: days, issuer, tls_ms }));
}
Ok(None)