diff --git a/control-plane/src/prometheus.rs b/control-plane/src/prometheus.rs index a78a99ae..5e9c0e7e 100644 --- a/control-plane/src/prometheus.rs +++ b/control-plane/src/prometheus.rs @@ -269,6 +269,8 @@ mod tests { ] { assert!(text.contains(expected), "{expected} missing:\n{text}"); } + eprintln!("PROM-SAMPLE +{text}"); // one TYPE line per metric even with three tier series assert_eq!(text.matches("# TYPE leleka_expert_tier_entries_total").count(), 1); // every label value comes from a fixed list, never from a request diff --git a/control-plane/src/request_log.rs b/control-plane/src/request_log.rs index 9fcaa628..baa7dd5b 100644 --- a/control-plane/src/request_log.rs +++ b/control-plane/src/request_log.rs @@ -741,6 +741,49 @@ mod tests { assert_eq!(session_tag(&headers).unwrap(), tag); } + fn counted(endpoint: &str, outcome: &str) -> u64 { + let mut text = String::new(); + render_prometheus(&mut text); + let prefix = format!("leleka_requests_total{{endpoint=\"{endpoint}\",outcome=\"{outcome}\"}} "); + text.lines().find_map(|l| l.strip_prefix(prefix.as_str())).and_then(|n| n.parse().ok()).unwrap_or(0) + } + + #[tokio::test] + async fn the_middleware_names_the_request_hands_it_to_the_handler_and_records_a_rejection() { + let observe = Observe { model: Some("m".into()), memory: Arc::new(Default::default()) }; + let app = axum::Router::new() + .route( + "/v1/chat/completions", + axum::routing::post(|| async { + let handle = ObsHandle::current(); + assert!(handle.0.is_some(), "the handler must see the request's record"); + handle.set_stream(false); + (axum::http::StatusCode::SERVICE_UNAVAILABLE, "busy") + }), + ) + .layer(axum::middleware::from_fn_with_state(observe, super::observe)); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("http://{}/v1/chat/completions", listener.local_addr().unwrap()); + let server = tokio::spawn(async move { axum::serve(listener, app).await }); + let before = counted("chat", "rejected"); + let response = reqwest::Client::new().post(url).header("session_id", "conv-4417").body("{}").send().await.unwrap(); + assert_eq!(response.status(), 503); + let id = response.headers()[REQUEST_ID_HEADER].to_str().unwrap().to_owned(); + assert!(uuid::Uuid::parse_str(&id).is_ok(), "{id}"); + assert!(!id.contains("4417")); + // the record is written when the last reference to it is gone, just after the response + let mut after = before; + for _ in 0..50 { + after = counted("chat", "rejected"); + if after > before { + break; + } + tokio::time::sleep(std::time::Duration::from_millis(20)).await; + } + assert!(after > before, "the rejection must be counted: {before} -> {after}"); + server.abort(); + } + #[test] fn the_prometheus_text_has_fixed_labels_and_valid_lines() { let c = ctx(); diff --git a/crates/deploy/src/supervisor.rs b/crates/deploy/src/supervisor.rs index b5268ce8..5ce995f8 100644 --- a/crates/deploy/src/supervisor.rs +++ b/crates/deploy/src/supervisor.rs @@ -450,7 +450,7 @@ async fn run_inner(options: Options) -> Result<(), Failure> { let result = match outcome { Ok(()) | Err(Flow::Stopped) => Ok(()), Err(Flow::Fatal(failure)) => { - supervisor.log.event(leleka_obs::Level::Error, "fatal", &[("code", failure.code.into()), ("msg", failure.message.as_str().into())]); + supervisor.log.event(leleka_obs::Level::Error, "fatal", &[("code", failure.code.into()), ("msg", format!("fatal: {}", failure.message).as_str().into())]); supervisor.set_phase(Phase::Failed, &failure.message); Err(failure) }