Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(layers/prometheus): provide builder APIs #5072

Merged
merged 19 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/layers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub use self::mime_guess::MimeGuessLayer;
#[cfg(feature = "layers-prometheus")]
mod prometheus;
#[cfg(feature = "layers-prometheus")]
pub use self::prometheus::PrometheusLayer;
pub use self::prometheus::{PrometheusLayer, PrometheusLayerBuilder};

#[cfg(feature = "layers-prometheus-client")]
mod prometheus_client;
Expand Down
40 changes: 40 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,42 @@ 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;

/// Return the path label value according to the given `path` and `level`.
///
/// - level = 0: return `None`, which means we ignore the path label.
/// - level > 0: the path label will be the path split by "/" and get the last n level,
/// if n=1 and input path is "abc/def/ghi", and then we'll use "abc/" as the path label.
pub fn path_label_value(path: &str, level: usize) -> Option<&str> {
if path.is_empty() {
koushiro marked this conversation as resolved.
Show resolved Hide resolved
return None;
}

if level > 0 {
let label_value = path
.char_indices()
.filter(|&(_, c)| c == '/')
.nth(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
Loading