From c7ac41fb1988d5537a774d6c904c1058d62114fb Mon Sep 17 00:00:00 2001 From: Dominik Werder Date: Tue, 14 May 2024 16:30:19 +0200 Subject: [PATCH] Fix metrics error type --- daqingest/src/daemon.rs | 1 + netfetch/Cargo.toml | 1 + netfetch/src/metrics.rs | 39 ++++++++++++++++++++------------------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/daqingest/src/daemon.rs b/daqingest/src/daemon.rs index 6e4b604..d0aa1f8 100644 --- a/daqingest/src/daemon.rs +++ b/daqingest/src/daemon.rs @@ -638,6 +638,7 @@ impl Daemon { let worker_fut = process_api_query_items(backend, item_rx, info_worker_tx, iqtx); taskrun::spawn(worker_fut) }; + self.spawn_metrics().await?; Self::spawn_ticker(self.tx.clone(), self.stats.clone()); loop { if self.shutting_down { diff --git a/netfetch/Cargo.toml b/netfetch/Cargo.toml index b0ecc28..435b9c1 100644 --- a/netfetch/Cargo.toml +++ b/netfetch/Cargo.toml @@ -23,6 +23,7 @@ md-5 = "0.10.5" hex = "0.4.3" regex = "1.8.4" axum = "0.6.18" +http-body = "0.4" url = "2.2" hyper = "0.14" chrono = "0.4" diff --git a/netfetch/src/metrics.rs b/netfetch/src/metrics.rs index 7fd0361..f7c2c23 100644 --- a/netfetch/src/metrics.rs +++ b/netfetch/src/metrics.rs @@ -17,9 +17,11 @@ use axum::extract::Query; use axum::http; use axum::response::IntoResponse; use axum::response::Response; +use bytes::Bytes; use err::Error; use http::Request; use http::StatusCode; +use http_body::Body; use log::*; use scywr::iteminsertqueue::QueryItem; use serde::Deserialize; @@ -50,13 +52,22 @@ trait ToPublicErrorMsg { impl ToPublicErrorMsg for err::Error { fn to_public_err_msg(&self) -> PublicErrorMsg { - todo!() + let msg = self + .public_msg() + .map_or("no error message provided".into(), |x| x.join(", ")); + PublicErrorMsg(msg) } } impl IntoResponse for PublicErrorMsg { fn into_response(self) -> axum::response::Response { - todo!() + let msgbytes = self.0.as_bytes(); + let body = axum::body::Bytes::from(msgbytes.to_vec()); + let body = axum::body::Full::new(body); + let body = body.map_err(|_| axum::Error::new(Error::from_string("error while trying to create fixed body"))); + let body = axum::body::BoxBody::new(body); + let x = axum::response::Response::builder().status(500).body(body).unwrap(); + x } } @@ -67,13 +78,13 @@ where T: ToPublicErrorMsg, { fn from(value: T) -> Self { - todo!() + Self(value.to_public_err_msg().into_response()) } } impl IntoResponse for CustomErrorResponse { fn into_response(self) -> Response { - todo!() + self.0 } } @@ -270,7 +281,7 @@ fn make_routes(dcom: Arc, connset_cmd_tx: Sender, st StatusCode::NOT_FOUND }) .nest( - "/some", + "/daqingest/some", Router::new() .route("/path1", get(|| async { (StatusCode::OK, format!("Hello there!")) })) .route( @@ -279,13 +290,6 @@ fn make_routes(dcom: Arc, connset_cmd_tx: Sender, st ) .route("/path3/", get(|| async { (StatusCode::OK, format!("Hello there!")) })), ) - .route( - "/metrics", - get({ - let stats_set = stats_set.clone(); - || async move { metrics(&stats_set) } - }), - ) .route( "/daqingest/metrics", get({ @@ -300,13 +304,6 @@ fn make_routes(dcom: Arc, connset_cmd_tx: Sender, st || async move { metricbeat(&stats_set) } }), ) - .route( - "/metricbeat", - get({ - let stats_set = stats_set.clone(); - || async move { metricbeat(&stats_set) } - }), - ) .route( "/daqingest/always-error/", get(|Query(params): Query>| always_error(params)), @@ -397,6 +394,7 @@ pub async fn metrics_service( stats_set: StatsSet, shutdown_signal: Receiver, ) -> Result<(), Error> { + info!("metrics service start {bind_to}"); let addr = bind_to.parse().map_err(Error::from_string)?; let router = make_routes(dcom, connset_cmd_tx, stats_set).into_make_service(); axum::Server::bind(&addr) @@ -405,6 +403,9 @@ pub async fn metrics_service( let _ = shutdown_signal.recv().await; }) .await + .inspect(|x| { + info!("metrics service finished with {x:?}"); + }) .map_err(Error::from_string)?; Ok(()) }