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); + } +}