我想弄清楚如何向普罗米修斯收集器添加标签。任何想法我在这里缺少什么?我有两个文件:main.go 和 collector.go
我使用以下链接作为指南。https://rsmitty.github.io/Prometheus-Exporters/
我模拟了这个例子,所以我可以把它贴在这里。我最终不会为命令提取“date +%s”。只是不知道在哪里添加标签。
对于我正在尝试添加主机名的标签,结果如下:
# HELP cmd_result Shows the cmd result
# TYPE cmd_result gauge
cmd_result{host="my_device_hostname"} 1.919256141363144e-76
我对 golang 也很陌生,所以我很有可能把这一切都弄错了!我最终试图让普罗米修斯在每次刮擦时拉出 cmd 结果。
主程序
package main
import (
"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))
}
收集器.go
package main
import (
"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)
}
潇潇雨雨
相关分类