How to get Prometheus Node Exporter metrics with JSON format

12/12/2019

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?

-- JunsungChoi
go
kubernetes
prometheus
prometheus-node-exporter

1 Answer

12/12/2019

You already mentioned prom2json and you can pull the package into your Go file by importing github.com/prometheus/prom2json.

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.

However, you should also have a look at expfmt.TextParser as that is the native way to ingest Prometheus formatted metrics.


-- Oliver
Source: StackOverflow