簡體   English   中英

如何在 prometheus/client_golang 中禁用 go_collector 指標

[英]How to disable go_collector metrics in prometheus/client_golang

我正在使用 NewGaugeVec 報告我的指標:

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

一切正常,但我注意到我的自定義導出器包含來自 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
...

我懷疑這是一種默認行為,但我在文檔中沒有找到有關如何禁用它的任何內容。 關於如何配置我的自定義導出器以使這些默認指標消失的任何想法?

好吧,這個話題已經很老了,但以防萬一其他人不得不處理它。 以下代碼適用於當前代碼庫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))
}

這在 Go 客戶端中目前是不可能的,一旦https://github.com/prometheus/client_golang/issues/46完成,您將有辦法做到這一點。

一般來說,您希望您的自定義導出器導出這些,我知道目前沒有意義的唯一導出器是 snmp 和 blackbox 導出器。

順便說一句, timestamp作為標簽似乎很奇怪,如果您想要,您可能應該使用日志記錄而不是指標。 請參閱https://blog.raintank.io/logs-and-metrics-and-graphs-oh-my/普羅米修斯的方式是將時間戳作為一個值,而不是一個標簽。

作為回答說“你必須自己去做”並沒有真正的幫助,但這似乎是目前唯一的選擇。

由於 Prometheus 是開源的,如果您真的需要這樣做; 我相信你必須分叉這個go_collector.go line #28 和相關部分,或者更好地修改它以使所有這些指標可選並制作 PR,以便其他人將來也可以從中受益。

我會簡單地這樣做 - >

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

您現在可以使用--web.disable-exporter-metrics

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

這個解決方案對我有用。 想法是創建一個自定義注冊表並注冊我們的指標。 確保我們在打開指標的處理程序選項中傳遞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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM