Home > Back-end >  prometheus.NewHistogram() api for histogram metric type
prometheus.NewHistogram() api for histogram metric type

Time:10-27

Using github.com/prometheus/client_golang/prometheus library to instrument GO app, for metrics:

In the below code:

requestDurations := prometheus.NewHistogram(prometheus.HistogramOpts{
     Name: "http_request_duration_seconds"
     Help: "A Histogram of the http request duration in secconds"

     // Cumulative bucket upper bounds
     Buckets: []float64{0.05, 0.1, 0.25, 0.5, 1, 2,5, 5, 10}
})

requestDurations.Observe(0.42)

  1. What does Buckets: []float64{0.05, 0.1, 0.25, 0.5, 1, 2,5, 5, 10} imply?

  2. What does requestDurations.Observe(0.42) imply?

CodePudding user response:

I recommend you read the extensive online documentation, e.g. Histograms

Histograms are represented by buckets of values.

The first command defines the histogram's buckets by their upper bounds: values <= 0.05, <= 0.1 etc.

The second command, adds an observed value of 0.42 to the histogram by incrementing the <= 0.5 bucket (and all larger buckets).

CodePudding user response:

As the package documentation states:

Buckets defines the buckets into which observations are counted. Each element in the slice is the upper inclusive bound of a bucket. The values must be sorted in strictly increasing order. There is no need to add a highest bucket with Inf bound, it will be added implicitly. The default value is DefBuckets.

Histogram counts observations in buckets. With this declaration, you declare buckets with upper limits of 0.05, 0.1, 0.25, ..., 5, 10, inf. Each observation will be counted in one of these buckets. For instance, the Observe(0.42) will increment the buckets whose upper limits are >=0.5.

  • Related