如何在prometheus/client_golang中禁用go_collector指标?

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

How to disable go_collector metrics in prometheus/client_golang

问题

我正在使用NewGaugeVec来报告我的指标:

elapsed := prometheus.NewGaugeVec(prometheus.GaugeOpts{
    Name: "gogrinder_elapsed_ms",
    Help: "gogrinder测试步骤的当前经过时间",
}, []string{"teststep", "user", "iteration", "timestamp"})
prometheus.MustRegister(elapsed)

一切都正常,但我注意到我的自定义导出器包含了prometheus/go_collector.go中的所有指标:

# HELP go_gc_duration_seconds GC调用持续时间的摘要。
# TYPE go_gc_duration_seconds 摘要
go_gc_duration_seconds{quantile="0"} 0.00041795300000000004
go_gc_duration_seconds{quantile="0.25"} 0.00041795300000000004
go_gc_duration_seconds{quantile="0.5"} 0.00041795300000000004
...

我怀疑这是一种默认行为,但我在文档中没有找到如何禁用它的任何信息。有没有办法配置我的自定义导出器,使这些默认指标消失?

英文:

I am using a NewGaugeVec to report my metrics:

elapsed := prometheus.NewGaugeVec(prometheus.GaugeOpts{
	Name: "gogrinder_elapsed_ms",
	Help: "Current time elapsed of gogrinder teststep",
}, []string{"teststep", "user", "iteration", "timestamp"})
prometheus.MustRegister(elapsed)

All works fine but I noticed that my custom exporter contains all metrics from prometheus/go_collector.go:

# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0.00041795300000000004
go_gc_duration_seconds{quantile="0.25"} 0.00041795300000000004
go_gc_duration_seconds{quantile="0.5"} 0.00041795300000000004
...

I suspect that this is kind of a default behavior but I did not find anything in the documentation on how to disable that. Any ideas on how to configure my custom exporter so that these default metrics disappear?

答案1

得分: 16

好的,以下是翻译好的内容:

这个话题相当老了,但是以防其他人需要处理它的情况。
以下代码在当前的代码库v0.9.0-pre1中可以正常工作。

// [...] 导入,指标初始化 ...

func main() {
  // 去除任何额外的指标
  // 我们必须使用自定义注册表来公开我们的指标
  r := prometheus.NewRegistry()
  r.MustRegister(myMetrics)
  handler := promhttp.HandlerFor(r, promhttp.HandlerOpts{})

  // [...] 在 goroutine 中更新指标

  http.Handle("/metrics", handler)
  log.Fatal(http.ListenAndServe(":12345", nil))
}

请注意,这只是代码的翻译部分,不包括其他内容。

英文:

Well the topic is rather old but in case others have to deal with it.
The following code works fine with current codebase v0.9.0-pre1

// [...] imports, metric initialization ...

func main() {
  // go get rid of any additional metrics 
  // we have to expose our metrics with a custom registry
  r := prometheus.NewRegistry()
  r.MustRegister(myMetrics)
  handler := promhttp.HandlerFor(r, promhttp.HandlerOpts{})

  // [...] update metrics within a goroutine

  http.Handle("/metrics", handler)
  log.Fatal(http.ListenAndServe(":12345", nil))
}

答案2

得分: 4

这个解决方案对我有效。思路是创建一个自定义的注册表并注册我们的指标。确保在打开指标的处理程序选项中传递False将禁用这些默认指标。

var httpDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
    Name: "golang_api_http_duration_seconds",
    Help: "Duration of HTTP requests.",
}, []string{"path", "host"})

promReg := prometheus.NewRegistry()
promReg.MustRegister(httpDuration)

handler := promhttp.HandlerFor(
    promReg,
    promhttp.HandlerOpts{
        EnableOpenMetrics: false,
    })


http.Handle("/metrics", handler)
log.Fatal(http.ListenAndServe(":12345", nil))
英文:

This solution worked from me. Idea is to create a custom registry and register our metrics. Making sure we pass False in handler options for open metrics will disable those default metrics

var httpDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
		Name: "golang_api_http_duration_seconds",
		Help: "Duration of HTTP requests.",
	}, []string{"path", "host"})

promReg := prometheus.NewRegistry()
promReg.MustRegister(httpDuration)

handler := promhttp.HandlerFor(
		promReg,
		promhttp.HandlerOpts{
			EnableOpenMetrics: false,
		})


http.Handle("/metrics", handler)
log.Fatal(http.ListenAndServe(":12345", nil))

答案3

得分: 3

我会这样做:

// 注册你的收集器
elapsed := prometheus.NewGaugeVec(prometheus.GaugeOpts{
    Name: "gogrinder_elapsed_ms",
    Help: "gogrinder测试步骤的当前经过时间",
}, []string{"teststep", "user", "iteration", "timestamp"})
prometheus.MustRegister(elapsed)
// 移除Go收集器
prometheus.Unregister(prometheus.NewGoCollector())
英文:

I would simply do it this way ->

// Register your collectors
elapsed := prometheus.NewGaugeVec(prometheus.GaugeOpts{
    Name: "gogrinder_elapsed_ms",
    Help: "Current time elapsed of gogrinder teststep",
}, []string{"teststep", "user", "iteration", "timestamp"})
prometheus.MustRegister(elapsed)
// Remove Go collector
prometheus.Unregister(prometheus.NewGoCollector())

答案4

得分: 1

这在Go客户端目前是不可能的,一旦https://github.com/prometheus/client_golang/issues/46完成,你将有一种方法来实现这个。

一般来说,你希望你的自定义导出器来导出这些内容,我只知道其中目前没有意义的是snmp和blackbox导出器。

顺便说一下,timestamp作为一个标签似乎有点奇怪,如果你想要这个,你应该使用日志而不是指标。参见https://blog.raintank.io/logs-and-metrics-and-graphs-oh-my/
Prometheus的方式是将时间戳作为一个值,而不是一个标签。

英文:

This is not currently possible in the Go client, once https://github.com/prometheus/client_golang/issues/46 is complete you'll have a way to do this.

In general you want your custom exporter to export these, the only ones I'm aware of where it doesn't currently make sense are the snmp and blackbox exporter.

Incidentally timestamp seems odd as a label, if you want that you should likely be using logging rather than metrics. See https://blog.raintank.io/logs-and-metrics-and-graphs-oh-my/
The Prometheus way would be to have the timestamp as a value, not as a label.

答案5

得分: 1

你现在可以使用 --web.disable-exporter-metrics

链接:https://github.com/prometheus/node_exporter/pull/1148

英文:

you can use --web.disable-exporter-metrics now.

https://github.com/prometheus/node_exporter/pull/1148

答案6

得分: 0

这个回答似乎并不是很有帮助,因为它说“你必须自己去做”,但目前似乎只有这个选项。

由于Prometheus是开源的,如果你真的需要这样做,我相信你需要fork这个链接中的go_collector.go文件的第28行和相关部分,或者更好的是修改它,使所有这些指标都是可选的,并提出一个PR,这样其他人将来也可以从中受益。

英文:

It's not really helpful as an answer to say "you'd have to go and do it yourself" but it seems like the only option for now.

Since Prometheus is open source and if you really need to do that; I believe you'd have to fork this one go_collector.go line #28 and the related sections, or better yet modify it to make all those metrics optional and make a PR so other people may also benefit from that in the future.

huangapple
  • 本文由 发表于 2016年2月1日 02:59:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/35117993.html
匿名

发表评论

匿名网友

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

确定