亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為 golang prometheus 收集器添加標簽

為 golang prometheus 收集器添加標簽

Go
一只甜甜圈 2023-05-02 10:24:07
我想弄清楚如何向普羅米修斯收集器添加標簽。任何想法我在這里缺少什么?我有兩個文件:main.go 和 collector.go我使用以下鏈接作為指南。https://rsmitty.github.io/Prometheus-Exporters/我模擬了這個例子,所以我可以把它貼在這里。我最終不會為命令提取“date +%s”。只是不知道在哪里添加標簽。對于我正在嘗試添加主機名的標簽,結果如下:# HELP cmd_result Shows the cmd result# TYPE cmd_result gaugecmd_result{host="my_device_hostname"} 1.919256141363144e-76我對 golang 也很陌生,所以我很有可能把這一切都弄錯了!我最終試圖讓普羅米修斯在每次刮擦時拉出 cmd 結果。主程序package mainimport (    "net/http"    log "github.com/Sirupsen/logrus"    "github.com/prometheus/client_golang/prometheus"    "github.com/prometheus/client_golang/prometheus/promhttp")func main() {    //Create a new instance of the collector and    //register it with the prometheus client.    cmd := newCollector()    prometheus.MustRegister(cmd)    //This section will start the HTTP server and expose    //any metrics on the /metrics endpoint.    http.Handle("/metrics", promhttp.Handler())    log.Info("Beginning to serve on port :8080")    log.Fatal(http.ListenAndServe(":8080", nil))}收集器.gopackage mainimport (    "encoding/binary"    "fmt"    "math"    "os/exec"    "strings"    "github.com/prometheus/client_golang/prometheus")type cmdCollector struct {    cmdMetric *prometheus.Desc}func newCollector() *cmdCollector {    return &cmdCollector{        cmdMetric: prometheus.NewDesc("cmd_result",            "Shows the cmd result",            nil, nil,        ),    }}func (collector *cmdCollector) Describe(ch chan<- *prometheus.Desc) {    ch <- collector.cmdMetric}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)}
查看完整描述

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)

查看完整回答
反對 回復 2023-05-02
  • 1 回答
  • 0 關注
  • 256 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號