• 动手写prometheus的exporter-03-HIstogram(直方图)


    • 特点:显示数据的区间分布。

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

    1. 不带标签的HIstogram

    语法

    • 实例化
    func NewHistogram(opts HistogramOpts) Histogram
    
    • 1
    • HistogramOpts结构体
    type HistogramOpts struct {
        Namespace   string
        Subsystem   string
        Name        string
        Help        string
        ConstLabels Labels
        Buckets     []float64
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • Buckets设置
    func LinearBuckets(start float64, width float64, count int) []float64
    
    • 1

    说明:

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

    传入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))
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 执行结果
    # 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 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
    
    • 1
    • HistogramOpts结构体
    type HistogramOpts struct {
        Namespace   string
        Subsystem   string
        Name        string
        Help        string
        ConstLabels Labels
        Buckets     []float64
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 打标签
    func (v *HistogramVec) WithLabelValues(lvs ...string) Observer
    
    • 1
    • Observe
    func (Histogram) Observe(float64)
    
    • 1

    传入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))
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 输出
    # 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    说明:见上例


    在这里插入图片描述

  • 相关阅读:
    三层交换机与防火墙对接上网如何配置
    JDK1.5后增加了泛型,那么为什么要有泛型呢?我们该如何自定义泛型结构呢?
    npm下载的包分类
    民宿酒店景区污水处理生产厂家整套设备
    C语言之函数
    【Spring】——9、如何指定初始化和销毁的方法?
    小程序面试题:js、vue、uni、小程序的页面传参方式区别
    Node.js | 数据加密 —— 内置模块 crypto 的应用
    什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
    使用ADB命令查询Android设备的安卓版本信息
  • 原文地址:https://blog.csdn.net/xingzuo_1840/article/details/126284067