func NewSummary(opts SummaryOpts) Summary
type SummaryOpts struct {
Namespace string
Subsystem string
Name string
Help string
ConstLabels Labels
Objectives map[float64]float64
MaxAge time.Duration
AgeBuckets uint32
BufCap uint32
}
func (Histogram) Observe(float64)
一组学生的成绩,分别为分到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))
}
# 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
结果说明:
Summary_num{quantile="0.4"}
:该数组从小到大排序,前40%(含)的成员最后一个是400如:本例从小到大排序,前40%是:100,200,300,400。最后一个是400。
Summary_num{quantile="0.8"}
:该数组从小到大排序,前80%(含)的成员最后一个是800Summary_num{quantile="0.88"}
:该数组从小到大排序,前88%(含)的成员最后一个是900从小到大排序后,前80%(含)的最后一位是800,但前88%(含)不包含800了,而最后一个取到了900
summary_count_sum
:所用统计的数字(10个)和为5500scores_count_count
:当前一共统计了10个数的结果
func NewSummaryVec(opts SummaryOpts, labelNames []string) *SummaryVec
type SummaryOpts struct {
Namespace string
Subsystem string
Name string
Help string
ConstLabels Labels
Objectives map[float64]float64
MaxAge time.Duration
AgeBuckets uint32
BufCap uint32
}
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 (
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))
}
# 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
说明:见上例