簡體   English   中英

Golang Prometheus Exporter - 按需拉取指標

[英]Golang Prometheus Exporter - Pull metrics on demand

正如prometheus 文檔中所定義的,在編寫導出器時,它聲明如下:

僅當 Prometheus 抓取指標時,才應從應用程序中提取指標,導出器不應根據自己的計時器執行抓取。

以下代碼在技術上可以正常工作,並使用我的自定義指標發布適當的頁面。 所以它按原樣解決了我的業務需求。

但是,它效率低下並且運行無限循環來不斷更新值。 這與 Prometheus 抓取時僅生成度量值的文檔中的上述做法匹配。

package main

import (
    "log"
    "net/http"
    "time"

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

func main() {
    var metricSystemTime = prometheus.NewGauge(prometheus.GaugeOpts{
        Name: "custom_system_time", Help: "Timestamp in unix epoch format"})

    prometheus.MustRegister(metricSystemTime)

    go startServer()

    for {
        metricSystemTime.Set(float64(time.Now().Unix()))
        time.Sleep(1 * time.Second)
    }
}

func startServer() {
    http.Handle("/metrics", promhttp.Handler())
    log.Fatal(http.ListenAndServe(":19100", nil))
}

為了僅在 prometheus 抓取導出器端點時提取指標,這意味着我需要以某種方式偵聽http.ListenAndServe() object 上的 GET 請求? 我怎樣才能做到這一點?

在我的閱讀中,我發現http.HandlerFunc()而不是http.Handler()似乎正在走正確的道路,但我無法讓它與promhttp.Handler()一起工作而且我迷失了它。

您引用的文檔是關於編寫導出器的。 導出器通常是一個實用程序,它以自定義格式從另一個應用程序中提取指標,並以普羅米修斯格式公開它們。

您展示的代碼是檢測您自己的應用程序的正確方法。 您的業務邏輯會隨時更新指標,並且 HTTP 服務器會在請求時提供當前指標值。

經過大量搜索后弄清楚了。 希望這對將來的某人有所幫助。

這是一個正常工作的導出器,適合僅在發生普羅米修斯刮擦時收集指標的要求

  • 沒有無限循環,沒有內部計時器。
  • 每次 Prometheus 抓取導出器時發布新指標,如原始問題的文檔中所定義。
package main

import (
    "log"
    "net/http"
    "time"

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

type Collector struct {
    // Add one of the below lines for each collector you define in the newCollector() function
    systemTime    *prometheus.Desc
}

// Declare your exporter metrics here. Referred to as "collectors"
func newCollector() *Collector {
    return &Collector{
        systemTime: prometheus.NewDesc("my_system_time",
            "System timestamp in unix epoch format",
            nil, nil,
        ),
        // Add more collectors (aka metric definitions) here 
    }
}

func (collector *Collector) Describe(ch chan<- *prometheus.Desc) {
    // Add one of these lines for each of your collectors declared above
    ch <- collector.systemTime
}

// This fuction runs when Prometheus scrapes the exporter. It will set a new value for the metric(s)
// I have no idea how it works, but it does.
func (collector *Collector) Collect(ch chan<- prometheus.Metric) {

    var setSystemTime float64

    // Calculate the value of the metric
    setSystemTime = float64(time.Now().Unix())

    // Here is where you set the "new" value for the metric
    // This example is a Gauge, but it can be any Prom metric type
    ch <- prometheus.MustNewConstMetric(collector.systemTime, prometheus.GaugeValue, setSystemTime)
}

// Main function, spawns the collector and the web server
func main() {
    collector := newCollector()
    prometheus.MustRegister(collector)

    http.Handle("/metrics", promhttp.Handler())
    log.Fatal(http.ListenAndServe(":19100", nil))
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM