如何在Golang中为Prometheus导出器添加直方图?

huangapple go评论89阅读模式
英文:

How do I add histogram to prometheus exporter in golang?

问题

这是我的代码示例。现在我想在代码中添加直方图,但是我找不到添加直方图的方法。有人可以帮助我吗?我能够编写直方图示例,但是无法将其添加到下面的代码中。

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)
}

请注意,这是您的代码的翻译版本,我没有对代码进行任何修改。

英文:

Here is the example of my code.Now I want add histogram in my code.
but I can't find a way to add histogram like this.

Is anybody could help me?
I am able to write histogram sample but I can't add it in my below code

package main
import (
	&quot;github.com/prometheus/client_golang/prometheus&quot;
	&quot;github.com/prometheus/client_golang/prometheus/promhttp&quot;
	&quot;github.com/prometheus/common/log&quot;
	&quot;net/http&quot;
)

type fooCollector struct {
	fooMetric *prometheus.Desc
}

func newFooCollector(label1 string) *fooCollector {
	return &amp;fooCollector{
		fooMetric: prometheus.NewDesc(&quot;fff_metric&quot;,
			&quot;Shows whether a foo has occurred in our cluster&quot;,
			nil, prometheus.Labels{&quot;env&quot;:label1},
		),
		
	}
}

func (collector *fooCollector) Describe(ch chan&lt;- *prometheus.Desc) {

	//Update this section with the each metric you create for a given collector
	ch &lt;- collector.fooMetric
}

func (collector *fooCollector) Collect(ch chan&lt;- prometheus.Metric) {

	ch &lt;- prometheus.MustNewConstMetric(collector.fooMetric, prometheus.GaugeValue, 111111)

}
func main() {
	prometheus.MustRegister(newFooCollector(&quot;dev&quot;))
	http.Handle(&quot;/metrics&quot;, promhttp.Handler())
	http.ListenAndServe(&quot;:80&quot;, nil)
}

答案1

得分: 1

最后我学会了直方图的工作原理。这是我的代码:

package main

import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"net/http"
)

type fooCollector struct {
	fooMetric *prometheus.Desc
}

// 首先,我们定义直方图的变量
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, 15次
		},
		[]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))
	// 其次,我们以这种方式设置指标,而不是写入通道,我们只是在访问URL时调用下面的代码。
	hbrms_histovec.WithLabelValues("val1").Observe(float64(10))
}

func main() {
	reg := prometheus.NewPedanticRegistry()
	reg.MustRegister(newFooCollector())
	// 最后,我们以这种方式注册指标"hbrms_histovec"
	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)
}

希望对你有帮助!

英文:

finally I learned how histogram works.here is my code

package main
import (
&quot;github.com/prometheus/client_golang/prometheus&quot;
&quot;github.com/prometheus/client_golang/prometheus/promhttp&quot;
&quot;net/http&quot;
)
type fooCollector struct {
fooMetric *prometheus.Desc
}
//First,we define the variable of histogram
var (
hbrms_histovec = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name:        &quot;hbrms_histogram&quot;,
Help:        &quot;hbrms_histogram&quot;,
ConstLabels: prometheus.Labels{&quot;constname&quot;: &quot;constvalue&quot;},
Buckets: prometheus.ExponentialBuckets(50, 1.3, 15),//50*1.3,15times
},
[]string{&quot;env&quot;},
)
)
func newFooCollector() *fooCollector {
return &amp;fooCollector{
fooMetric: prometheus.NewDesc(&quot;fff_metric&quot;,
&quot;Shows whether a foo has occurred in our cluster&quot;,
nil, nil,
),
}
}
func (collector *fooCollector) Describe(ch chan&lt;- *prometheus.Desc) {
ch &lt;- collector.fooMetric
}
func (collector *fooCollector) Collect(ch chan&lt;- prometheus.Metric) {
ch &lt;- 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(&quot;val1&quot;).Observe(float64(10))
}
func main() {
reg := prometheus.NewPedanticRegistry()
reg.MustRegister(newFooCollector())
// finally,we register the metrics &quot;hbrms_histovec&quot; in this way
reg.MustRegister(hbrms_histovec)
gatherers := prometheus.Gatherers{reg}
h := promhttp.HandlerFor(gatherers,
promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
})
http.HandleFunc(&quot;/metrics&quot;, func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
})
http.ListenAndServe(&quot;:80&quot;, nil)
}

