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

Allow construct a histogram family with a closure #167

Open
jotare opened this issue Oct 3, 2023 · 2 comments
Open

Allow construct a histogram family with a closure #167

jotare opened this issue Oct 3, 2023 · 2 comments

Comments

@jotare
Copy link

jotare commented Oct 3, 2023

Hi, I'm trying to build a custom struct that wraps a couple of metrics and measure them at the same time. Something like this:

struct Observer<M> {
   count: Family<M, Counter>,
   duration: Family<M, Histogram>,
}

impl<M: Clone + Hash + Eq + PartialEq> Observer<M> {
   pub fn with_buckets(buckets: &[f64]) -> Self {
       Self {
           count: Family::<M, Counter>::default(),
           duration: Family::<M, Histogram>::new_with_constructor(|| {
               Histogram::new(buckets.iter().copied()
           }),
       }
   }

   pub fn observe(&self, label_set: M, value: f64) {
       self.count.get_or_create(&label_set).inc();
       self.duration.get_or_create(&label_set).observe(value);
   }
}

The idea is to be able to increase the counter at the same time we observe the histogram.

The problem is Family::new_with_constructor requires a function pointer. As I want dynamic buckets, I need to capture the buckets variable inside the closure and the compiler can't cast it to a function pointer anymore.

Could be possible to allow a more flexible constructor?

Thanks!

@tylerlevine
Copy link

I think what you're asking for can be done with a custom metric constructor. See here for details.

@mxinden
Copy link
Member

mxinden commented Oct 16, 2023

As I want dynamic buckets

Is this supported by Prometheus? As far as I know, Prometheus expects Histogram buckets to be constant throughout the lifetime of a monitoring target.

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