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

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

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

1. 不带标签的HIstogram

语法

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

说明:

  • start : 第一个bucket统计的最大值(小于等于该值的放入该bucket)
  • width:bucket宽,或者说步长。(start+width就是第二个bucket的上限,依次类推)
  • count:设置多少个bucket
  • Observe
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
  • HistogramOpts结构体
type HistogramOpts struct {
    Namespace   string
    Subsystem   string
    Name        string
    Help        string
    ConstLabels Labels
    Buckets     []float64
}
  • 打标签
func (v *HistogramVec) WithLabelValues(lvs ...string) Observer
  • Observe
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

说明:见上例


在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玄德公笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值