简体   繁体   English

如何获取 JSON 格式的 Prometheus Node Exporter 指标

[英]How to get Prometheus Node Exporter metrics with JSON format

I deployed Prometheus Node Exporter pod on k8s.我在 k8s 上部署了 Prometheus Node Exporter pod。 It worked fine.它工作得很好。

But when I try to get system metrics by calling Node Exporter metric API in my custom Go application但是当我尝试通过在我的自定义 Go 应用程序中调用 Node Exporter metric API 来获取系统指标时

curl -X GET "http://[my Host]:9100/metrics"

The result format was like this结果格式是这样的

# 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

Those long texts are hard to parse and I want to get the results in JSON format to parse them easily.那些长文本很难解析,我想以 JSON 格式获取结果以轻松解析它们。

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

I checked Prometheus Node Exporter GitHub Issues and someone recommended prom2json .我检查了 Prometheus Node Exporter GitHub 问题,有人推荐了prom2json But this is not I'm looking for.但这不是我要找的。 Because I have to run extra process to execute prom2json to get results.因为我必须运行额外的进程来执行 prom2json 才能获得结果。 I want to get Node Exporter's system metric by simply calling HTTP request or some kind of Go native packages in my code.我想通过在我的代码中简单地调用 HTTP 请求或某种 Go 本地包来获取 Node Exporter 的系统指标。

How can I get those Node Exporter metrics in JSON format?如何以 JSON 格式获取这些节点导出器指标?

You already mentioned prom2json and you can pull the package into your Go file by importing github.com/prometheus/prom2json .您已经提到了prom2json并且您可以通过导入github.com/prometheus/prom2json将包拉入您的 Go 文件中。

The sample executable in the repo has the all building blocks you need.存储库中的示例可执行文件包含您需要的所有构建块。 First, open the URL and then use the prom2json package to read the data and store the result . 首先打开URL ,然后使用prom2json包读取数据存储结果

However, you should also have a look at expfmt.TextParser as that is the native way to ingest Prometheus formatted metrics.但是,您还应该查看expfmt.TextParser,因为这是摄取 Prometheus 格式指标的本机方式。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM