我还没有找到一些在 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/) 上看到指标。界面如下所示:
关于go - 如何使用 client_golang 将指标推送到普罗米修斯?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37611754/