登录
首页 >  Golang >  Go问答

灵活获取指标的Golang Prometheus导出器

来源:stackoverflow

时间:2024-02-13 09:00:25 222浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《灵活获取指标的Golang Prometheus导出器》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

问题内容

根据普罗米修斯文档中的定义,在编写导出器时,它声明以下内容:

仅当 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() 对象上的 get 请求?我怎样才能做到这一点?

在我的阅读中,我发现 http.handlerfunc() 而不是 http.handler() ,它似乎走在正确的道路上,但我无法让它与 promhttp.handler() 一起工作,我我已经迷失了方向。


正确答案


经过一番查找终于明白了。希望这对将来的人有所帮助。

这是一个工作导出器,它满足仅在普罗米修斯刮擦发生时收集指标的要求

  • 没有无限循环,没有内部计时器。
  • 每次 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))
}

您引用的文档是关于编写导出器的。导出器通常是一个实用程序,它以自定义格式从另一个应用程序中提取指标并以 prometheus 格式公开它们。

您展示的代码是检测您自己的应用程序的正确方法。您的业​​务逻辑会随时更新指标,并且 http 服务器会在请求时提供当前指标值。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《灵活获取指标的Golang Prometheus导出器》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>