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