Create a custom collector HistogramMetricFamily with python

741 views
Skip to first unread message

Salah Bmba

unread,
Jun 16, 2021, 6:27:55 AM6/16/21
to Prometheus Users
Hello everyone ,

am trying to expose Histogram Metric Family , bate i don't know the parameter of .add_metric(), specify buckets. I already doing that while Metric() and its working

class CustomCollector(object):
    def __init__(self):
        pass
    def collect(self):
        ll= [1,2,3,4,5]
        #With 'Metric' - Working
        metricH = Metric("a_teste",  "A simple teste",'histogram')
        for element in ll:
            metricH.add_sample("a_teste", value=element, labels={'element':'a labels'})
        yield metricH
       #with Histogram - not working
        h =  HistogramMetricFamily("HistogramMetricFamily_teste", "HistogramMetricFamily simple teste")
        h.add_metric(???????)
        yield h
if __name__ == '__main__':
    start_http_server(1234)
    REGISTRY.register(CustomCollector())
    while True:
        time.sleep(1)

I need an example of how add_metric work for Histogram Metric Family, and the specificity of Metric Family in generale.

I would be grateful for anyone who will help me

thank you so much


Ian Billett

unread,
Jun 16, 2021, 10:06:55 AM6/16/21
to Salah Bmba, Prometheus Users
Hi Salah,

I am unfamiliar with the prometheus/client_python, but a quick search for add_metric in that repository leads me to the definition of this function here. Helpfully, this function has a docstring explaining the function parameters.

In this situation I would also look for examples of this function being used in this codebase, the libraries unit-tests are normally always a good place to start.

Best,

Ian

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/4ac425fc-7294-4747-8a8e-2ae83c1165aan%40googlegroups.com.

Salah Bmba

unread,
Jun 16, 2021, 10:56:23 AM6/16/21
to Prometheus Users
thank you so much Ian ,
it works  , it is more explain in the libraries unit-tests
My code becomes  :
        h =  HistogramMetricFamily("HistogramMetricFamily_teste", "HistogramMetricFamily simple teste")
        h.add_metric(['b'], buckets=[('0', 1), ('+Inf', 2)], sum_value=3)
        yield h
and  the metrique becomes :
# TYPE je_teste_HistogramMetricFamily histogram
je_teste_HistogramMetricFamily_bucket{a="b",le="0"} 1.0
je_teste_HistogramMetricFamily_bucket{a="b",le="+Inf"} 2.0
je_teste_HistogramMetricFamily_count{a="b"} 2.0
je_teste_HistogramMetricFamily_sum{a="b"} 3.0

Is that made me create the metrics correctly as I wanted.

thank you,

Salah

Ian Billett

unread,
Jun 17, 2021, 5:35:32 AM6/17/21
to Salah Bmba, Prometheus Users
Salah, awesome - I'm glad you were able to resolve your issue.

Ian

Reply all
Reply to author
Forward
0 new messages