How to Retrieve and Analyze Prometheus Monitoring Data via API
Learn how to use Prometheus's stable V1 API to query instant and range data, handle responses and errors, and fetch metrics programmatically with curl and Python, enabling data analysis, cost management, and advanced monitoring beyond basic alerts.
As a skilled engineer, leveraging Prometheus monitoring data can create value beyond alerts, such as analysis and cost management.
API Format
Prometheus provides a stable V1 API at /api/v1. It supports GET and POST requests and returns 2xx on success. Common error codes include 400 (Bad Request), 422 (Unprocessable Entity), and 503 (Service Unavailable).
Successful responses follow this JSON structure:
{
"status": "success" | "error",
"data": <data>,
// present only when status is "error"
"errorType": "<string>",
"error": "<string>",
"warnings": ["<string>"]
}API Calls
Instant query returns data for a single point in time.
GET /api/v1/query
POST /api/v1/queryParameters:
query=<string> – Prometheus expression.
time=<rfc3339 | unix_timestamp> – optional evaluation timestamp.
timeout=<duration> – optional timeout.
Example using curl:
curl http://localhost:9090/api/v1/query?query=node_load5{instance="192.168.214.108:9100"}Sample response:
{
"status":"success",
"data":{
"resultType":"vector",
"result":[
{
"metric":{
"__name__":"node_load5",
"instance":"192.168.214.108:9100",
"job":"node"
},
"value":[1666865246.993,"0.04"]
}
]
}
}Range query returns data over a time interval.
GET /api/v1/query_range
POST /api/v1/query_rangeParameters:
query=<string> – expression.
start=<rfc3339 | unix_timestamp> – start time.
end=<rfc3339 | unix_timestamp> – end time.
step=<duration | float> – resolution.
timeout=<duration> – optional timeout.
Example:
curl http://localhost:9090/api/v1/query_range?query=node_load5{instance="192.168.214.108:9100"}&start=2022-10-28T02:10:10.000Z&end=2022-10-28T02:13:00.000Z&step=60sSample response (matrix):
{
"status":"success",
"data":{
"resultType":"matrix",
"result":[
{
"metric":{
"__name__":"node_load5",
"instance":"192.168.214.108:9100",
"job":"node"
},
"values":[
[1666923010,"0.04"],
[1666923070,"0.04"],
[1666923130,"0.03"]
]
}
]
}
}Fetching Data in Code
In production, use a programming language such as Python. First install the requests library: pip install requests Example script test_api.py:
# -*- coding: utf-8 -*-
import requests
url = 'http://192.168.214.108:9090'
query_api = '/api/v1/query'
params = 'query=node_load5{instance="192.168.214.108:9100"}'
res = requests.get(url + query_api, params)
metrics = res.json().get("data").get("result")
if metrics:
value = metrics[0].get('value')[1]
print('Server 192.168.214.108 node_load5 value is %s' % value)
else:
print('Unable to retrieve data')Running the script prints: Server 192.168.214.108 node_load5 value is 0.01 For more advanced usage, refer to the official Prometheus API documentation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
