设置 PromQL 值的格式

Ngọ*_*ình 2 prometheus slack promql

我使用此查询规则进行警报:

- alert: HostOutOfMemory

    expr: (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100 > 90

    for: 5m

    labels:

      severity: warning

    annotations:

      summary: "{{ $labels.name }} out of memory "

      description: "Host memory is {{ $value }}%"
Run Code Online (Sandbox Code Playgroud)

但是该值是float(PromQL的默认值),我想格式化它(如下图,我可以将其更改为仅显示90%),我该怎么办?

在此输入图像描述

谢谢您阅读此篇。

Mic*_*bez 5

Prometheus 模板语言基于Go 模板系统文档中有很多例子。

在您的具体情况下,您将使用:

 description: Host memory is {{ $value | printf "%.2f%" }}.
Run Code Online (Sandbox Code Playgroud)

Prometheus 中还有一些有趣的内置函数,例如humanizePercentage

- alert: HostOutOfMemory
  expr: (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) > 0.9
  ...
  annotations:
    description: Host memory is {{ $value | humanizePercentage }}
Run Code Online (Sandbox Code Playgroud)

  • humanizePercentage 需要一个分数比(0 到 1 之间)。请参阅https://prometheus.io/docs/prometheus/latest/configuration/template_reference/#numbers (3认同)