登录
首页 >  Golang >  Go问答

为 golang prometheus 收集器添加标签

来源:stackoverflow

时间:2024-04-19 14:48:33 291浏览 收藏

在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是Golang学习者,那么本文《为 golang prometheus 收集器添加标签》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

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

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>