prometheus / prometheus/client_rust

Allow construct a histogram family with a closure

Open
#167 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
606
Forks
113
Avg merge
9h 7m
Merged PRs (30d)
8

Description

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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Family::new_with_constructor and compare its function-pointer requirement with the captured closure in the issue's Rust example. Determine an API that accepts dynamic buckets while preserving the custom Observer use case; done means the example can construct and observe the histogram with captured bucket data.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
observability-sre
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.