Skip to content

Commit

Permalink
refactor(layers/prometheus): provide consistent APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
koushiro committed Aug 30, 2024
1 parent 3b50fdb commit af0b2f4
Show file tree
Hide file tree
Showing 2 changed files with 244 additions and 97 deletions.
35 changes: 35 additions & 0 deletions core/src/layers/observe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
//! This module offers essential components to facilitate the implementation of observability in OpenDAL.

mod metrics;

pub use metrics::MetricMetadata;
pub use metrics::MetricsAccessor;
pub use metrics::MetricsIntercept;
Expand All @@ -33,3 +34,37 @@ pub use metrics::LABEL_SCHEME;
pub use metrics::METRIC_OPERATION_BYTES;
pub use metrics::METRIC_OPERATION_DURATION_SECONDS;
pub use metrics::METRIC_OPERATION_ERRORS_TOTAL;

pub(crate) fn path_label_value(path: &str, path_level: usize) -> Option<&str> {
if path.is_empty() {
return None;
}

if path_level > 0 {
let label_value = path
.char_indices()
.filter(|&(_, c)| c == '/')
.nth(path_level - 1)
.map_or(path, |(i, _)| &path[..i]);
Some(label_value)
} else {
None
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_path_label_value() {
let path = "abc/def/ghi";
assert_eq!(path_label_value(path, 0), None);
assert_eq!(path_label_value(path, 1), Some("abc"));
assert_eq!(path_label_value(path, 2), Some("abc/def"));
assert_eq!(path_label_value(path, 3), Some("abc/def/ghi"));
assert_eq!(path_label_value(path, usize::MAX), Some("abc/def/ghi"));

assert_eq!(path_label_value("", 1), None);
}
}
Loading

0 comments on commit af0b2f4

Please sign in to comment.