go - 如何使用 client_golang 将指标推送到普罗米修斯?

标签 go prometheus

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

最佳答案

您可以在 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/) 上看到指标。界面如下所示: enter image description here

关于go - 如何使用 client_golang 将指标推送到普罗米修斯?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37611754/

相关文章:

mongodb - Mongodb Exporter不使用Docker和Prometheus显示指标

ssl - Go https 客户端问题 - 远程错误 : tls: handshake failure

ubuntu - 运行已编译的 golang 脚本时出错

go - 符号是什么 [ :] mean in Go?

python - 使用 Prometheus 中的指标监控批处理作业的状态

http - 将标签添加到默认的 Golang Prometheus 指标

go - 不能在 func 文字的参数中使用 nil 作为类型 _Ctype_CFAllocatorRef

csv - 在 go 中导入数字 csv 数据的正确方法

prometheus - 处理计算中缺失的指标

python - 如何在 pytest test_functions 之间重置 Python 运行时?