PromQL和node-exporter:最近一分钟服务器的内存峰值消耗

huangapple go评论57阅读模式
英文:

PromQL and node-exporter: Peak memory consumption on a server during last minute

问题

我尝试使用PromQL和node-exporter指标在服务器上获取最近一分钟的峰值内存使用情况。当我像这样使用Prometheus API时:

```bash
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]'

我收到以下错误:{"status":"error","errorType":"bad_data","error":"invalid parameter \"query\": 1:143: parse error: unexpected identifier \"node_memory_Cached_bytes\""}

我应该指出,当我运行curl -X POST -g 'http://epgt012:9070/api/v1/query?query=node_memory_Cached_bytes{instance="epgp003:9401"}'时,我得到了正确的响应。

一旦我在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])

我收到以下错误:Error executing query: invalid parameter "query": 1:268: parse error: ranges only allowed for vector selectors

看起来我在这里有两个问题。最终我需要使用curl获取结果。我尝试了上述查询的各种不同变体,但无法得到有效的查询。

此外,使用query_range API调用在某个时间点(“start”和“end”)之间获取服务器的峰值内存使用情况也是不错的选择,而不是在最后一分钟内获取。


<details>
<summary>英文:</summary>

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: `{&quot;status&quot;:&quot;error&quot;,&quot;errorType&quot;:&quot;bad_data&quot;,&quot;error&quot;:&quot;invalid parameter \&quot;query\&quot;: 1:143: parse error: unexpected identifier \&quot;node_memory_Cached_bytes\&quot;&quot;}`

I should note that when I run `curl -X POST -g &#39;http://epgt012:9070/api/v1/query?query=node_memory_Cached_bytes{instance=&quot;epgp003:9401&quot;}&#39;` 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 &quot;query&quot;: 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&#39;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.

</details>


# 答案1
**得分**: 2

`+` 在 URL 中具有特殊含义,需要在使用时进行编码。

你的参数 `query` 预期如下:

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])


你可以通过手动对参数进行编码,或者使用类似 [`--data-urlencode`][1] 这样的方法来实现。

PromQL 不允许直接将范围选择器应用于复杂选择器,它仅适用于向量选择器。但你可以使用 [子查询][2] 语法。在这种情况下,你的查询将如下所示:

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:])


  [1]: https://stackoverflow.com/a/2027690/21363224
  [2]: https://prometheus.io/docs/prometheus/latest/querying/basics/#subquery

<details>
<summary>英文:</summary>

#### 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`][1]

#### Your promQL problem
PromQL doesn&#39;t allow to apply range selector straight to the complex selector, it is only applicable to the vector selector. But you can use [subquery][2] 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:])



  [1]: https://stackoverflow.com/a/2027690/21363224
  [2]: https://prometheus.io/docs/prometheus/latest/querying/basics/#subquery

</details>



huangapple
  • 本文由 发表于 2023年7月12日 21:47:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定