diff --git a/src/garage/tests/common/garage.rs b/src/garage/tests/common/garage.rs index 45c62d40..c230a22e 100644 --- a/src/garage/tests/common/garage.rs +++ b/src/garage/tests/common/garage.rs @@ -194,6 +194,15 @@ api_bind_addr = "127.0.0.1:{admin_port}" .expect("Could not build garage endpoint URI") } + pub fn admin_uri(&self, path: &str) -> http::Uri { + format!( + "http://127.0.0.1:{admin_port}/{path}", + admin_port = self.admin_port, + ) + .parse() + .expect("Could not build garage endpoint URI") + } + pub fn key(&self, maybe_name: Option<&str>) -> Key { let mut key = Key::default(); diff --git a/src/garage/tests/lib.rs b/src/garage/tests/lib.rs index ef370db3..040bd080 100644 --- a/src/garage/tests/lib.rs +++ b/src/garage/tests/lib.rs @@ -4,6 +4,9 @@ mod common; mod admin; mod bucket; +#[cfg(feature = "metrics")] +mod metrics; + mod s3; #[cfg(feature = "k2v")] diff --git a/src/garage/tests/metrics.rs b/src/garage/tests/metrics.rs new file mode 100644 index 00000000..b5db8662 --- /dev/null +++ b/src/garage/tests/metrics.rs @@ -0,0 +1,49 @@ +use bytes::Bytes; +use http::{Request, StatusCode}; +use http_body_util::{BodyExt, Full}; + +use crate::common; + +#[tokio::test] +async fn check_metrics_name() { + let ctx = common::context(); + + let req_url = ctx.garage.admin_uri("metrics"); + let client = ctx.custom_request.client(); + let get_metrics_req = Request::builder() + .method("GET") + .uri(req_url) + .body(Full::new(Bytes::new())) + .unwrap(); + + let response = client + .request(get_metrics_req) + .await + .expect("failed to build 'get metrics' request"); + + assert_eq!(response.status(), StatusCode::OK); + let body = BodyExt::collect(response.into_body()) + .await + .expect("failed to collect bytes from body stream") + .to_bytes(); + let body = String::from_utf8_lossy(&body); + + //dbg!(&body); + let invalid_metrics_name = body + .lines() + .filter(isnot_comment_line) // skip the comment lines + .filter(hasnt_prefix_garage) + .collect::>(); + + if !invalid_metrics_name.is_empty() { + panic!("metrics name should all start with 'garage_' prefix.\nDoc: https://prometheus.io/docs/practices/naming/#metric-names\n\nInvalid:\n{:#?}", invalid_metrics_name); + } +} + +fn isnot_comment_line(line: &&str) -> bool { + !line.starts_with("#") +} + +fn hasnt_prefix_garage(line: &&str) -> bool { + !line.starts_with("garage_") +}