query_range - error with RFC3339 start/end time format

460 views
Skip to first unread message

shirley

unread,
Aug 30, 2022, 6:18:00 AM8/30/22
to Prometheus Users
Hello,

I am trying to query Prometheus metrics using curl. From what I read on the Prometheus HTTP API, I need to supply start and end times in RFC3339 or timestamp format.

The query I want to produce:
curl -s -g -k -X GET -H "Authorization: Bearer $TOKEN" -H 'Accept: application/json' -H 'Content-Type: application/json' "$URL/api/v1/query_range?query=(sum(rate(container_cpu_usage_seconds_total{pod!=\"\", container!=\"\"}[1h]))by(namespace))&start=2022-08-21T07:00:00+03:00&end=2022-08-21T09:00:00+03:00&step=1h"

The following error occurred when I used rfc3339 format: "Cannot parse \"2022-08-21T07:00:00 03:00\" to a valid timestamp".
Any time zone that starts with "-" (for example, -03:00) or the default (Z) gives me results. The "+" appears to be converted to a space.

My other attempts were:
1. I declared the "+" as a variable (export $var="+")
2. I wrote the query under ' ' (I wrote the full URL address)
I still receive the same error message.


Thanks in advance,
Shirley

Brian Candler

unread,
Aug 30, 2022, 7:12:52 AM8/30/22
to Prometheus Users
Firstly, drop off the "-s" from your curl line.  Or use "-Ss" if you want to suppress output in non-error situations, but still see errors when they occur.

That should give you a visible error message:
curl: (3) URL using bad/illegal format or missing URL

So the problem is that curl itself is rejected the URL as malformed - it's not even getting to Prometheus.

Then trim it down to see where the problem occurs
"$URL/api/v1/query_range"    # this is OK (at least, the request reaches Prometheus and gives an error about missing start value)
"$URL/api/v1/query_range?query=(sum(rate(container_cpu_usage_seconds_total{pod!=\"\", container!=\"\"}[1h]))by(namespace))"   # this is rejected by curl
"$URL/api/v1/query_range?query=(sum(rate(container_cpu_usage_seconds_total{pod!=\"\",container!=\"\"}[1h]))by(namespace))"   # this is accepted

I needed to remove the space after the comma in the URL.

As for the plus sign: "+" is actually the way to represent a space in a URL ("%20" is another way).  If you want to insert a plus sign, then use %2B

With all those changes, that gives:

$ curl -Ss -g -k -X GET -H "Authorization: Bearer $TOKEN" -H 'Accept: application/json' -H 'Content-Type: application/json' "$URL/api/v1/query_range?query=(sum(rate(container_cpu_usage_seconds_total{pod!=\"\",container!=\"\"}[1h]))by(namespace))&start=2022-08-21T07:00:00%2B03:00&end=2022-08-21T09:00:00%2B03:00&step=1h"
{"status":"success","data":{"resultType":"matrix","result":[]}}


(although you don't actually need the "Content-Type: application/json" header, since your request has no body)
Reply all
Reply to author
Forward
0 new messages