Flatten run_metrics_poll's nested match pyramid

Extract fetch_metrics_body(client, url) -> Result<String, String> to
handle the request/error_for_status/text chain with `?`, centralizing
the three warning messages. run_metrics_poll now matches once on the
result instead of nesting three match arms deep on the happy path.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TeyhRfMYjMditek6geDRoQ
This commit is contained in:
Hugh Kaznowski
2026-09-07 10:28:13 +01:00
co-authored by Claude Sonnet 5
parent f96de8f38d
commit 4398acfa47
+24 -15
View File
@@ -106,25 +106,34 @@ pub async fn run_file(engine: EngineHandle, path: PathBuf) {
} }
} }
async fn fetch_metrics_body(client: &reqwest::Client, url: &str) -> Result<String, String> {
let response = client
.get(url)
.send()
.await
.map_err(|err| format!("warning: failed to poll metrics endpoint {url}: {err}"))?;
let response = response
.error_for_status()
.map_err(|err| format!("warning: metrics endpoint {url} returned an error: {err}"))?;
response
.text()
.await
.map_err(|err| format!("warning: failed to read metrics response from {url}: {err}"))
}
pub async fn run_metrics_poll(engine: EngineHandle, url: String) { pub async fn run_metrics_poll(engine: EngineHandle, url: String) {
let client = reqwest::Client::new(); let client = reqwest::Client::new();
loop { loop {
match client.get(&url).send().await { match fetch_metrics_body(&client, &url).await {
Ok(response) => match response.error_for_status() { Ok(body) => {
Ok(response) => match response.text().await { let now = Timestamp::now();
Ok(body) => { for sample in parse_openmetrics(&body, now) {
let now = Timestamp::now(); if engine.tx.send(EngineMessage::IngestMetricSample(sample)).await.is_err() {
for sample in parse_openmetrics(&body, now) { return;
if engine.tx.send(EngineMessage::IngestMetricSample(sample)).await.is_err() {
return;
}
}
} }
Err(err) => eprintln!("warning: failed to read metrics response from {url}: {err}"), }
}, }
Err(err) => eprintln!("warning: metrics endpoint {url} returned an error: {err}"), Err(warning) => eprintln!("{warning}"),
},
Err(err) => eprintln!("warning: failed to poll metrics endpoint {url}: {err}"),
} }
tokio::time::sleep(METRICS_POLL_INTERVAL).await; tokio::time::sleep(METRICS_POLL_INTERVAL).await;
} }