登录
首页 >  Golang >  Go问答

获取 Prometheus Node Exporter 输出的 JSON 格式指标

来源:stackoverflow

时间:2024-03-01 08:27:24 301浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《获取 Prometheus Node Exporter 输出的 JSON 格式指标》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

我在 k8s 上部署了 prometheus node exporter pod。效果很好。

但是当我尝试通过在自定义 go 应用程序中调用 node exporter 指标 api 来获取系统指标时

curl -x get "http://[my host]:9100/metrics"

结果格式是这样的

# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 1.7636e-05
go_gc_duration_seconds{quantile="0.25"} 2.466e-05
go_gc_duration_seconds{quantile="0.5"} 5.7992e-05
go_gc_duration_seconds{quantile="0.75"} 9.1109e-05
go_gc_duration_seconds{quantile="1"} 0.004852894
go_gc_duration_seconds_sum 1.291217651
go_gc_duration_seconds_count 11338
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 8
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.12.5"} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 2.577128e+06
# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE go_memstats_alloc_bytes_total counter
go_memstats_alloc_bytes_total 2.0073577064e+10
.
.
.
something like this

那些长文本很难解析,我想获取 json 格式的结果以便轻松解析它们。

https://github.com/prometheus/node_exporter/issues/1062

我检查了 prometheus node exporter github issues,有人推荐了 prom2json。 但这不是我要找的。因为我必须运行额外的进程来执行 prom2json 才能获得结果。我想通过简单地在代码中调用 http 请求或某种 go 原生包来获取 node exporter 的系统指标。

如何获取 json 格式的节点导出器指标?


解决方案


您已经提到了 prom2json,您可以通过导入 github.com/prometheus/prom2json 将软件包拉入您的 Go 文件中。

sample executable in the repo 拥有您需要的所有构建模块。 First, open the URL,然后是 use the prom2json package to read the datastore the result

但是,您还应该查看 expfmt.TextParser,因为这是提取 Prometheus 格式指标的本机方式。

理论要掌握,实操不能落!以上关于《获取 Prometheus Node Exporter 输出的 JSON 格式指标》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

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