【问题标题】:How to push metrics to prometheus using client_golang?如何使用 client_golang 将指标推送到 prometheus?
【发布时间】:2016-06-03 10:18:46
【问题描述】:

我还没有找到一些在 prometheus 中使用 Gauge、Counter 和 Histogram 的好例子。对此有任何帮助。我尝试使用文档,但无法成功创建工作应用程序。

【问题讨论】:

  • 你的问题不清楚,因为prometheus是一个基于pull的监控系统。
  • 我发现 python 客户端的文档比 golang 更容易。

标签: go prometheus


【解决方案1】:

您可以从prometheus/client_golang 中找到示例。为了让您开始,您只需获取软件包即可:

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

您可以通过设置正确的推送网关地址来运行以下示例,在此示例中为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()
}

运行你的脚本:

$ go run pushExample.go

运行代码后,您应该会在网关 (http://localhost:9091/) 上看到指标。界面如下所示:

【讨论】:

    【解决方案2】:

    我找到了这个

    `

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

    `

    这可能对某些人有用。

    【讨论】:

      【解决方案3】:

      Prometheus 是一个基于拉的系统,如果你想要基于推送的监控,你需要使用某种网关。一个最小的例子(实际上没有做任何有用的事情,比如启动一个 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
      }
      

      然后您需要配置 Prometheus 以抓取您的二进制文件提供的 /metrics 页面。

      【讨论】:

      • @Efox 是的,我复制了错误的行,它说“requestCounter”,它应该(就像现在一样)说“responseMetric”。
      • 它是否已经测量了某些东西?如何在其上添加示例指标?
      • 我在github页面找到了完整的源代码github
      猜你喜欢
      • 2016-05-09
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      相关资源
      最近更新 更多