Skip to content

Commit b183aba

Browse files
authored
Merge pull request #1063 from flashcatcloud/docs/monitors-append-label-template
docs(monitors): document Go templates in alert rule additional labels
2 parents 23d063b + a86a05d commit b183aba

6 files changed

Lines changed: 122 additions & 4 deletions

File tree

‎en/monitors/alert-rules/description-template.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ available through the `sprig_` prefix.
5151
| `$labels` | `map[string]string` | Alert labels. Same data as `.DataLabels`. |
5252
| `$values` | `map[string]float64` | Numeric values used by alert evaluation. Same data as `.Values`. |
5353
| `$value` | `float64` | Main alert value. Same data as `.Value`. |
54-
| `$appendLabels` | `map[string]string` | Extra labels configured on the alert rule. |
54+
| `$appendLabels` | `map[string]string` | Extra labels configured on the alert rule. Additional labels that use Go templates appear here as raw template text and are rendered only before delivery. See [Additional label syntax](/en/monitors/quickstart/quickstart#append-label-syntax). |
5555
| `$annotations` | `map[string]string` | Custom rule fields and additional information carried from query results. |
5656
| `$dsType` | `string` | Data source type. |
5757
| `$dsName` | `string` | Data source name. |

‎en/monitors/alert-rules/query-result-fields.mdx‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,37 @@ The `$` prefix identifies query-provided fields. A custom field configured on th
142142
A No data alert carries the additional information from the last successful result for that alert object. If the query has never returned data, no query-provided additional information is available.
143143
</Note>
144144

145+
<span id="query-fields-as-labels"></span>
146+
## Sending additional information as alert labels
147+
148+
Additional information reaches On-call with the `__alert_rule_annotation_` prefix, so channel notification templates, escalation rules, and [Alert pipeline](/en/on-call/integration/alert-integration/alert-pipelines) conditions that match ordinary label names (for example `labels.msg`) can't read it. When you need it, use a Go template in the rule's [Additional Labels](/en/monitors/quickstart/quickstart#append-label-syntax) to turn additional information into ordinary labels.
149+
150+
For example, to group error logs by service and still keep the latest log line in notifications:
151+
152+
```sql
153+
SELECT service, namespace, COUNT(*) AS value,
154+
MAX(datetime) AS datetime,
155+
MAX_BY(msg, datetime) AS msg,
156+
MAX_BY(trace_id, datetime) AS tid
157+
FROM error_logs
158+
WHERE datetime >= NOW() - INTERVAL 1 MINUTE
159+
GROUP BY service, namespace
160+
```
161+
162+
Select `service` and `namespace` as label fields, so `msg`, `tid`, and `datetime` become additional information. Then configure these additional labels:
163+
164+
```text
165+
msg = {{ index $annotations "$A.msg" }}
166+
tid = {{ index $annotations "$A.tid" }}
167+
datetime = {{ index $annotations "$A.datetime" }}
168+
```
169+
170+
Each service then produces one alert instance, and the `msg`, `tid`, and `datetime` sent to On-call carry the latest log line for that service. Existing notification templates and escalation rules keep working without changes.
171+
172+
<Note>
173+
Query-provided additional information requires monit-edge `v0.53.0` or later. Earlier alert engines don't provide it, so templates can't read these values.
174+
</Note>
175+
145176
## Query result row limits
146177

147178
Edge limits scheduled alert queries and interactive preview queries separately. The following defaults can be configured independently by the deployer:

‎en/monitors/quickstart/quickstart.mdx‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,35 @@ The create / edit alert rule page is one form with six sections, from top to bot
7676
|--------|------|
7777
| **Group** | The group path of the rule (for example, "Default / Database Alerts"). If the group has been deleted or you cannot access it, go back to the rule list and pick a group again |
7878
| **Rule Name** | Name of the alert rule; does not support variable references (fixed names facilitate filtering and grouping operations). It must be unique within its group; imports, edits, and moves fail if the target group already contains that name |
79-
| **Additional Labels** | Similar to `labels` in Prometheus; attached to all alert events for filtering, routing, and inhibition |
79+
| **Additional Labels** | Similar to `labels` in Prometheus; attached to all alert events for filtering, routing, and inhibition. See below for value syntax |
80+
81+
<span id="append-label-syntax"></span>
82+
#### Additional label syntax
83+
84+
An additional label value takes one of three forms, depending on its content:
85+
86+
| Form | Example | Handling |
87+
|------|---------|----------|
88+
| Plain text or `${label name}` | `team=ops`, `instance=${host}` | The alert engine replaces `${label name}` with the alert label |
89+
| Go template containing `{{` | `msg={{ index $annotations "$A.msg" }}` | Rendered before delivery to the channel and sent as an ordinary label |
90+
| Starts with `[TPL]` | `owner=[TPL]{{ .Labels.team }}` | Passed to On-call as is and rendered by On-call. See [Alert pipeline](/en/on-call/integration/alert-integration/alert-pipelines) for the syntax |
91+
92+
Go templates can use these variables:
93+
94+
| Variable | Description |
95+
|----------|-------------|
96+
| `$labels` | The alert's data labels, for example `{{ $labels.service }}` |
97+
| `$annotations` | Query-provided additional information and the rule's custom fields. Query-provided keys use `$<query name>.<field name>` and must be read with `index`, for example `{{ index $annotations "$A.msg" }}`. See [Query result field mapping](/en/monitors/alert-rules/query-result-fields#query-fields-as-labels) |
98+
| `$values` | Values used in alert evaluation, for example `{{ index $values "$A.value" }}` |
99+
| `$severity` | Alert severity |
100+
| `$status` | `firing` or `recovered` |
101+
102+
- Templates support only `if` / `else` plus `index`, `len`, `printf`, `print`, `replace`, `urlquery`, and the comparison and logical functions. `range`, `with`, and `define` aren't supported.
103+
- A missing key reads as an empty string in `$labels` and `$annotations`, and as `0` in `$values`. A label that renders to an empty string isn't sent.
104+
- A single value can't mix `${...}` and `{{...}}`.
105+
- A label that fails to render isn't sent, and the alert event is still delivered. The error goes to the `__alert_rule_label_template_error` label, which you can see in the alert details.
106+
- Leading and trailing whitespace in label names is trimmed on save, and the trimmed names must be unique. Label names can't start with `__alert_rule_`. All additional labels together are limited to 1,024 characters as JSON, and `job` and `instance` values to 255 characters.
107+
- When the alert engine renders descriptions, custom fields, and titles, template labels in `$appendLabels` are still the raw template text. To use query results there, reference `$annotations` directly.
80108

81109
### Data sources and queries
82110

‎zh/monitors/alert-rules/description-template.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ monit-edge 会使用 Sprig 标准函数名注册安全的 Sprig 函数。同一
4747
| `$labels` | `map[string]string` | 告警标签,与 `.DataLabels` 相同。 |
4848
| `$values` | `map[string]float64` | 告警计算中使用到的数值,与 `.Values` 相同。 |
4949
| `$value` | `float64` | 主告警值,与 `.Value` 相同。 |
50-
| `$appendLabels` | `map[string]string` | 规则上配置的附加标签。 |
50+
| `$appendLabels` | `map[string]string` | 规则上配置的附加标签。使用 Go 模板的附加标签在这里是模板原文,推送前才会渲染,见[附加标签的写法](/zh/monitors/quickstart/quickstart#append-label-syntax)。 |
5151
| `$annotations` | `map[string]string` | 规则自定义字段和查询结果携带的附加信息。 |
5252
| `$dsType` | `string` | 数据源类型。 |
5353
| `$dsName` | `string` | 数据源名称。 |

‎zh/monitors/alert-rules/query-result-fields.mdx‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,37 @@ Critical: $A.error_count > 20 and $B.latency_ms > 1000
142142
数据缺失告警会携带该告警对象最后一次成功查询时的附加信息。如果查询从未返回过数据,则没有可携带的查询附加信息。
143143
</Note>
144144

145+
<span id="query-fields-as-labels"></span>
146+
## 把附加信息作为告警标签推送
147+
148+
附加信息推送到 On-call 时带有 `__alert_rule_annotation_` 前缀,协作空间的通知模板、分派策略和[告警处理](/zh/on-call/integration/alert-integration/alert-pipelines)按普通标签名(例如 `labels.msg`)匹配时读不到它们。需要时,可以在规则的[附加标签](/zh/monitors/quickstart/quickstart#append-label-syntax)中用 Go 模板把附加信息转成普通标签。
149+
150+
例如按服务聚合错误日志,同时在通知中保留最新一条日志:
151+
152+
```sql
153+
SELECT service, namespace, COUNT(*) AS value,
154+
MAX(datetime) AS datetime,
155+
MAX_BY(msg, datetime) AS msg,
156+
MAX_BY(trace_id, datetime) AS tid
157+
FROM error_logs
158+
WHERE datetime >= NOW() - INTERVAL 1 MINUTE
159+
GROUP BY service, namespace
160+
```
161+
162+
标签字段选择 `service`、`namespace`,`msg`、`tid`、`datetime` 自动成为附加信息。再配置附加标签:
163+
164+
```text
165+
msg = {{ index $annotations "$A.msg" }}
166+
tid = {{ index $annotations "$A.tid" }}
167+
datetime = {{ index $annotations "$A.datetime" }}
168+
```
169+
170+
这样每个服务只产生一个告警实例,推送给 On-call 的 `msg`、`tid`、`datetime` 是该服务最新一条日志的真实内容,现有通知模板和分派策略无需修改。
171+
172+
<Note>
173+
查询附加信息需要 monit-edge `v0.53.0` 或以上版本。更早版本的告警引擎不提供这些附加信息,模板中读不到对应的值。
174+
</Note>
175+
145176
## 查询结果行数上限
146177

147178
Edge 分别限制周期告警查询和交互预览查询。以下是默认值,部署者可以独立调整:

‎zh/monitors/quickstart/quickstart.mdx‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,35 @@ keywords: ["入门指南", "monitedge", "数据源", "告警规则", "快速开
7777
|--------|------|
7878
| **所属分组** | 规则所在的分组路径(如“默认分组 / 数据库告警”)。如果分组已被删除或您无权访问,需要返回规则列表重新选择分组 |
7979
| **规则名称** | 告警规则的名称,不支持引用变量(固定名称便于过滤、聚合操作)。同一分组内必须唯一;导入、编辑或移动规则时如与目标分组中已有规则重名,操作会失败 |
80-
| **附加标签** | 类似 Prometheus 中的 `labels`,会附加到所有告警事件上,便于过滤、路由、抑制 |
80+
| **附加标签** | 类似 Prometheus 中的 `labels`,会附加到所有告警事件上,便于过滤、路由、抑制。值的写法见下文 |
81+
82+
<span id="append-label-syntax"></span>
83+
#### 附加标签的写法
84+
85+
附加标签的值有三种写法,按值的内容区分:
86+
87+
| 写法 | 示例 | 处理方式 |
88+
|------|------|----------|
89+
| 普通文本或 `${标签名}` | `team=ops`、`instance=${host}` | 告警引擎用告警标签替换 `${标签名}` |
90+
| 包含 `{{` 的 Go 模板 | `msg={{ index $annotations "$A.msg" }}` | 推送到协作空间之前渲染,渲染结果作为普通标签推送 |
91+
| 以 `[TPL]` 开头 | `owner=[TPL]{{ .Labels.team }}` | 原样交给 On-call,由 On-call 渲染,写法见[告警处理](/zh/on-call/integration/alert-integration/alert-pipelines) |
92+
93+
Go 模板可以使用以下变量:
94+
95+
| 变量 | 说明 |
96+
|------|------|
97+
| `$labels` | 告警的数据标签,例如 `{{ $labels.service }}` |
98+
| `$annotations` | 查询结果附加信息和规则自定义字段。查询附加信息的 key 为 `$<查询名称>.<字段名称>`,需要用 `index` 读取,例如 `{{ index $annotations "$A.msg" }}`。详见[查询结果字段映射](/zh/monitors/alert-rules/query-result-fields#query-fields-as-labels) |
99+
| `$values` | 告警计算中使用到的数值,例如 `{{ index $values "$A.value" }}` |
100+
| `$severity` | 告警级别 |
101+
| `$status` | `firing` 或 `recovered` |
102+
103+
- 模板只支持 `if` / `else` 和 `index`、`len`、`printf`、`print`、`replace`、`urlquery` 以及比较、逻辑函数,不支持 `range`、`with`、`define`。
104+
- 读取不存在的 key 时,`$labels` 和 `$annotations` 得到空字符串,`$values` 得到 `0`。渲染结果为空的标签不会推送。
105+
- 同一个值不能同时使用 `${...}` 和 `{{...}}`。
106+
- 渲染失败的标签不会推送,告警事件照常推送,错误原因写入 `__alert_rule_label_template_error` 标签,可在告警详情中查看。
107+
- 标签名首尾的空白会在保存时去掉,去掉后不能重复;标签名不能以 `__alert_rule_` 开头。全部附加标签按 JSON 计合计不超过 1024 个字符,`job` 和 `instance` 的值不超过 255 个字符。
108+
- 告警引擎渲染备注描述、自定义字段和标题时,`$appendLabels` 中的模板标签仍是模板原文。需要在这些位置使用查询结果时,请直接引用 `$annotations`。
81109

82110
### 数据源与查询
83111

0 commit comments

Comments
 (0)