产品动态
POST /api/v1/prom/write
$IP、$PORT、$APPID、$TOKEN 这些变量改为自己实例的认证链接信息,这些链接信息可以从控制台进行查询。$INSTANCE 为当前机器的标识,例如 IP/Hostname。如果没有设置这个 groupingKey,当多台机器一起上报时,数据会互相覆盖。package mainimport ("fmt""time""github.com/prometheus/client_golang/prometheus""github.com/prometheus/client_golang/prometheus/push""github.com/prometheus/common/expfmt")var completionTime = prometheus.NewGauge(prometheus.GaugeOpts{Name: "db_backup_last_completion_timestamp_seconds",Help: "The timestamp of the last successful completion of a DB backup.",})func do() {completionTime.SetToCurrentTime()}func ExamplePusher_Push() {if err := push.New("http://$IP:$PORT", "db_backup").BasicAuth("$APPID", "$TOKEN").Collector(completionTime).Grouping("instance", "$INSTANCE").Grouping("db", "customers").Format(expfmt.FmtText).Push(); err != nil {fmt.Println("Could not push completion time to Pushgateway:", err)}}func main() {do()ticker := time.NewTicker(2 * time.Second)done := make(chan bool)for {select {case <-done:returncase <-ticker.C:ExamplePusher_Push()}}}
push.New 生成的对象可以通过 Client 方法自定义 HTTP Client,我们推荐设置一个合适的超时时间,同时 push 数据我们建议使用异步的方式调用,以免阻塞业务主流程。文档反馈