Prometheus – 将一组指标发送到 Gauge

huangapple go评论89阅读模式
英文:

Prometheus - send a list of metrics to Gauge

问题

我有一个以JSON格式表示的指标列表,需要发送到Prometheus。我该如何使用client_golang中的Gauge指标类型一次性将这些指标发送到Prometheus?

目前我有以下代码:

var (
    dockerVer = prometheus.NewGauge(prometheus.GaugeOpts{
        Name: "docker_version_latency",
        Help: "Latency of docker version command.",
    }))
    
func init() {
    // Metrics have to be registered to be exposed:
    prometheus.MustRegister(dockerVer)
}

func main() {
    for {
        get_json_response(1234, "version")
        dockerVer.Set(jsonData[0].Latency)

        // The Handler function provides a default handler to expose metrics
        // via an HTTP server. "/metrics" is the usual endpoint for that.
        http.Handle("/metrics", promhttp.Handler())
        log.Fatal(http.ListenAndServe(":8081", nil))
    }
}

我有很多其他指标,我需要从JSON中读取并动态地将其发送到Gauge指标。

英文:

I have a list of metrics in json format to be send to prometheus.How would I use Guage metrics type in client_golang to send these metrics to prometheus all at once?

Right now I have below code

var (
      dockerVer = prometheus.NewGauge(prometheus.GaugeOpts{
            Name: "docker_version_latency",
            Help: "Latency of docker version command.",
      }))
func init() {
    // Metrics have to be registered to be exposed:
    prometheus.MustRegister(dockerVer)
}

func main() {

    for {
        get_json_response(1234,"version")
        dockerVer.Set(jsonData[0].Latency)

        // The Handler function provides a default handler to expose    metrics
        // via an HTTP server. "/metrics" is the usual endpoint for that.

        http.Handle("/metrics", promhttp.Handler())
        log.Fatal(http.ListenAndServe(":8081", nil))
    }

}

I have many more metrics and I have to read these from the json and send it to gauge dynamically.

答案1

得分: 1

你正在寻找编写自定义收集器作为导出器的一部分,可以参考https://github.com/prometheus/consul_exporter/blob/master/consul_exporter.go#L156的示例。

Docker还内置了可以启用的Prometheus指标,所以你可能不需要编写这个。

英文:

You are looking to write a custom collector as part of an exporter, see https://github.com/prometheus/consul_exporter/blob/master/consul_exporter.go#L156 as one example.

Docker also has Prometheus metrics built in that can be enabled, so you may not need to write this.

huangapple
  • 本文由 发表于 2017年4月5日 16:01:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/43225278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定