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

说明:见上例


posted on   运维开发玄德公  阅读(85)  评论(0编辑  收藏  举报  

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示