Nimander
Nimander

Reputation: 77

How to get timeseries of CPU usage from Prometheus api

I have a Prometheus running in Kubernetes that monitors deployment of my app. I can see the cpu usage and other metrics as time series on http://localhost:8080/. But how can I get that time series from Prometheus API? I only found endpoint that returns list of avaible metrics, but not the data itself. Is there a way to simply get CPU usage from Prometheus API?

Upvotes: 0

Views: 2018

Answers (2)

valyala
valyala

Reputation: 17800

Prometheus returns raw samples for the given time series selector on the time range (time-d .. time] when querying /api/v1/query. The d is the lookbehind window specified in square brackets after the time series selector. For example, the following command returns raw samples for time series with the name node_cpu_seconds_total on the time range (2022-09-07T00:00:00Z .. 2022-09-08T00:00:00Z]:

curl http://prometheus:9090/api/v1/query?query=node_cpu_seconds_total[1d]&time=2022-09-08T00:00:00Z | jq .

Pay attention that the query param must be properly encoded with url encoding. Otherwise the query may return unexpected results.

See also this article.

Upvotes: 1

LittleFox
LittleFox

Reputation: 327

You can use the query API for that.

Basically (assuming Prometheus is running at localhost:8080) http://localhost:8080/api/v1/query_range?query=node_cpu_seconds_total&start=2022-09-07T00:00:00Z&end=2022-09-07T02:00:00Z&step=5m, which returns your data in a JSON document.

Upvotes: 2

Related Questions