• 动手写prometheus的exporter-04-Summary(摘要)


    1. 不带标签的Summary

    语法

    • 实例化
    func NewSummary(opts SummaryOpts) Summary
    
    • 1
    • SummaryOpts 结构体
    type SummaryOpts struct {
        Namespace   string
        Subsystem   string
        Name        string
        Help        string
        ConstLabels Labels
        Objectives  map[float64]float64
        MaxAge      time.Duration
        AgeBuckets  uint32
        BufCap      uint32
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • Observe
    func (Histogram) Observe(float64)
    
    • 1

    完整示例

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

    • 代码
    package main
    
    import (
    	"flag"
    	"fmt"
    	"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 (
    	numSummary = prometheus.NewSummary(prometheus.SummaryOpts{
    		Name: "Summary_num",
    		Help: "Calculate quantile",
    		Objectives: map[float64]float64{0.4:0,0.5:0,0.6:0,0.7:0,0.8:0,0.88:0,0.9:0,0.95:0},
    	})
    
    )
    
    
    func init() {
    	prometheus.MustRegister(numSummary)
    }
    
    func main() {
    	flag.Parse()
    
    	var numbers = [10]float64{100,200,300,400,500,600,700,800,900,1000}
    	for i:=0;i<len(numbers);i++{
    		numSummary.Observe(numbers[i])
    		fmt.Printf("Insert number: %f \n", numbers[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
    • 38
    • 执行结果
    # HELP Summary_num Calculate quantile
    # TYPE Summary_num summary
    Summary_num{quantile="0.4"} 400
    Summary_num{quantile="0.5"} 500
    Summary_num{quantile="0.6"} 600
    Summary_num{quantile="0.7"} 700
    Summary_num{quantile="0.8"} 800
    Summary_num{quantile="0.88"} 900
    Summary_num{quantile="0.9"} 900
    Summary_num{quantile="0.95"} 1000
    Summary_num_sum 5500
    Summary_num_count 10
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    结果说明:

    • Summary_num{quantile="0.4"}:该数组从小到大排序,前40%(含)的成员最后一个是400

    如:本例从小到大排序,前40%是:100,200,300,400。最后一个是400。

    • Summary_num{quantile="0.8"}:该数组从小到大排序,前80%(含)的成员最后一个是800
    • Summary_num{quantile="0.88"}:该数组从小到大排序,前88%(含)的成员最后一个是900

    从小到大排序后,前80%(含)的最后一位是800,但前88%(含)不包含800了,而最后一个取到了900

    • summary_count_sum:所用统计的数字(10个)和为5500
    • scores_count_count:当前一共统计了10个数的结果

    2. 带标签的Summary

    语法

    • 实例化
    func NewSummaryVec(opts SummaryOpts, labelNames []string) *SummaryVec
    
    • 1
    • HistogramOpts结构体
    type SummaryOpts struct {
        Namespace   string
        Subsystem   string
        Name        string
        Help        string
        ConstLabels Labels
        Objectives  map[float64]float64
        MaxAge      time.Duration
        AgeBuckets  uint32
        BufCap      uint32
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 打标签
    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 (
    	numSummary = prometheus.NewSummaryVec(prometheus.SummaryOpts{
    		Name: "Summary_num",
    		Help: "Calculate quantile",
    		Objectives: map[float64]float64{0.4:0,0.5:0,0.6:0,0.7:0,0.8:0,0.88:0,0.9:0,0.95:0},
    	},[]string{"group"})
    
    )
    
    
    func init() {
    	prometheus.MustRegister(numSummary)
    }
    
    func main() {
    	flag.Parse()
    
    	var numbers01 = [10]float64{100,200,300,400,500,600,700,800,900,1000}
    	var numbers02 = [10]float64{1100,1200,1300,1400,1500,1600,1700,1800,1900,2000}
    	for i:=0;i<10;i++{
    		numSummary.WithLabelValues("group-01").Observe(numbers01[i])
    		numSummary.WithLabelValues("group-02").Observe(numbers02[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
    • 38
    • 输出
    # HELP Summary_num Calculate quantile
    # TYPE Summary_num summary
    Summary_num{group="group-01",quantile="0.4"} 400
    Summary_num{group="group-01",quantile="0.5"} 500
    Summary_num{group="group-01",quantile="0.6"} 600
    Summary_num{group="group-01",quantile="0.7"} 700
    Summary_num{group="group-01",quantile="0.8"} 800
    Summary_num{group="group-01",quantile="0.88"} 900
    Summary_num{group="group-01",quantile="0.9"} 900
    Summary_num{group="group-01",quantile="0.95"} 1000
    Summary_num_sum{group="group-01"} 5500
    Summary_num_count{group="group-01"} 10
    Summary_num{group="group-02",quantile="0.4"} 1400
    Summary_num{group="group-02",quantile="0.5"} 1500
    Summary_num{group="group-02",quantile="0.6"} 1600
    Summary_num{group="group-02",quantile="0.7"} 1700
    Summary_num{group="group-02",quantile="0.8"} 1800
    Summary_num{group="group-02",quantile="0.88"} 1900
    Summary_num{group="group-02",quantile="0.9"} 1900
    Summary_num{group="group-02",quantile="0.95"} 2000
    Summary_num_sum{group="group-02"} 15500
    Summary_num_count{group="group-02"} 10
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    说明:见上例


    在这里插入图片描述

  • 相关阅读:
    Dubbo---使用直连方式 dubbo
    多媒体应用设计师 第4章 移动多媒体技术基础
    3-MySQL常用数据类型及表管理
    拿到直播平台的rtmp地址和推流码之后,用 nodejs写一个循环读取视频文件内容,这个地址推流
    如何让bug远离你?
    使用命令行快速创建Vite项目
    C++与C编译后符号表对比(一百九十二)
    clickhouse的日期处理
    Java idea编译器工程out目录修改
    C++实现哈希表。
  • 原文地址:https://blog.csdn.net/xingzuo_1840/article/details/126290919