This is an automated email from the ASF dual-hosted git repository.
xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new e4cf8f20e feat(layers/prometheus_client): Add disable_label_root to
allow skip root label in metrics (#6071)
e4cf8f20e is described below
commit e4cf8f20eedb3c89befc511c76ec6adc77250aec
Author: flaneur <[email protected]>
AuthorDate: Mon Apr 21 17:34:53 2025 +0800
feat(layers/prometheus_client): Add disable_label_root to allow skip root
label in metrics (#6071)
* allow disable high cardinality labels in prometheus client
* rename as enable_label_root
* take disable_label_root
---
core/src/layers/prometheus_client.rs | 38 ++++++++++++++++++++++++++++--------
1 file changed, 30 insertions(+), 8 deletions(-)
diff --git a/core/src/layers/prometheus_client.rs
b/core/src/layers/prometheus_client.rs
index 07c03d552..b9f6743db 100644
--- a/core/src/layers/prometheus_client.rs
+++ b/core/src/layers/prometheus_client.rs
@@ -107,6 +107,7 @@ pub struct PrometheusClientLayerBuilder {
entries_rate_buckets: Vec<f64>,
duration_seconds_buckets: Vec<f64>,
ttfb_buckets: Vec<f64>,
+ disable_label_root: bool,
}
impl Default for PrometheusClientLayerBuilder {
@@ -118,6 +119,7 @@ impl Default for PrometheusClientLayerBuilder {
entries_rate_buckets:
observe::DEFAULT_ENTRIES_RATE_BUCKETS.to_vec(),
duration_seconds_buckets:
observe::DEFAULT_DURATION_SECONDS_BUCKETS.to_vec(),
ttfb_buckets: observe::DEFAULT_TTFB_BUCKETS.to_vec(),
+ disable_label_root: false,
}
}
}
@@ -171,6 +173,13 @@ impl PrometheusClientLayerBuilder {
self
}
+ /// The 'root' label might have risks of being high cardinality, users can
choose to disable it
+ /// when they found it's not useful for their metrics.
+ pub fn disable_label_root(mut self, disable: bool) -> Self {
+ self.disable_label_root = disable;
+ self
+ }
+
/// Register the metrics into the registry and return a
[`PrometheusClientLayer`].
///
/// # Examples
@@ -359,6 +368,8 @@ impl PrometheusClientLayerBuilder {
http_response_duration_seconds,
http_connection_errors_total,
http_status_errors_total,
+
+ disable_label_root: self.disable_label_root,
},
}
}
@@ -395,11 +406,16 @@ pub struct PrometheusClientInterceptor {
http_response_duration_seconds: Family<OperationLabels, Histogram,
HistogramConstructor>,
http_connection_errors_total: Family<OperationLabels, Counter>,
http_status_errors_total: Family<OperationLabels, Counter>,
+
+ disable_label_root: bool,
}
impl observe::MetricsIntercept for PrometheusClientInterceptor {
fn observe(&self, labels: observe::MetricLabels, value:
observe::MetricValue) {
- let labels = OperationLabels(labels);
+ let labels = OperationLabels {
+ labels,
+ disable_label_root: self.disable_label_root,
+ };
match value {
observe::MetricValue::OperationBytes(v) => self
.operation_bytes
@@ -473,19 +489,25 @@ impl observe::MetricsIntercept for
PrometheusClientInterceptor {
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
-struct OperationLabels(observe::MetricLabels);
+struct OperationLabels {
+ labels: observe::MetricLabels,
+ disable_label_root: bool,
+}
impl EncodeLabelSet for OperationLabels {
fn encode(&self, mut encoder: LabelSetEncoder) -> Result<(), fmt::Error> {
- (observe::LABEL_SCHEME,
self.0.scheme.into_static()).encode(encoder.encode_label())?;
- (observe::LABEL_NAMESPACE,
self.0.namespace.as_ref()).encode(encoder.encode_label())?;
- (observe::LABEL_ROOT,
self.0.root.as_ref()).encode(encoder.encode_label())?;
- (observe::LABEL_OPERATION,
self.0.operation).encode(encoder.encode_label())?;
+ (observe::LABEL_SCHEME,
self.labels.scheme.into_static()).encode(encoder.encode_label())?;
+ (observe::LABEL_NAMESPACE, self.labels.namespace.as_ref())
+ .encode(encoder.encode_label())?;
+ if !self.disable_label_root {
+ (observe::LABEL_ROOT,
self.labels.root.as_ref()).encode(encoder.encode_label())?;
+ }
+ (observe::LABEL_OPERATION,
self.labels.operation).encode(encoder.encode_label())?;
- if let Some(error) = &self.0.error {
+ if let Some(error) = &self.labels.error {
(observe::LABEL_ERROR,
error.into_static()).encode(encoder.encode_label())?;
}
- if let Some(code) = &self.0.status_code {
+ if let Some(code) = &self.labels.status_code {
(observe::LABEL_STATUS_CODE,
code.as_str()).encode(encoder.encode_label())?;
}
Ok(())