Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

acelyc111
Copy link

No description provided.

Copy link
Member

@csmarchbanks csmarchbanks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the idea is that you want to observe count instances of the exact same amount? Can I ask what your use case for this is? My first thought is that it is quite niche and might be better served with a custom collector, but I want to keep my mind open.

@acelyc111
Copy link
Author

acelyc111 commented Aug 30, 2025

@csmarchbanks
Thanks for your reply!
My use case is using Heatmap in Grafana via a Histogram in Prometheus, I can only get the sum of each bucket when I use Histogram, because the data source is from another module which I can't invasive modify too much, so I have to update the metrics like:

age_to_count = ... # another module
for age, count in age_to_count:
  for _ in range(count):
    age_to_count_hist.observe(age)

It's a bit of waste because every observe is O(n) to find the corresponding bucket.

@acelyc111
Copy link
Author

@csmarchbanks Or we have to write code like this which is not safe to access the protected fields:

age_to_count = ... # another module
for age, count in age_to_count:
  age_to_count_hist._sum.inc(age * count)
  for i, bound in enumerate(age_to_count_hist._upper_bounds):
    if age <= bound:
      age_to_count_hist._buckets[i].inc(count)
      break

@csmarchbanks
Copy link
Member

My thought for this case is to use a custom collector, specifically creating the buckets (as you somewhat already have those). See

def add_metric(self,
and https://prometheus.github.io/client_python/collector/custom/ for more information.

That will allow at scrape time for you to call age_to_count and generate the histogram directly. I think with your current implementation you will also be "re-observing" many of your buckets if you are calling inc(age*count) on some sort of timer.

Let me know what you think of that suggestion!

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

Successfully merging this pull request may close these issues.

2 participants