From 2fac4be88e5c8f49ac1cd04ae4237eb9091dd050 Mon Sep 17 00:00:00 2001 From: koushiro Date: Sat, 24 Aug 2024 20:12:01 +0800 Subject: [PATCH] update get_path_label --- core/src/layers/prometheus_client.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/core/src/layers/prometheus_client.rs b/core/src/layers/prometheus_client.rs index d8a973c13e4f..137f3d95bdf0 100644 --- a/core/src/layers/prometheus_client.rs +++ b/core/src/layers/prometheus_client.rs @@ -828,6 +828,9 @@ impl oio::BlockingWrite for PrometheusClientMetricWrapper fn get_path_label(path: &str, path_level: usize) -> Option<&str> { if path_level > 0 { + if path.is_empty() { + return None; + } let label_value = path .char_indices() .filter(|&(_, c)| c == '/') @@ -838,3 +841,20 @@ fn get_path_label(path: &str, path_level: usize) -> Option<&str> { None } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_get_path_label() { + let path = "abc/def/ghi"; + assert_eq!(get_path_label(path, 0), None); + assert_eq!(get_path_label(path, 1), Some("abc")); + assert_eq!(get_path_label(path, 2), Some("abc/def")); + assert_eq!(get_path_label(path, 3), Some("abc/def/ghi")); + assert_eq!(get_path_label(path, usize::MAX), Some("abc/def/ghi")); + + assert_eq!(get_path_label("", 1), None); + } +}