# PromQL操作符

## 对时间序列进行算术运算

我们可以通过下面的表达式来查询主机的可用内存（以Mb为单位）

```
node_memory_MemAvailable_bytes / 1024 / 1024
```

上面的表达式中，`node_memory_MemAvailable_bytes`称为一个向量，`1024`称为一个标量（在任何时间点值都不变）

而向量与向量之间的运算则复杂一些，比如查询主机内存使用率的表达式

```
(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes
```

那这个表达式是如何工作的呢？

**依次找到与左边向量元素匹配（标签完全一致）的右边向量元素进行运算，如果没找到匹配元素，则直接丢弃。同时新的时间序列将不会包含指标名称。**

该表达式返回结果的示例如下所示：

```
{instance="peng01","job"="node_exporter"} 0.4
{instance="peng02","job"="node_exporter"} 0.98
```

算术运算符有`+ - * / % ^`

**使用算术运算符对时间序列进行算术运算，得到的还是时间序列**

## 使用关系运算符过滤时间序列

我们可以通过上面的表达式，查找主机的内存使用率，而如果我们只是想要查找内存使用率大于95%的主机呢？

那么我们可以使用下面的表达式：

```
(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes > 0.95
```

该表达式我们称为布尔表达式，其返回的依然是时间序列，如下：

```
{instance="peng02","job"="node_exporter"} 0.98
```

这里的返回会和我们平时的理解有些差异。其返回结果依旧是：左边算术表达式的时间序列。

比如，下面的表达式返回的就是主机的内存使用字节数（只返回内存使用率超过95%的主机）

```
node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes > 0.95 * node_memory_MemTotal_bytes
```

关系运算符有：`== != > < >= <=`。

**使用关系运算符对时间序列进行过滤，得到的还是时间序列**

## 逻辑运算符

逻辑运算符有：与（`and`）、或（`or`）、非（`unless`）。

to be continued


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pshizhsysu.gitbook.io/prometheus/4e8c29-tan-suo-promql/promqlcao-zuo-fu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
