Skip to content

feat(layers/prometheus_client): Add disable_label_root to allow skip root label in metrics #6071

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

Merged
merged 3 commits into from
Apr 21, 2025
Merged
Changes from all 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
38 changes: 30 additions & 8 deletions core/src/layers/prometheus_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
},
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(())
Expand Down
Loading