Using Prometheus to ingest from foreign APIs
kubernetes - Parsing JSON rest api response in Prometheus - Stack Overflow
How to get all the metrics of an instance with prometheus api? - Stack Overflow
kubernetes - Reading Prometheus metric using python - Stack Overflow
Greetings
I'm very new to Prometheus so I come here for pointers from the community.
My goal is to monitor a hardware device which exposes a nice REST API that contains all the data but not formatted specifically for monitoring.
What is the best approach/tool to have Prometheus "read" the data through this API?
Thanks in advance
ยป pip install prometheus-api-client
You can fetch all metrics with following curl expression:
url=http://{urIPorhostname}:9090
curl -s $url/api/v1/label/__name__/values | jq -r ".data[]" | sort
All the metrics for a particular instance IP:9100 can be obtained via the following PromQL query:
{instance="IP:9100"}
This query returns all the metrics, which have the given instance="IP:9100" label. See time series selector docs for more details. See also PromQL tutorial.
The {instance="IP:9100"} query can be sent to the following Prometheus querying APIs:
- /api/v1/query - this endpoint returns matching time series values at the given timestamp specified via
timequery arg. For example,curl 'http://prometheus:9090/api/v1/query?query={instance="IP:9100"}'. Make sure you properly encodedqueryarg with percent encoding. See this demo. - /api/v1/series - this endpoint returns matching time series without any data, e.g. only metric names and labels are returned. For example,
curl 'http://prometheus:9090/api/v1/series?match[]={instance="IP:9100"}'. Make sure you properly encodedmatch[]arg with percent encoding. See this demo - /api/v1/query_range - this endpoint returns calculated datapoints for matching time series on the selected time range
[start ... end]with the givenstepinterval between samples. See more details about calculated datapoints in these docs.
If you want returning just unique metric names without labels, then you can query /api/v1/label/__name__/values endpoint: curl http://prometheus:9090/api/v1/label/__name__/values . The __name__ is a special label name used in Prometheus for referring to metric names. Note that multiple time series may share the same metric name. For example, http_requests_total{path="/foo"} and http_requests_total{path="/bar"} time series share http_requests_total metric name, while they differ by path label values.
The code looks true, However,the query in your response command is wrong . the true formate is :
response =requests.get(PROMETHEUS + '/api/v1/query', params={'query': 'container_cpu_user_seconds_total'})
you can change "container_cpu_user_seconds_total" to any query that you want to read. .. good luck
Performing a GET request at <prom-server-ip>:9090/metrics returns the Prometheus metrics (not in JSON format) of the Prometheus server itself.
Since you're trying to perform query, you need to use the HTTP API endpoints like /api/v1/query or /api/v1/query_range instead of using /metrics.
$ curl 'http://localhost:9090/api/v1/query?query=up&time=2015-07-01T20:10:51.781Z'
{
"status" : "success",
"data" : {
"resultType" : "vector",
"result" : [
{
"metric" : {
"__name__" : "up",
"job" : "prometheus",
"instance" : "localhost:9090"
},
"value": [ 1435781451.781, "1" ]
},
{
"metric" : {
"__name__" : "up",
"job" : "node",
"instance" : "localhost:9100"
},
"value" : [ 1435781451.781, "0" ]
}
]
}
}
For more detailed information visit Prometheus official docs.
Your code is almost correct, just pass the query as a parameter to the URL. See this abbreviated snippet of code taken from here:
response = requests.get('http://localhost:9090/api/v1/query'),
params={'query': "query=container_cpu_load_average_10s{container_name=POD}"})
print(response.json()['data']['result'])
#!/usr/bin/env python
import requests
prome_sql = "(node_memory_MemTotal_bytes - (node_memory_MemFree_bytes + node_memory_Buffers_bytes + node_memory_Cached_bytes)) / node_memory_MemTotal_bytes * 100"
response = requests.get('http://192.168.20.249:9090/api/v1/query',
params={'query': prome_sql})
print(response.json()["data"]['result'])