chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.6.0 Latest 5 packages
0
0
Logs warn of what failed, not of calls let go or a service's 502
crates/hud-core/src/net.rs+40−9
| @@ -365,7 +365,7 @@ impl Net { | ||
req: reqwest::RequestBuilder, | ||
) -> reqwest::Result<reqwest::Response> { | ||
let (client, req) = req.build_split(); | ||
execute(&client, req?).await | ||
execute(&client, req?, None).await | ||
} | ||
| ||
/// Send a request made by `get` or `post` and read its answer, within | ||
| @@ -380,11 +380,16 @@ impl Net { | ||
why: "its answer is too large".into(), | ||
answered: true, | ||
}; | ||
// The wait's end, so a call it cuts off is logged as timed out, not | ||
// abandoned by its caller. | ||
let deadline = std::time::Instant::now() + wait; | ||
let read = async { | ||
let (client, req) = req.build_split(); | ||
let req = req.map_err(|e| Failure::of(&e))?; | ||
let asked = req.url().clone(); | ||
let mut res = execute(&client, req).await.map_err(|e| Failure::of(&e))?; | ||
let mut res = execute(&client, req, Some(deadline)) | ||
.await | ||
.map_err(|e| Failure::of(&e))?; | ||
let status = res.status().as_u16(); | ||
let redirected = res.url() != &asked; | ||
let headers = res.headers().clone(); | ||
| @@ -431,10 +436,12 @@ impl Net { | ||
} | ||
| ||
/// Sends `req` on `client`, in the trace the task is in (`traceparent`), | ||
/// and logs it once answered, failed, or given up on before either. | ||
/// and logs it once answered, failed, or given up on before either: by | ||
/// `deadline`, its wait's end, if it has one, or else by what asked for it. | ||
async fn execute( | ||
client: &reqwest::Client, | ||
mut req: reqwest::Request, | ||
deadline: Option<std::time::Instant>, | ||
) -> reqwest::Result<reqwest::Response> { | ||
if let Some(id) = crate::telemetry::trace_id() { | ||
let parent = crate::telemetry::traceparent(&id); | ||
| @@ -446,6 +453,7 @@ async fn execute( | ||
method: req.method().to_string(), | ||
path: crate::telemetry::short(req.url().path()), | ||
started: std::time::Instant::now(), | ||
deadline, | ||
done: false, | ||
}; | ||
let res = client.execute(req).await; | ||
| @@ -462,6 +470,8 @@ struct Sent { | ||
method: String, | ||
path: String, | ||
started: std::time::Instant, | ||
/// When its wait ends, where it has one. | ||
deadline: Option<std::time::Instant>, | ||
done: bool, | ||
} | ||
| ||
| @@ -479,11 +489,20 @@ impl Sent { | ||
} | ||
| ||
impl Drop for Sent { | ||
/// One given up on before it was answered, as when its wait ran out or | ||
/// what asked for it went away, is logged as `abandoned`. | ||
/// One given up on before it was answered is logged as `timeout` where | ||
/// its wait ran out, and as `abandoned` where what asked for it went | ||
/// away first, as a search typed past or a race's loser. | ||
fn drop(&mut self) { | ||
if !self.done { | ||
self.log(None, Some("abandoned")); | ||
let ran_out = self | ||
.deadline | ||
.is_some_and(|d| std::time::Instant::now() >= d); | ||
let why = if ran_out { | ||
"timeout" | ||
} else { | ||
crate::telemetry::ABANDONED | ||
}; | ||
self.log(None, Some(why)); | ||
} | ||
} | ||
} | ||
| @@ -942,24 +961,36 @@ pub(crate) mod tests { | ||
assert!(said.starts_with(&format!("00-{id}-")), "{said}"); | ||
let req = net.get(&url("/parent")).unwrap(); | ||
assert_eq!(net.execute(req).await.unwrap().text().await.unwrap(), ""); | ||
// Cut off by its own wait: timed out. | ||
let gone = net | ||
.fetch(&url("/slow"), Duration::from_millis(100), 1024) | ||
.await; | ||
assert!(gone.is_err()); | ||
// Given up on by what asked, long before its wait: abandoned. | ||
let slow = url("/slow"); | ||
let fetching = net.fetch(&slow, Duration::from_secs(5), 1024); | ||
let left = tokio::time::timeout(Duration::from_millis(100), fetching).await; | ||
assert!(left.is_err()); | ||
}) | ||
}); | ||
let calls: Vec<&serde_json::Value> = lines | ||
.iter() | ||
.filter(|l| l["message"] == "outgoing") | ||
.collect(); | ||
assert_eq!(calls.len(), 3, "{lines:#?}"); | ||
assert_eq!(calls.len(), 4, "{lines:#?}"); | ||
assert_eq!(calls[0]["trace_id"], id); | ||
assert_eq!(calls[0]["url.path"], "/parent"); | ||
assert_eq!(calls[0]["http.response.status_code"], 200); | ||
assert_eq!(calls[0]["server.address"], "127.0.0.1"); | ||
assert!(calls[1].get("trace_id").is_none()); | ||
assert_eq!(calls[2]["error.type"], "abandoned"); | ||
assert_eq!(calls[2]["level"], "WARN"); | ||
assert_eq!( | ||
(&calls[2]["error.type"], &calls[2]["level"]), | ||
(&"timeout".into(), &"WARN".into()) | ||
); | ||
assert_eq!( | ||
(&calls[3]["error.type"], &calls[3]["level"]), | ||
(&"abandoned".into(), &"DEBUG".into()) | ||
); | ||
assert!(!lines.iter().any(|l| l.to_string().contains("secret"))); | ||
} | ||
} | ||
crates/hud-core/src/telemetry.rs+17−7
| @@ -371,17 +371,22 @@ pub(crate) struct Call<'a> { | ||
pub took: Duration, | ||
} | ||
| ||
/// What a call given up on by what asked for it, not by its wait, is | ||
/// logged as: no failure, at debug. | ||
pub(crate) const ABANDONED: &str = "abandoned"; | ||
| ||
/// Logs a call made and counts it: at debug, or warn if it failed, | ||
/// answered 5xx or was slow. | ||
/// answered 5xx or was slow; one [`ABANDONED`] is no failure. | ||
pub(crate) fn called(c: &Call) { | ||
let ms = millis(c.took); | ||
let server_error = c.status.filter(|s| *s >= 500).map(|s| s.to_string()); | ||
let error = c.error.or(server_error.as_deref()); | ||
let failed = error.is_some_and(|e| e != ABANDONED); | ||
let slow = slow(c.took); | ||
let outcome = c | ||
.status | ||
.map_or(error.unwrap_or_default().to_string(), |s| s.to_string()); | ||
let host = record(|s| s.call(c.host, &outcome, ms, error.is_some(), slow)); | ||
let host = record(|s| s.call(c.host, &outcome, ms, failed, slow)); | ||
let rpc = c.path.strip_prefix("/xrpc/"); | ||
macro_rules! line { | ||
($level:expr) => { | ||
| @@ -399,7 +404,7 @@ pub(crate) fn called(c: &Call) { | ||
) | ||
}; | ||
} | ||
match error.is_some() || slow { | ||
match failed || slow { | ||
true => line!(Level::WARN), | ||
false => line!(Level::DEBUG), | ||
} | ||
| @@ -445,10 +450,13 @@ pub struct Served<'a> { | ||
} | ||
| ||
/// Logs a request served and counts it: at error if it failed on this | ||
/// side (5xx), warn if slow, info otherwise, and debug for a health check. | ||
/// side (5xx), warn if what HUD asked for it failed (502, 504: the call's | ||
/// own line says which) or it was slow, info otherwise, and debug for a | ||
/// health check. | ||
pub fn served(r: &Served) { | ||
let ms = millis(r.took); | ||
let failed = r.status >= 500; | ||
let upstream = matches!(r.status, 502 | 504); | ||
let slow = slow(r.took) && !r.waits; | ||
let route = r.route.unwrap_or("fallback"); | ||
let key = (route.to_string(), r.method.to_string(), r.status); | ||
| @@ -478,7 +486,7 @@ pub fn served(r: &Served) { | ||
) | ||
}; | ||
} | ||
match (failed, slow, quiet) { | ||
match (failed && !upstream, failed || slow, quiet) { | ||
(true, _, _) => line!(Level::ERROR), | ||
(false, true, _) => line!(Level::WARN), | ||
(false, false, true) => line!(Level::DEBUG), | ||
| @@ -1053,7 +1061,9 @@ pub(crate) mod tests { | ||
for (route, status, ms) in [ | ||
(Some("/_health"), 200, 1), | ||
(Some("/blob/{did}/{cid}"), 200, 2), | ||
(Some("/blob/{did}/{cid}"), 502, 2), | ||
(Some("/blob/{did}/{cid}"), 500, 2), | ||
// What HUD asked for it failed: its call's line says so. | ||
(Some("/icon/{domain}"), 502, 2), | ||
(None, 200, 5000), | ||
] { | ||
served(&Served { | ||
| @@ -1075,7 +1085,7 @@ pub(crate) mod tests { | ||
summarize(&[("docs", 3, 1), ("handles", 0, 0)]); | ||
}); | ||
let levels: Vec<&str> = lines.iter().map(|l| l["level"].as_str().unwrap()).collect(); | ||
assert_eq!(&levels[..4], ["DEBUG", "INFO", "ERROR", "WARN"]); | ||
assert_eq!(&levels[..5], ["DEBUG", "INFO", "ERROR", "WARN", "WARN"]); | ||
assert_eq!(lines[2]["error"], "PDS down"); | ||
assert_eq!(lines[2]["http.route"], "/blob/{did}/{cid}"); | ||
assert_eq!(lines[1]["http.response.status_code"], 200); | ||
docs/observability.md+4−4
| @@ -15,11 +15,11 @@ Each line has `timestamp`, `level` (`ERROR`, `WARN`, `INFO`, `DEBUG`), `message` | ||
| ||
| `message` | Level | Fields | | ||
| --- | --- | --- | | ||
| `request` | `info`. `warn` past `HUD_SLOW_MS`, `error` for a 5xx, `debug` for `/_health` | `http.route`, `http.request.method`, `url.path`, `url.query`, `user_agent.original`, `http.response.status_code`, `http.response.body.size`, `duration_ms`, `trace_id`, `error` | | ||
| `request` | `info`. `warn` past `HUD_SLOW_MS` or for a 502 or 504, `error` for any other 5xx, `debug` for `/_health` | `http.route`, `http.request.method`, `url.path`, `url.query`, `user_agent.original`, `http.response.status_code`, `http.response.body.size`, `duration_ms`, `trace_id`, `error` | | ||
| `mcp tool` | `info`. `warn` past `HUD_SLOW_MS` | `gen_ai.tool.name`, `did`, `is_error`, `reason`, `duration_ms` | | ||
| `mcp tool failed` | `warn` | `gen_ai.tool.name`, `did`, `is_error`, `error`, `duration_ms` | | ||
| `mcp error` | `info` | `rpc.method`, `error` | | ||
| `outgoing` | `debug`. `warn` when it failed, answered 5xx or was slow | `server.address`, `http.request.method`, `url.path`, `rpc.method`, `http.response.status_code`, `error.type`, `duration_ms` | | ||
| `outgoing` | `debug`. `warn` when it failed, timed out, answered 5xx or was slow | `server.address`, `http.request.method`, `url.path`, `rpc.method`, `http.response.status_code`, `error.type`, `duration_ms` | | ||
| `summary: requests` | `info`, each minute | `count`, `failed`, `slow`, `mean_ms`, `max_ms` | | ||
| `summary: outgoing` | `info`, each minute, per host | `server.address`, `count`, `failed`, `slow`, `mean_ms`, `max_ms` | | ||
| `summary: cache` | `info`, each minute, per cache | `cache`, `hits`, `misses` | | ||
| @@ -27,13 +27,13 @@ Each line has `timestamp`, `level` (`ERROR`, `WARN`, `INFO`, `DEBUG`), `message` | ||
| `hud starting` | `info` | `version`, `commit` | | ||
| `panicked` | `error` | `error`, with the file and line | | ||
| ||
`request` is one wide line per request, written when the answer goes out. `http.route` is the route's template (`/blob/{did}/{cid}`), and each of HUD's own XRPC methods by its path (`/xrpc/place.hud.getCanvas`). A request no route takes is `fallback` in the counts. The workspace's event stream (`getWorkspaceEvents`) is never slow, since it stays open on purpose. `error` names an XRPC method's error, the blob proxy's reason, and for a 5xx why it failed. | ||
`request` is one wide line per request, written when the answer goes out. `http.route` is the route's template (`/blob/{did}/{cid}`), and each of HUD's own XRPC methods by its path (`/xrpc/place.hud.getCanvas`). A request no route takes is `fallback` in the counts. The workspace's event stream (`getWorkspaceEvents`) is never slow, since it stays open on purpose. `error` names an XRPC method's error, the blob proxy's reason, and for a 5xx why it failed. A 502 or 504 is a service HUD called failing, not HUD (a PDS for the blob proxy, the favicon service for an icon), so it's a warning, and the call's own `outgoing` line names the service. | ||
| ||
`url.query` masks the value of any parameter a secret could be in (`code`, `state`, `iss`, `token`, `access_token`, `refresh_token`, `password`, `secret`, `key`) as `_`, and cuts it at 200 characters. A request under `/oauth` logs no query. | ||
| ||
`mcp tool` is one line per `tools/call`, with the caller's DID when signed in. A tool that answered an error is `mcp tool failed`, with the error the client saw. A call that asked the person to sign in has `reason: sign-in asked`. HUD never logs a tool's arguments or its result, since tokens, records and messages can be in them. | ||
| ||
`outgoing` is one line per call through hud-core's outbound client (`hud_core::net`), which every request to a PDS, PLC, an AppView, a feed generator or any other service goes through. `rpc.method` is the XRPC method, for a path under `/xrpc/`. `error.type` is why it failed before an answer (`timeout`, `connect`, `body` or `request`, with the cause), the status of a 5xx, or `abandoned` for a call given up on before it was answered (its wait ran out, or the request that made it ended). Calls are at debug, so a busy minute doesn't drown the log. Set `hud::outgoing=debug` to see each one. | ||
`outgoing` is one line per call through hud-core's outbound client (`hud_core::net`), which every request to a PDS, PLC, an AppView, a feed generator or any other service goes through. `rpc.method` is the XRPC method, for a path under `/xrpc/`. `error.type` is why it failed before an answer (`timeout`, `connect`, `body` or `request`, with the cause), or the status of a 5xx. A call its wait cut off is `timeout`. One dropped before then, because what asked for it stopped waiting (a search typed past, the loser of a race), is `abandoned`: no failure, at debug, and not counted as failed. Calls are at debug, so a busy minute doesn't drown the log. Set `hud::outgoing=debug` to see each one. | ||
| ||
Other lines (sign-ins, canvases, deletions) are as their code writes them, with the same `timestamp`, `level`, `message` and `trace_id`. | ||