亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 client_golang 將指標推送到 prometheus?

如何使用 client_golang 將指標推送到 prometheus?

Go
偶然的你 2022-01-17 10:38:58
我還沒有找到一些在 prometheus 中使用 Gauge、Counter 和 Histogram 的好例子。對此有任何幫助。我嘗試使用文檔,但無法成功創建工作應用程序。
查看完整描述

3 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

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

http://img1.sycdn.imooc.com//61e4d6dd00012cec16100497.jpg

查看完整回答
反對 回復 2022-01-17
?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

我找到了這個


`


package main


import (

    "net/http"


    "github.com/prometheus/client_golang/prometheus"

)


var (

cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{

    Name: "cpu_temperature_celsius",

    Help: "Current temperature of the CPU.",

 })

hdFailures = prometheus.NewCounter(prometheus.CounterOpts{

    Name: "hd_errors_total",

    Help: "Number of hard-disk errors.",

})

)


func init() {

    prometheus.MustRegister(cpuTemp)

    prometheus.MustRegister(hdFailures)

}


func main() {

    cpuTemp.Set(65.3)

    hdFailures.Inc()


    http.Handle("/metrics", prometheus.Handler())

    http.ListenAndServe(":8080", nil)

}

`


這可能對某些人有用。


查看完整回答
反對 回復 2022-01-17
?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

Prometheus 是一個基于拉的系統,如果你想要基于推送的監控,你需要使用某種網關。一個最小的例子(實際上沒有做任何有用的事情,比如啟動一個 HTTP 監聽器,或者實際上對一個指標做任何事情)如下:


import (

        "github.com/prometheus/client_golang/prometheus"

        "net/http"

)


var responseMetric = prometheus.NewHistogram(

        prometheus.HistogramOpts{

                Name: "request_duration_milliseconds",

                Help: "Request latency distribution",

                Buckets: prometheus.ExponentialBuckets(10.0, 1.13, 40),

        })


func main() {

        prometheus.MustRegister(responseMetric)

        http.Handle("/metrics", prometheus.Handler())

        // Any other setup, then an http.ListenAndServe here

}

然后,您需要配置 Prometheus 以抓取/metrics您的二進制文件提供的頁面。


查看完整回答
反對 回復 2022-01-17
  • 3 回答
  • 0 關注
  • 537 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號