1 回答

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
最后我學(xué)會(huì)了直方圖是如何工作的,這是我的代碼
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
type fooCollector struct {
fooMetric *prometheus.Desc
}
//First,we define the variable of histogram
var (
hbrms_histovec = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "hbrms_histogram",
Help: "hbrms_histogram",
ConstLabels: prometheus.Labels{"constname": "constvalue"},
Buckets: prometheus.ExponentialBuckets(50, 1.3, 15),//50*1.3,15times
},
[]string{"env"},
)
)
func newFooCollector() *fooCollector {
return &fooCollector{
fooMetric: prometheus.NewDesc("fff_metric",
"Shows whether a foo has occurred in our cluster",
nil, nil,
),
}
}
func (collector *fooCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.fooMetric
}
func (collector *fooCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(collector.fooMetric, prometheus.CounterValue, float64(1))
// 2nd,we set metrics in this way instead of write to channel,we just find a way of calling the code below when we visit the url.
hbrms_histovec.WithLabelValues("val1").Observe(float64(10))
}
func main() {
reg := prometheus.NewPedanticRegistry()
reg.MustRegister(newFooCollector())
// finally,we register the metrics "hbrms_histovec" in this way
reg.MustRegister(hbrms_histovec)
gatherers := prometheus.Gatherers{reg}
h := promhttp.HandlerFor(gatherers,
promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
})
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
})
http.ListenAndServe(":80", nil)
}
- 1 回答
- 0 關(guān)注
- 123 瀏覽
添加回答
舉報(bào)