答案2

得分: 0

你可以为每个指标调用.Collect(ch)方法(包括描述和值)。此外,你不需要扩展默认的Prometheus路由处理程序,只需注意不要与默认指标名称冲突。

package main

import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"net/http"
)

type fooCollector struct {
	fooMetric      *prometheus.GaugeVec
	hmdrsHistogram *prometheus.HistogramVec
}

func newFooCollector() *fooCollector {
	return &fooCollector{
		fooMetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
			Name: "fff_metric",
			Help: "Shows whether a foo has occurred in our cluster",
		}, []string{"country"}),
		hmdrsHistogram: 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 (collector *fooCollector) Describe(ch chan<- *prometheus.Desc) {
	// 不需要在这里手动调用.Describe(),
	// 因为描述已经在prometheus.MustRegister方法中定义

	//collector.fooMetric.Describe(ch)
	//collector.hmdrsHistogram.Describe(ch)
}

func (collector *fooCollector) Collect(ch chan<- prometheus.Metric) {
	v := 14 // 从数据库/外部服务等获取值

	collector.fooMetric.WithLabelValues("qwe").Set(float64(v))

	collector.fooMetric.WithLabelValues("qwe").Set(v)
	collector.fooMetric.Collect(ch)

	collector.hmdrsHistogram.WithLabelValues("val1").Observe(float64(10))
	collector.hmdrsHistogram.Collect(ch)
}

func RegisterFooCollector() {
	fc := newFooCollector()
	prometheus.MustRegister(fc)
}

func main() {
	RegisterFooCollector()

	// 还包括默认指标
	http.Handle("/metrics", promhttp.Handler())

	err := http.ListenAndServe(":80", nil)

	if err != nil {
		return
	}
}
英文:

You can just simply call .Collect(ch) method for each metric(thats both includes description and values). Also you don't need to extend default prometheus route handler - just be careful you don't have collision with default metric names

package main
import (
&quot;github.com/prometheus/client_golang/prometheus&quot;
&quot;github.com/prometheus/client_golang/prometheus/promhttp&quot;
&quot;net/http&quot;
)
type fooCollector struct {
fooMetric      *prometheus.GaugeVec
hmdrsHistogram *prometheus.HistogramVec
}
func newFooCollector() *fooCollector {
return &amp;fooCollector{
fooMetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: &quot;fff_metric&quot;,
Help: &quot;Shows whether a foo has occurred in our cluster&quot;,
}, []string{&quot;country&quot;}),
hmdrsHistogram: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name:        &quot;hbrms_histogram&quot;,
Help:        &quot;hbrms_histogram&quot;,
ConstLabels: prometheus.Labels{&quot;constname&quot;: &quot;constvalue&quot;},
Buckets:     prometheus.ExponentialBuckets(50, 1.3, 15), //50*1.3,15times
},
[]string{&quot;env&quot;},
),
}
}
func (collector *fooCollector) Describe(ch chan&lt;- *prometheus.Desc) {
// don&#39;t need to manually call .Describe() here,
// because description was defined with prometheus.MustRegister method
//collector.fooMetric.Describe(ch)
//collector.hmdrsHistogram.Describe(ch)
}
func (collector *fooCollector) Collect(ch chan&lt;- prometheus.Metric) {
v := 14 // get value from DB/External service/etc
collector.fooMetric.WithLabelValues(&quot;qwe&quot;).Set(float64(v))
collector.fooMetric.WithLabelValues(&quot;qwe&quot;).Set(v)
collector.fooMetric.Collect(ch)
collector.hmdrsHistogram.WithLabelValues(&quot;val1&quot;).Observe(float64(10))
collector.hmdrsHistogram.Collect(ch)
}
func RegisterFooCollector() {
fc := newFooCollector()
prometheus.MustRegister(fc)
}
func main() {
RegisterFooCollector()
// also includes default metrics
http.Handle(&quot;/metrics&quot;, promhttp.Handler())
err := http.ListenAndServe(&quot;:80&quot;, nil)
if err != nil {
return
}
}

huangapple
  • 本文由 发表于 2021年10月9日 15:36:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/69504691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定