如何在戈朗的普罗米修斯出口器中添加直方图?

下面是我的代码示例。现在我想在我的代码中添加直方图。

但是我找不到像这样添加直方图的方法。


有人可以帮助我吗?

我能够编写直方图示例,但我无法将其添加到下面的代码中


package main

import (

    "github.com/prometheus/client_golang/prometheus"

    "github.com/prometheus/client_golang/prometheus/promhttp"

    "github.com/prometheus/common/log"

    "net/http"

)


type fooCollector struct {

    fooMetric *prometheus.Desc

}


func newFooCollector(label1 string) *fooCollector {

    return &fooCollector{

        fooMetric: prometheus.NewDesc("fff_metric",

            "Shows whether a foo has occurred in our cluster",

            nil, prometheus.Labels{"env":label1},

        ),

        

    }

}


func (collector *fooCollector) Describe(ch chan<- *prometheus.Desc) {


    //Update this section with the each metric you create for a given collector

    ch <- collector.fooMetric

}


func (collector *fooCollector) Collect(ch chan<- prometheus.Metric) {


    ch <- prometheus.MustNewConstMetric(collector.fooMetric, prometheus.GaugeValue, 111111)


}

func main() {

    prometheus.MustRegister(newFooCollector("dev"))

    http.Handle("/metrics", promhttp.Handler())

    http.ListenAndServe(":80", nil)

}


慕的地8271018
浏览 170回答 1
1回答

慕少森

最后我学会了直方图是如何工作的,这是我的代码package mainimport (&nbsp; &nbsp; "github.com/prometheus/client_golang/prometheus"&nbsp; &nbsp; "github.com/prometheus/client_golang/prometheus/promhttp"&nbsp; &nbsp; "net/http")type fooCollector struct {&nbsp; &nbsp; fooMetric *prometheus.Desc}//First,we define the variable of histogramvar (&nbsp; &nbsp; hbrms_histovec = prometheus.NewHistogramVec(&nbsp; &nbsp; &nbsp; &nbsp; prometheus.HistogramOpts{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name:&nbsp; &nbsp; &nbsp; &nbsp; "hbrms_histogram",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Help:&nbsp; &nbsp; &nbsp; &nbsp; "hbrms_histogram",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ConstLabels: prometheus.Labels{"constname": "constvalue"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Buckets: prometheus.ExponentialBuckets(50, 1.3, 15),//50*1.3,15times&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; []string{"env"},&nbsp; &nbsp; ))func newFooCollector() *fooCollector {&nbsp; &nbsp; return &fooCollector{&nbsp; &nbsp; &nbsp; &nbsp; fooMetric: prometheus.NewDesc("fff_metric",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Shows whether a foo has occurred in our cluster",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil, nil,&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; }}func (collector *fooCollector) Describe(ch chan<- *prometheus.Desc) {&nbsp; &nbsp; ch <- collector.fooMetric}func (collector *fooCollector) Collect(ch chan<- prometheus.Metric) {&nbsp; &nbsp; ch <- prometheus.MustNewConstMetric(collector.fooMetric, prometheus.CounterValue, float64(1))&nbsp; &nbsp; // 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.&nbsp; &nbsp; hbrms_histovec.WithLabelValues("val1").Observe(float64(10))}func main() {&nbsp; &nbsp; reg := prometheus.NewPedanticRegistry()&nbsp; &nbsp; reg.MustRegister(newFooCollector())&nbsp; &nbsp; // finally,we register the metrics "hbrms_histovec" in this way&nbsp; &nbsp; reg.MustRegister(hbrms_histovec)&nbsp; &nbsp; gatherers := prometheus.Gatherers{reg}&nbsp; &nbsp; h := promhttp.HandlerFor(gatherers,&nbsp; &nbsp; &nbsp; &nbsp; promhttp.HandlerOpts{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ErrorHandling: promhttp.ContinueOnError,&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; &nbsp; &nbsp; h.ServeHTTP(w, r)&nbsp; &nbsp; })&nbsp; &nbsp; http.ListenAndServe(":80", nil)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go