A database step's trace says how long it waited and how long Postgres took (#501)

All Subscriptions' Unread page spends 170-210 ms in its 'page' step one
time in ten, where the query itself takes 24-31 ms on production and the
pool's wait metric stays under 5 ms. The step now carries wait_ms and
query_ms, the same split #367's metrics make, so a slow trace names which.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RqkrVCCH8YcP2Y3i8VNYWA
This commit is contained in:
2026-11-27 23:24:26 -05:00
parent 19262bd788
commit 467ac138e3

View File

@@ -327,7 +327,13 @@ thread_local! {
/// shortcut: half the queries, measured by difference; a wrapper on sqlx's pool would see all.
fn waited(started: std::time::Instant) {
if let Some(q) = LAST_QUERY.take() {
crate::metrics::DB_WAIT.observe("", started.elapsed().saturating_sub(q).as_secs_f64());
let wait = started.elapsed().saturating_sub(q);
crate::metrics::DB_WAIT.observe("", wait.as_secs_f64());
// On the step too, so a slow one in Tempo says whether it waited for a connection or for
// Postgres: the metric's spread could not say which of a page's queries it was.
let span = tracing::Span::current();
span.record("wait_ms", wait.as_millis() as u64);
span.record("query_ms", q.as_millis() as u64);
}
}
@@ -3074,7 +3080,11 @@ pub enum EntryFlag {
/// A span for one step of a request or a scan, and none outside one: the healthcheck and the page's
/// live updates run the same code, and every call there would be a trace of its own.
pub fn step(of: &'static str) -> tracing::Span {
if tracing::Span::current().is_none() { tracing::Span::none() } else { tracing::info_span!("step", of) }
if tracing::Span::current().is_none() {
tracing::Span::none()
} else {
tracing::info_span!("step", of, wait_ms = tracing::field::Empty, query_ms = tracing::field::Empty)
}
}
/// A file on disk with no DeadAir copy made since it was downloaded (issue #171).