golang实现向pushgateway推送数据

package commands

import (
    "fmt"

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


func main() {
    ExamplePusher_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()
      // completionTime.Set(200)  // set可以设置任意值(float64)
if err := push.New("http://127.0.0.1:9091", "db_backup").   // push.New("pushgateway地址", "job名称")
        Collector(completionTime).  // Collector(completionTime) 给指标赋值
        Grouping("db", "customers").Grouping("instance", "1.1.1.1").  // 给指标添加标签,可以添加多个
        Push(); err != nil {
        fmt.Println("Could not push completion time to Pushgateway:", err)
    }

}

 

 

 

 

 

posted @ 2021-11-09 09:29  明天OoO你好  阅读(2846)  评论(2编辑  收藏  举报