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

Unable to reuse the PrometheusLayer with the same registry #4854

Open
evenyag opened this issue Jul 4, 2024 · 4 comments
Open

Unable to reuse the PrometheusLayer with the same registry #4854

evenyag opened this issue Jul 4, 2024 · 4 comments

Comments

@evenyag
Copy link
Contributor

evenyag commented Jul 4, 2024

As mentioned in GreptimeTeam/greptimedb#4277 (comment), the PrometheusLayer will create a new PrometheusMetrics each time we register it to an OperatorBuilder.

PrometheusAccessor {
inner,
stats: Arc::new(PrometheusMetrics::new(
self.registry.clone(),
self.requests_duration_seconds_buckets.clone(),
self.bytes_total_buckets.clone(),
self.path_label_level,
)),
scheme,
}

However, the registry panics if we create PrometheusMetrics with the same registry more than once.

/// new with prometheus register.
pub fn new(
registry: Registry,
requests_duration_seconds_buckets: Vec<f64>,
bytes_total_buckets: Vec<f64>,
path_label_level: usize,
) -> Self {
let labels = if path_label_level > 0 {
vec!["scheme", "operation", "path"]
} else {
vec!["scheme", "operation"]
};
let requests_total = register_int_counter_vec_with_registry!(
"requests_total",
"Total times of create be called",
&labels,
registry
)

So users are unable to create multiple operators with the same layer that collects metrics to the default global registry of rust-prometheus.

One approach to work around this without breaking change is to allow users to set the PrometheusMetrics.

pub struct PrometheusLayer {
    registry: Registry,
    requests_duration_seconds_buckets: Vec<f64>,
    bytes_total_buckets: Vec<f64>,
    path_label_level: usize,
    metrics: Option<Arc<PrometheusMetrics>>,
}

If users set the metrics explicitly, then the layer uses the provided metrics. So we can pre-build the PrometheusMetrics and reuse it without constructing a new one each time.

@sunng87
Copy link
Contributor

sunng87 commented Jul 4, 2024

I think we can just remove this unwrap https://github.com/apache/opendal/blob/174bda53f79123cd114d2409189423a0a4cf6bf3/core/src/layers/prometheus.rs#L199C9-L199C19 by checking the error type: https://github.com/tikv/rust-prometheus/blob/master/src/registry.rs#L48 and ignore this AlreadyReg

@Xuanwo
Copy link
Member

Xuanwo commented Jul 4, 2024

I think we can just remove this unwrap https://github.com/apache/opendal/blob/174bda53f79123cd114d2409189423a0a4cf6bf3/core/src/layers/prometheus.rs#L199C9-L199C19 by checking the error type: https://github.com/tikv/rust-prometheus/blob/master/src/registry.rs#L48 and ignore this AlreadyReg

Thanks, I think this can be addressed in a separate PR. Would you like to help with that?

@sunng87
Copy link
Contributor

sunng87 commented Jul 4, 2024

Oh, I just see why it's impossible because we need that register call to return the handle. If there is an error we won't be able to get the handle. We need to go upstream for this.

@evenyag
Copy link
Contributor Author

evenyag commented Jul 4, 2024

Oh, I just see why it's impossible because we need that register call to return the handle. If there is an error we won't be able to get the handle. We need to go upstream for this.

We need a way to get the registered metric
tikv/rust-prometheus#248

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants