1 回答

TA貢獻1833條經驗 獲得超4個贊
我想到了。我必須在調用 NewDesc 方法的地方聲明標簽,然后在 MustNewConstMetric 方法中傳遞值
這是帶有“主機名”標簽的新“newCollector”。
func newCollector() *cmdCollector {
? ? return &cmdCollector{
? ? ? ? cmdMetric: prometheus.NewDesc("cmd_result",
? ? ? ? ? ? "Shows the cmd result",
? ? ? ? ? ? []string{"hostname"}, nil,
? ? ? ? ),
? ? }
}
值得注意的是,我只是在這里添加“變量標簽”。最后一個 'nil' 用于常量標簽。
您可以像這樣添加任意數量的項目...
[]string{"hostname", "another_label", "and_another_label"}
這在此處介紹: https ://godoc.org/github.com/prometheus/client_golang/prometheus#NewDesc
接下來,我可以在調用“MustNewConstMetric”方法時添加這些值。
ch <- prometheus.MustNewConstMetric(collector.cmdMetric, prometheus.GaugeValue, metricValue, hostname)
整個街區...
func (collector *cmdCollector) Collect(ch chan<- prometheus.Metric) {
? ? var metricValue float64
? ? command := string("date +%s")
? ? cmdResult := exeCmd(command)
? ? metricValue = cmdResult
? ? ch <- prometheus.MustNewConstMetric(collector.cmdMetric, prometheus.GaugeValue, metricValue, hostname)
}
如果我傳入多個標簽;比如我上面的例子,它看起來更像這樣......
ch <- prometheus.MustNewConstMetric(collector.cmdMetric, prometheus.GaugeValue, metricValue, hostname, anotherLabel", "andAnotherLabel)
- 1 回答
- 0 關注
- 256 瀏覽
添加回答
舉報