1

I try to obtain peak memory consumption on a server during last minute using PromQL and node-exporter metrics. When I use prometheus API like this:

curl -X POST -g 'http://epgt012:9070/api/v1/query?query=max_over_time(node_memory_MemTotal_bytes{instance="epgp003:9401"}-(node_memory_MemFree_bytes{instance="epgp003:9401"}+node_memory_Cached_bytes{instance="epgp003:9401"}+node_memory_Buffers_bytes{instance="epgp003:9401"})[1m])'

I get this error: {"status":"error","errorType":"bad_data","error":"invalid parameter \"query\": 1:143: parse error: unexpected identifier \"node_memory_Cached_bytes\""}

I should note that when I run curl -X POST -g 'http://epgt012:9070/api/v1/query?query=node_memory_Cached_bytes{instance="epgp003:9401"}' I get proper response.

Once I run that query inside prometheus UI:

max_over_time(node_memory_MemTotal_bytes{instance="epgp003:9401"}-(node_memory_MemFree_bytes{instance="epgp003:9401"}+node_memory_Cached_bytes{instance="epgp003:9401"}+node_memory_Buffers_bytes{instance="epgp003:9401"})[1m])

I get this error: Error executing query: invalid parameter "query": 1:268: parse error: ranges only allowed for vector selectors.

So it looks like I have two problems here. At the end I need to obtain results using curl. I tried all kind of different variations of the above, but couldn't get a working query.

Additionally it also would be nice to get peak memory consumption on a server between point in time - start and end (and not during last minute) using query_range api call.

user1876484
  • 610
  • 6
  • 16

1 Answers1

2

Your original problem with curl

+ has a special meaning in URLs, and need to be encoded when used there.

Your parameter query is expected to be

max_over_time(node_memory_MemTotal_bytes{instance%3D"epgp003%3A9401"}-(node_memory_MemFree_bytes{instance%3D"epgp003%3A9401"}%2Bnode_memory_Cached_bytes{instance%3D"epgp003%3A9401"}%2Bnode_memory_Buffers_bytes{instance%3D"epgp003%3A9401"})[1m])

You can achieve this by manually encoding your parameters, or using something like --data-urlencode

Your promQL problem

PromQL doesn't allow to apply range selector straight to the complex selector, it is only applicable to the vector selector. But you can use subquery syntax. In this case you query will look like this:

max_over_time((node_memory_MemTotal_bytes{instance="epgp003:9401"}-(node_memory_MemFree_bytes{instance="epgp003:9401"}+node_memory_Cached_bytes{instance="epgp003:9401"}+node_memory_Buffers_bytes{instance="epgp003:9401"}))[1m:])
markalex
  • 8,623
  • 2
  • 7
  • 32
  • Thank you @markalex! Maybe you also have an answer to the [CPU usage related question](https://stackoverflow.com/q/76721902/1876484)? – user1876484 Jul 20 '23 at 08:41
  • how will this query look like if instead of last minute `[1m:]` I have a `query_range` with a `start` an `end` and a `step`? Thank you! – user1876484 Jul 24 '23 at 12:49
  • @user1876484, sorry, I'm not sure I understand what you want. query_range parameters has nothing to do with range selector in query. – markalex Jul 24 '23 at 12:57
  • I mean, that now I need to get peak memory consumption on a server between point in time - `start` and `end` (and not as before - during last minute). Thank you! – user1876484 Jul 24 '23 at 13:01
  • @user1876484, then you'll need to calculate difference between end and start, convert it to prometheus notation and supply it as a range selector (still using `query` endpoint). Or use `query_range` and select maximum manually. – markalex Jul 24 '23 at 13:17