动手写prometheus的exporter-03-HIstog

2022-08-24  本文已影响0人  玄德公笔记

统计各个值落到每个bucket中的数量

1. 不带标签的HIstogram

语法

func NewHistogram(opts HistogramOpts) Histogram
type HistogramOpts struct {
    Namespace   string
    Subsystem   string
    Name        string
    Help        string
    ConstLabels Labels
    Buckets     []float64
}
func LinearBuckets(start float64, width float64, count int) []float64

说明:

  • start : 第一个bucket统计的最大值(小于等于该值的放入该bucket)
  • width:bucket宽,或者说步长。(start+width就是第二个bucket的上限,依次类推)
  • count:设置多少个bucket
func (Histogram) Observe(float64)

传入Observe(float64)的数据将统计到Bucket

完整示例

一组学生的成绩,分别为分到59分(含)以下、69分(含)以下……等各bucket中

package main

import (
    "flag"
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "log"
    "net/http"
)
var addr = flag.String("listen-address", ":1840", "The address to listen on for HTTP requests")

var (
    scoresHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
        Name: "scores_count",
        Help: "Statistics of student scores",
        Buckets: prometheus.LinearBuckets(59,10,5),
    })
)


func init() {
    prometheus.MustRegister(scoresHistogram)
}

func main() {
    flag.Parse()

    var scores = [10]float64{65,88,82,87,84,92,96,59,87,42}
    for i:=0;i<len(scores);i++{
        scoresHistogram.Observe(scores[i])
    }

    http.Handle("/metrics", promhttp.Handler())
    log.Fatal(http.ListenAndServe(*addr, nil))
}
# HELP scores_count Statistics of student scores
# TYPE scores_count histogram
scores_count_bucket{le="59"} 2
scores_count_bucket{le="69"} 3
scores_count_bucket{le="79"} 3
scores_count_bucket{le="89"} 8
scores_count_bucket{le="99"} 10
scores_count_bucket{le="+Inf"} 10
scores_count_sum 782
scores_count_count 10

结果说明:

  • scores_count_bucket{le="59"}:59分以下(含59分)2 人
  • scores_count_bucket{le="69"}:69分以下(含59分)3 人
  • scores_count_bucket{le="79"}:79分以下(含59分)3 人
  • scores_count_bucket{le="89"}:89分以下(含59分)8 人
  • scores_count_bucket{le="99"}:99分以下(含59分)10 人
  • scores_count_bucket{le="+Inf"}:当前一共统计了10人
  • scores_count_sum:当前学生分数和为 782分
  • scores_count_count:当前一共统计了10人

2. 带标签的HIstogram

根据标签,一个bucket统计多组数据。

语法

func NewHistogramVec(opts HistogramOpts, labelNames []string) *HistogramVec
type HistogramOpts struct {
    Namespace   string
    Subsystem   string
    Name        string
    Help        string
    ConstLabels Labels
    Buckets     []float64
}
func (v *HistogramVec) WithLabelValues(lvs ...string) Observer
func (Histogram) Observe(float64)

传入Observe(float64)的数据将统计到Bucket

完整示例

package main

import (
    "flag"
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "log"
    "net/http"
)
var addr = flag.String("listen-address", ":1840", "The address to listen on for HTTP requests")

var (
    scoresHistogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{
        Name: "scores_count",
        Help: "Statistics of student scores",
        Buckets: prometheus.LinearBuckets(59,10,5),
    },[]string{"group"})
)


func init() {
    prometheus.MustRegister(scoresHistogram)
}

func main() {
    flag.Parse()

    scoresClass01 := [10]float64{65,88,82,87,84,92,96,59,87,42}
    scoresClass02 := [10]float64{90,98,89,97,86,82,99,100,97,88}
    for i:=0;i<10;i++{
        scoresHistogram.WithLabelValues("class-01").Observe(scoresClass01[i])
        scoresHistogram.WithLabelValues("class-02").Observe(scoresClass02[i])
    }

    http.Handle("/metrics", promhttp.Handler())
    log.Fatal(http.ListenAndServe(*addr, nil))
}
# HELP scores_count Statistics of student scores
# TYPE scores_count histogram
scores_count_bucket{group="class-01",le="59"} 2
scores_count_bucket{group="class-01",le="69"} 3
scores_count_bucket{group="class-01",le="79"} 3
scores_count_bucket{group="class-01",le="89"} 8
scores_count_bucket{group="class-01",le="99"} 10
scores_count_bucket{group="class-01",le="+Inf"} 10
scores_count_sum{group="class-01"} 782
scores_count_count{group="class-01"} 10
scores_count_bucket{group="class-02",le="59"} 0
scores_count_bucket{group="class-02",le="69"} 0
scores_count_bucket{group="class-02",le="79"} 0
scores_count_bucket{group="class-02",le="89"} 4
scores_count_bucket{group="class-02",le="99"} 9
scores_count_bucket{group="class-02",le="+Inf"} 10
scores_count_sum{group="class-02"} 926
scores_count_count{group="class-02"} 10

说明:见上例


上一篇 下一篇

猜你喜欢

热点阅读