From 18b07be91a6f032956b9b58435cd0f06c3296944 Mon Sep 17 00:00:00 2001 From: Wan Nur Shafiqah <176239359+shanurwan@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:21:12 +0800 Subject: [PATCH] fix(metrics): send a Content-Type with the Prometheus metrics The monitoring endpoint responded without a Content-Type header. Prometheus 3 refuses such targets unless fallback_scrape_protocol is configured, marking them down with "non-compliant scrape target sending blank Content-Type". Set the text exposition format's content type from the encoder. --- src/metrics.rs | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/metrics.rs b/src/metrics.rs index e32dab068..dd3aaa0ca 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -96,10 +96,13 @@ fn handle_request( ) -> io::Result<()> { let request = request?; let mut buffer = vec![]; - prometheus::TextEncoder::new() - .encode(®.gather(), &mut buffer) - .unwrap(); - let response = tiny_http::Response::from_data(buffer); + let encoder = prometheus::TextEncoder::new(); + encoder.encode(®.gather(), &mut buffer).unwrap(); + // Prometheus 3 rejects scrapes without a Content-Type it recognises. + let content_type = + tiny_http::Header::from_bytes(&b"Content-Type"[..], encoder.format_type().as_bytes()) + .expect("valid Content-Type header"); + let response = tiny_http::Response::from_data(buffer).with_header(content_type); request.respond(response) } @@ -162,3 +165,34 @@ fn start_process_exporter(metrics: &Metrics) { thread::sleep(Duration::from_secs(5)); }); } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn metrics_endpoint_sets_prometheus_content_type() { + let addr = std::net::TcpListener::bind("127.0.0.1:0") + .unwrap() + .local_addr() + .unwrap(); + let metrics = Metrics::new(addr); + metrics + .counter(prometheus::Opts::new("test_requests", "test counter")) + .inc(); + metrics.start(); + + let response = ureq::get(&format!("http://{}/metrics", addr)) + .call() + .unwrap(); + let content_type = response + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .unwrap_or_default() + .to_string(); + assert_eq!(content_type, prometheus::TEXT_FORMAT); + let body = response.into_body().read_to_string().unwrap(); + assert!(body.contains("test_requests 1"), "{}", body); + } +}