简体   繁体   中英

How to disable go_collector metrics in prometheus/client_golang

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?

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

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.

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.

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM