1

I deployed Prometheus Node Exporter pod on k8s. It worked fine.

But when I try to get system metrics by calling Node Exporter metric API in my custom Go application

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.

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

I checked Prometheus Node Exporter GitHub Issues and someone recommended prom2json. But this is not I'm looking for. Because I have to run extra process to execute prom2json to get results. I want to get Node Exporter's system metric by simply calling HTTP request or some kind of Go native packages in my code.

How can I get those Node Exporter metrics in JSON format?

4

1 回答 1

1

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

存储库中的示例可执行文件具有您需要的所有构建块。首先,打开 URL,然后使用prom2json包读取数据存储结果

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


于 2019-12-12T17:01:16.317 回答