简体   繁体   English

如何使用client_golang将指标推送到prometheus?

[英]How to push metrics to prometheus using client_golang?

I have not yet able to find some good examples in using Gauge, Counter and Histogram in prometheus.我还没有找到一些在 prometheus 中使用 Gauge、Counter 和 Histogram 的好例子。 Any help on this will do.对此的任何帮助都可以。 I tried using the the documentation but I was not able to successfully create a working app.我尝试使用文档,但无法成功创建工作应用程序。

I have found this我找到了这个

` `

package main

import (
    "net/http"

    "github.com/prometheus/client_golang/prometheus"
)

var (
cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{
    Name: "cpu_temperature_celsius",
    Help: "Current temperature of the CPU.",
 })
hdFailures = prometheus.NewCounter(prometheus.CounterOpts{
    Name: "hd_errors_total",
    Help: "Number of hard-disk errors.",
})
)

func init() {
    prometheus.MustRegister(cpuTemp)
    prometheus.MustRegister(hdFailures)
}

func main() {
    cpuTemp.Set(65.3)
    hdFailures.Inc()

    http.Handle("/metrics", prometheus.Handler())
    http.ListenAndServe(":8080", nil)
}

` `

This might be useful to some.这可能对某些人有用。

Prometheus is a pull-based system, if you want push-based monitoring, you need to use a gateway of some sort. Prometheus 是一个基于拉的系统,如果你想要基于推送的监控,你需要使用某种网关。 A minimal example (without actually doing anything useful like starting an HTTP listener, or actually doing anything to a metric) follows:一个最小的例子(实际上没有做任何有用的事情,比如启动一个 HTTP 监听器,或者实际上对一个指标做任何事情)如下:

import (
        "github.com/prometheus/client_golang/prometheus"
        "net/http"
)

var responseMetric = prometheus.NewHistogram(
        prometheus.HistogramOpts{
                Name: "request_duration_milliseconds",
                Help: "Request latency distribution",
                Buckets: prometheus.ExponentialBuckets(10.0, 1.13, 40),
        })

func main() {
        prometheus.MustRegister(responseMetric)
        http.Handle("/metrics", prometheus.Handler())
        // Any other setup, then an http.ListenAndServe here
}

You then need to configure Prometheus to scrape the /metrics page your binary provides.然后,您需要配置 Prometheus 来抓取二进制文件提供的/metrics页面。

You could find examples form the prometheus/client_golang .您可以从prometheus/client_golang 中找到示例。 To get you started, you can just get the packages:为了让您开始,您只需获取以下软件包:

$ go get github.com/prometheus/client_golang/prometheus
$ go get github.com/prometheus/client_golang/prometheus/push

The you can just run the following example by setting your correct pushgateway address, which is http://localhost:9091/ in this example:您可以通过设置正确的 pushgateway 地址来运行以下示例,在此示例中为http://localhost:9091/

package main
import (
        "fmt"
        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/push"
)

func ExamplePusher_Push() {
        completionTime := prometheus.NewGauge(prometheus.GaugeOpts{
                Name: "db_backup_last_completion_timestamp_seconds",
                Help: "The timestamp of the last successful completion of a DB backup.",
        })
        completionTime.SetToCurrentTime()
        if err := push.New("http://localhost:9091/", "db_backup").
                Collector(completionTime).
                Grouping("db", "customers").
                Push(); err != nil {
                fmt.Println("Could not push completion time to Pushgateway:", err)
        }
}
func main() {
        ExamplePusher_Push()
}

Run your script:运行你的脚本:

$ go run pushExample.go

After running your code, you should see the metrics at your gateway ( http://localhost:9091/ ).运行代码后,您应该会在网关 ( http://localhost:9091/ ) 上看到指标。 The interface looks like the following:界面如下所示: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM