DisplayName
DisplayName

Reputation: 649

Prometheus curl API query issue

I am trying to construct curl API query to get Json data from Prometheus. The working in Prometheus UI query looks like:

max_over_time(container_memory_usage_bytes{image!="",pod_name=~"somepod-.*"}[7d])

So I am trying with:

curl 'http://127.0.0.1:20001/api/v1/query?query=max_over_time(container_memory_usage_bytes{(pod_name="somepod-.*")})[1d]' | jq

But depending on brackets it always complaining about something like expected or unexpected character.

Upvotes: 11

Views: 28066

Answers (2)

kim0
kim0

Reputation: 291

I use this query below to get a fairly complex query from prometheus and use jq to extract the interesting info then format it as csv

curl -fs --data-urlencode 'query=sort_desc( sum(container_memory_working_set_bytes) by (container_name, namespace) /sum(label_join(kube_pod_container_resource_requests_memory_bytes, "container_name", "", "container")) by (container_name, namespace) > 1)' https://prometheus/api/v1/query | jq -r '.data.result[] | [.metric.container_name, .metric.namespace, .value[1] ] | @csv'

You can view more info at https://learndevops.substack.com/p/hitting-prometheus-api-with-curl

Upvotes: 7

brian-brazil
brian-brazil

Reputation: 34142

You want:

curl -g 'http://127.0.0.1:20001/api/v1/query?query=max_over_time(container_memory_usage_bytes{pod_name=~"somepod-.*"}[1d])' | jq

This disables curl's globbing, which gets in the way here.

Upvotes: 12

Related Questions