doushou5761 2018-10-26 23:27
浏览 315
已采纳

将标签添加到Golang Prometheus收集器

I'm trying to figure out how to add a label to a prometheus collector. Any ideas what I'm missing here? I have two files: main.go and collector.go

I used the following link as a guide. https://rsmitty.github.io/Prometheus-Exporters/

I mocked up this example, so I could post it here. I'm ultimately not going to pull "date +%s" for the command. Just can't figure out where to add labels.

For the label I'm trying to add a hostname, so I have a result like:

# HELP cmd_result Shows the cmd result
# TYPE cmd_result gauge
cmd_result{host="my_device_hostname"} 1.919256141363144e-76

I'm also really new to golang, so there is a good chance I'm going about this all wrong! I'm ultimately trying to get prometheus to pull the cmd result on each scrape.

main.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
}
  • 写回答

1条回答 默认 最新

  • dqbjvg2518 2018-10-27 14:45
    关注

    I figured it out. I had to declare the label where I was calling the NewDesc method and then pass the value within the MustNewConstMetric method

    Here is my new "newCollector" with the "hostname" label.

    func newCollector() *cmdCollector {
        return &cmdCollector{
            cmdMetric: prometheus.NewDesc("cmd_result",
                "Shows the cmd result",
                []string{"hostname"}, nil,
            ),
        }
    }
    

    It's worth noting that I'm only adding "variable labels" here. That last 'nil' is for constant labels.

    You can add any number of items like so...

    []string{"hostname", "another_label", "and_another_label"}
    

    This is covered here: https://godoc.org/github.com/prometheus/client_golang/prometheus#NewDesc

    Next I can add those values when calling the "MustNewConstMetric" method.

    ch <- prometheus.MustNewConstMetric(collector.cmdMetric, prometheus.GaugeValue, metricValue, hostname)
    

    The whole block...

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

    If I was passing in multiple labels; such as my example above, it would look more like this...

    ch <- prometheus.MustNewConstMetric(collector.cmdMetric, prometheus.GaugeValue, metricValue, hostname, anotherLabel", "andAnotherLabel)
    

    This is covered here: https://godoc.org/github.com/prometheus/client_golang/prometheus#MustNewConstMetric

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题