gpt4 book ai didi

go - 为 golang prometheus 收集器添加标签

转载 作者:IT王子 更新时间:2023-10-29 01:44:03 24 4
gpt4 key购买 nike

我正在尝试弄清楚如何向普罗米修斯收集器添加标签。任何想法我在这里缺少什么?我有两个文件: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 结果。

ma​​in.go

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

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

}

func exeCmd(cmd string) float64 {
parts := strings.Fields(cmd)
out, err := exec.Command(parts[0], parts[1]).Output()
if err != nil {
fmt.Println("error occured")
fmt.Printf("%s", err)
}
cmdProcessResult := Float64frombytes(out)
return cmdProcessResult
}

func Float64frombytes(bytes []byte) float64 {
bits := binary.LittleEndian.Uint64(bytes)
float := math.Float64frombits(bits)
return float
}

最佳答案

我想通了。我必须在调用 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)

这里介绍: https://godoc.org/github.com/prometheus/client_golang/prometheus#MustNewConstMetric

关于go - 为 golang prometheus 收集器添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53017421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com