灵活获取指标的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学习网公众号了解相关技术文章。
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习