Skip to content

Commit 2cf31eb

Browse files
authored
docs(trigger): for and keep_firing_for (#2251)
1 parent 9c309f7 commit 2cf31eb

File tree

4 files changed

+168
-28
lines changed

4 files changed

+168
-28
lines changed

docs/reference/sql/trigger-syntax.md

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ CREATE TRIGGER [IF NOT EXISTS] <trigger_name>
2121
ON (<query_expression>) EVERY <interval_expression>
2222
[LABELS (<label_name>=<label_val>, ...)]
2323
[ANNOTATIONS (<annotation_name>=<annotation_val>, ...)]
24+
[FOR <interval_expression>]
25+
[KEEP FIRING FOR <interval_expression>]
2426
NOTIFY (
2527
WEBHOOK <notify_name1> URL '<url1>' [WITH (<parameter1>=<value1>, ...)],
2628
WEBHOOK <notify_name2> URL '<url2>' [WITH (<parameter2>=<value2>, ...)]
@@ -34,12 +36,11 @@ CREATE TRIGGER [IF NOT EXISTS] <trigger_name>
3436

3537
#### Query expression
3638

37-
The SQL query which be executed periodically. The notification will be fired
38-
if query result is not empty. If query result has multiple rows, a notification
39-
will be fired for each row.
39+
The SQL query specified in the `ON` clause is executed periodically. Its evaluation
40+
result may produce one or more alert instances, depending on the returned rows.
4041

41-
In addition, the Trigger will extract the `labels` and `annotations` from the
42-
query result, and attach them to the notification message along with the key-value
42+
The Trigger extracts `labels` and `annotations` from the query result and attaches
43+
them to each alert instance. These values are combined with the static key-value
4344
pairs specified in the `LABELS` and `ANNOTATIONS` clauses.
4445

4546
The extraction rules are as follows:
@@ -48,6 +49,9 @@ The extraction rules are as follows:
4849
of labels is the column name or alias without the `label_` prefix.
4950
- Extract the other columns to `ANNOTATIONS`. The key of annotations is the
5051
column name.
52+
53+
It is worth noting that each alert instance is uniquely identified by its label
54+
set. Multiple rows with the same labels will only create a single alert instance.
5155

5256
For example, the query expression is as follows:
5357

@@ -64,9 +68,9 @@ Assume the query result is not empty and looks like this:
6468
| collector1 | host1 | 12 |
6569
| collector2 | host2 | 15 |
6670

67-
This will generate two notifications.
71+
This will generate two alert instances.
6872

69-
The first notification will have the following labels and annotations:
73+
The first alert instance will have the following labels and annotations:
7074

7175
- Labels:
7276
- collector: collector1
@@ -76,7 +80,7 @@ The first notification will have the following labels and annotations:
7680
- val: 12
7781
- the annotations defined in the `ANNOTATIONS` clause
7882

79-
The second notification will have the following labels and annotations:
83+
The second alert instance will have the following labels and annotations:
8084

8185
- Labels:
8286
- collector: collector2
@@ -99,6 +103,40 @@ It indicates how often the query is executed. e.g., `'5 minute'::INTERVAL`,
99103

100104
For more details about how to write INTERVAL time, see [interval-type](/reference/sql/data-types.md#interval-type).
101105

106+
### FOR clause
107+
108+
The `FOR` clause controls how long an alert must remain active before it fires.
109+
Its behavior is similar to the `for` option in Prometheus Alerting Rules.
110+
111+
When an alert instance appears in the evaluation results for the first time, it
112+
enters the `Pending` state, during which no notification is sent. If the alert
113+
instance remains active throughout every evaluation within the duration specified
114+
by `FOR`, its state transitions from `Pending` to `Firing`, and a notification
115+
is sent immediately.
116+
117+
If the `FOR` clause is not specified, the alert will not enter the `Pending`
118+
state. Instead, an alert instance transitions to the `Firing` state immediately
119+
upon its first appearance in the evaluation results, and a notification is sent
120+
immediately.
121+
122+
### KEEP FIRING FOR clause
123+
124+
The `KEEP FIRING FOR` clause controls how long an alert instance should remain
125+
in the `Firing` state after it first enters that state. Its behavior is similar
126+
to the `keep_firing_for` option in Prometheus Alerting Rules.
127+
128+
Once an alert instance enters the `Firing` state, it will remain firing for at
129+
least the duration specified by `KEEP FIRING FOR`, even if it no longer appears
130+
in subsequent evaluation results.
131+
132+
After the `KEEP FIRING FOR` duration has passed, if the alert instance does not
133+
appear in the next evaluation, it will be marked as resolved and will no longer
134+
remain in the `Firing` state.
135+
136+
If the `KEEP FIRING FOR` clause is not specified, an alert instance will be
137+
marked as resolved in the first evaluation where it no longer appears in the
138+
query results after entering the `Firing` state.
139+
102140
### Labels and Annotations clauses
103141

104142
The LABELS and ANNOTATIONS clauses allow you to attach static key-value pairs
@@ -189,4 +227,3 @@ DROP TRIGGER IF EXISTS load1_monitor;
189227
## Example
190228

191229
Please refer to the [Trigger](/enterprise/trigger.md) documentation in the enterprise user guide for examples.
192-

i18n/zh/docusaurus-plugin-content-docs/current/reference/sql/trigger-syntax.md

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ CREATE TRIGGER [IF NOT EXISTS] <trigger_name>
2020
ON (<query_expression>) EVERY <interval_expression>
2121
[LABELS (<label_name>=<label_val>, ...)]
2222
[ANNOTATIONS (<annotation_name>=<annotation_val>, ...)]
23+
[FOR <interval_expression>]
24+
[KEEP FIRING FOR <interval_expression>]
2325
NOTIFY (
2426
WEBHOOK <notify_name1> URL '<url1>' [WITH (<parameter1>=<value1>, ...)],
2527
WEBHOOK <notify_name2> URL '<url2>' [WITH (<parameter2>=<value2>, ...)]
@@ -33,18 +35,21 @@ CREATE TRIGGER [IF NOT EXISTS] <trigger_name>
3335

3436
#### Query expression
3537

36-
指定的 SQL 查询会被定期执行。若查询结果非空,则触发通知;若查询结果包含多行,则
37-
每一行都会触发一条独立通知
38+
指定的 SQL 查询会被定期执行。每次查询的结果可能形成一个或多个告警实例,具体取决
39+
于返回的行
3840

39-
此外,Trigger 会从查询结果中提取 labels 与 annotations,并与 `LABELS``ANNOTATIONS`
40-
子句中指定的键值对一起附加到通知消息中
41+
Trigger 会从查询结果中提取 `labels``annotations`,并将这些信息附加到每个告警
42+
实例上。同时,它们也会与 `LABELS``ANNOTATIONS` 子句中指定的静态键值对合并
4143

4244
提取规则如下:
4345

4446
- 若列名(或别名)以 `label_` 开头,则将该列提取到 LABELS 中,键名为去掉 `label_`
4547
前缀后的列名(或别名)。
4648
- 其余所有列均提取到 ANNOTATIONS 中,键名即为列名(或别名)。
4749

50+
值得注意的是,每个告警实例都由其 Label 集合唯一标识。如果多行查询结果生成的 Label
51+
集合相同,它们只会形成同一个告警实例。
52+
4853
例如,查询表达式如下:
4954

5055
```sql
@@ -88,7 +93,35 @@ SELECT collect as label_collector, host as label_host, val
8893
- INTERVAL 表达式中**禁止**使用 `years``months`。月和年的时长是可变的,取决于具体的月份或年份,因此不适合用来定义固定的间隔。
8994
- 最小间隔为 1 秒。任何小于 1 秒的间隔都会被自动向上取整为 1 秒。
9095

91-
有关 INTERVA L表达式的更多语法细节,请参见 [interval-type](/reference/sql/data-types.md#interval-type)
96+
有关 INTERVAL 表达式的更多语法细节,请参见 [interval-type](/reference/sql/data-types.md#interval-type)
97+
98+
### FOR 子句
99+
100+
`FOR <interval_expression>` 子句用于控制某个告警在真正触发前需要保持活跃的持续时
101+
间,其作用与 Prometheus Alerting Rules 中的 `for` 选项类似。
102+
103+
当评估结果中首次出现某个告警实例时,它首先会进入 `Pending` 状态,此时不会触发通
104+
知。若该告警实例在 `FOR` 指定的持续时间内的每次评估中均保持活跃(即始终出现在查
105+
询结果中),则状态将由 `Pending` 转为 `Firing`,并立即发送通知。
106+
107+
若未指定 `FOR` 子句,则告警不会进入 `Pending` 状态,而是在评估结果中首次出现该告
108+
警实例时立即进入 `Firing` 状态,并马上触发通知。
109+
110+
### KEEP FIRING FOR 子句
111+
112+
`KEEP FIRING FOR <interval_expression>` 子句用于控制某个告警实例在首次进入 `Firing`
113+
状态后,至少需要保持多长时间处于 `Firing` 状态,其作用与 Prometheus Alerting Rules
114+
中的 `keep_firing_for` 选项类似。
115+
116+
当某个告警实例满足条件并进入 `Firing` 状态后,即便后续评估中该告警实例不再出现在
117+
查询结果中,只要距离首次进入 `Firing` 状态尚未超过 `KEEP FIRING FOR` 指定的时长,
118+
该告警实例仍会保持 `Firing` 状态。
119+
120+
一旦超过 `KEEP FIRING FOR` 指定的时长,在下一次评估中如果该告警实例仍未出现在查
121+
询结果中,则会被标记为已恢复,不再处于 Firing 状态。
122+
123+
若未指定 `KEEP FIRING FOR` 子句,则告警实例在进入 `Firing` 状态后,只要在后续评
124+
估中不再出现在查询结果中,就会在该次评估中被标记为已恢复。
92125

93126
### Labels 和 Annotations 子句
94127

i18n/zh/docusaurus-plugin-content-docs/version-1.0/reference/sql/trigger-syntax.md

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ CREATE TRIGGER [IF NOT EXISTS] <trigger_name>
2020
ON (<query_expression>) EVERY <interval_expression>
2121
[LABELS (<label_name>=<label_val>, ...)]
2222
[ANNOTATIONS (<annotation_name>=<annotation_val>, ...)]
23+
[FOR <interval_expression>]
24+
[KEEP FIRING FOR <interval_expression>]
2325
NOTIFY (
2426
WEBHOOK <notify_name1> URL '<url1>' [WITH (<parameter1>=<value1>, ...)],
2527
WEBHOOK <notify_name2> URL '<url2>' [WITH (<parameter2>=<value2>, ...)]
@@ -33,18 +35,21 @@ CREATE TRIGGER [IF NOT EXISTS] <trigger_name>
3335

3436
#### Query expression
3537

36-
指定的 SQL 查询会被定期执行。若查询结果非空,则触发通知;若查询结果包含多行,则
37-
每一行都会触发一条独立通知
38+
指定的 SQL 查询会被定期执行。每次查询的结果可能形成一个或多个告警实例,具体取决
39+
于返回的行
3840

39-
此外,Trigger 会从查询结果中提取 labels 与 annotations,并与 `LABELS``ANNOTATIONS`
40-
子句中指定的键值对一起附加到通知消息中
41+
Trigger 会从查询结果中提取 `labels``annotations`,并将这些信息附加到每个告警
42+
实例上。同时,它们也会与 `LABELS``ANNOTATIONS` 子句中指定的静态键值对合并
4143

4244
提取规则如下:
4345

4446
- 若列名(或别名)以 `label_` 开头,则将该列提取到 LABELS 中,键名为去掉 `label_`
4547
前缀后的列名(或别名)。
4648
- 其余所有列均提取到 ANNOTATIONS 中,键名即为列名(或别名)。
4749

50+
值得注意的是,每个告警实例都由其 Label 集合唯一标识。如果多行查询结果生成的 Label
51+
集合相同,它们只会形成同一个告警实例。
52+
4853
例如,查询表达式如下:
4954

5055
```sql
@@ -88,7 +93,35 @@ SELECT collect as label_collector, host as label_host, val
8893
- INTERVAL 表达式中**禁止**使用 `years``months`。月和年的时长是可变的,取决于具体的月份或年份,因此不适合用来定义固定的间隔。
8994
- 最小间隔为 1 秒。任何小于 1 秒的间隔都会被自动向上取整为 1 秒。
9095

91-
有关 INTERVA L表达式的更多语法细节,请参见 [interval-type](/reference/sql/data-types.md#interval-type)
96+
有关 INTERVAL 表达式的更多语法细节,请参见 [interval-type](/reference/sql/data-types.md#interval-type)
97+
98+
### FOR 子句
99+
100+
`FOR <interval_expression>` 子句用于控制某个告警在真正触发前需要保持活跃的持续时
101+
间,其作用与 Prometheus Alerting Rules 中的 `for` 选项类似。
102+
103+
当评估结果中首次出现某个告警实例时,它首先会进入 `Pending` 状态,此时不会触发通
104+
知。若该告警实例在 `FOR` 指定的持续时间内的每次评估中均保持活跃(即始终出现在查
105+
询结果中),则状态将由 `Pending` 转为 `Firing`,并立即发送通知。
106+
107+
若未指定 `FOR` 子句,则告警不会进入 `Pending` 状态,而是在评估结果中首次出现该告
108+
警实例时立即进入 `Firing` 状态,并马上触发通知。
109+
110+
### KEEP FIRING FOR 子句
111+
112+
`KEEP FIRING FOR <interval_expression>` 子句用于控制某个告警实例在首次进入 `Firing`
113+
状态后,至少需要保持多长时间处于 `Firing` 状态,其作用与 Prometheus Alerting Rules
114+
中的 `keep_firing_for` 选项类似。
115+
116+
当某个告警实例满足条件并进入 `Firing` 状态后,即便后续评估中该告警实例不再出现在
117+
查询结果中,只要距离首次进入 `Firing` 状态尚未超过 `KEEP FIRING FOR` 指定的时长,
118+
该告警实例仍会保持 `Firing` 状态。
119+
120+
一旦超过 `KEEP FIRING FOR` 指定的时长,在下一次评估中如果该告警实例仍未出现在查
121+
询结果中,则会被标记为已恢复,不再处于 Firing 状态。
122+
123+
若未指定 `KEEP FIRING FOR` 子句,则告警实例在进入 `Firing` 状态后,只要在后续评
124+
估中不再出现在查询结果中,就会在该次评估中被标记为已恢复。
92125

93126
### Labels 和 Annotations 子句
94127

versioned_docs/version-1.0/reference/sql/trigger-syntax.md

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ CREATE TRIGGER [IF NOT EXISTS] <trigger_name>
2121
ON (<query_expression>) EVERY <interval_expression>
2222
[LABELS (<label_name>=<label_val>, ...)]
2323
[ANNOTATIONS (<annotation_name>=<annotation_val>, ...)]
24+
[FOR <interval_expression>]
25+
[KEEP FIRING FOR <interval_expression>]
2426
NOTIFY (
2527
WEBHOOK <notify_name1> URL '<url1>' [WITH (<parameter1>=<value1>, ...)],
2628
WEBHOOK <notify_name2> URL '<url2>' [WITH (<parameter2>=<value2>, ...)]
@@ -34,12 +36,11 @@ CREATE TRIGGER [IF NOT EXISTS] <trigger_name>
3436

3537
#### Query expression
3638

37-
The SQL query which be executed periodically. The notification will be fired
38-
if query result is not empty. If query result has multiple rows, a notification
39-
will be fired for each row.
39+
The SQL query specified in the `ON` clause is executed periodically. Its evaluation
40+
result may produce one or more alert instances, depending on the returned rows.
4041

41-
In addition, the Trigger will extract the `labels` and `annotations` from the
42-
query result, and attach them to the notification message along with the key-value
42+
The Trigger extracts `labels` and `annotations` from the query result and attaches
43+
them to each alert instance. These values are combined with the static key-value
4344
pairs specified in the `LABELS` and `ANNOTATIONS` clauses.
4445

4546
The extraction rules are as follows:
@@ -48,6 +49,9 @@ The extraction rules are as follows:
4849
of labels is the column name or alias without the `label_` prefix.
4950
- Extract the other columns to `ANNOTATIONS`. The key of annotations is the
5051
column name.
52+
53+
It is worth noting that each alert instance is uniquely identified by its label
54+
set. Multiple rows with the same labels will only create a single alert instance.
5155

5256
For example, the query expression is as follows:
5357

@@ -64,9 +68,9 @@ Assume the query result is not empty and looks like this:
6468
| collector1 | host1 | 12 |
6569
| collector2 | host2 | 15 |
6670

67-
This will generate two notifications.
71+
This will generate two alert instances.
6872

69-
The first notification will have the following labels and annotations:
73+
The first alert instance will have the following labels and annotations:
7074

7175
- Labels:
7276
- collector: collector1
@@ -76,7 +80,7 @@ The first notification will have the following labels and annotations:
7680
- val: 12
7781
- the annotations defined in the `ANNOTATIONS` clause
7882

79-
The second notification will have the following labels and annotations:
83+
The second alert instance will have the following labels and annotations:
8084

8185
- Labels:
8286
- collector: collector2
@@ -99,6 +103,40 @@ It indicates how often the query is executed. e.g., `'5 minute'::INTERVAL`,
99103

100104
For more details about how to write INTERVAL time, see [interval-type](/reference/sql/data-types.md#interval-type).
101105

106+
### FOR clause
107+
108+
The `FOR` clause controls how long an alert must remain active before it fires.
109+
Its behavior is similar to the `for` option in Prometheus Alerting Rules.
110+
111+
When an alert instance appears in the evaluation results for the first time, it
112+
enters the `Pending` state, during which no notification is sent. If the alert
113+
instance remains active throughout every evaluation within the duration specified
114+
by `FOR`, its state transitions from `Pending` to `Firing`, and a notification
115+
is sent immediately.
116+
117+
If the `FOR` clause is not specified, the alert will not enter the `Pending`
118+
state. Instead, an alert instance transitions to the `Firing` state immediately
119+
upon its first appearance in the evaluation results, and a notification is sent
120+
immediately.
121+
122+
### KEEP FIRING FOR clause
123+
124+
The `KEEP FIRING FOR` clause controls how long an alert instance should remain
125+
in the `Firing` state after it first enters that state. Its behavior is similar
126+
to the `keep_firing_for` option in Prometheus Alerting Rules.
127+
128+
Once an alert instance enters the `Firing` state, it will remain firing for at
129+
least the duration specified by `KEEP FIRING FOR`, even if it no longer appears
130+
in subsequent evaluation results.
131+
132+
After the `KEEP FIRING FOR` duration has passed, if the alert instance does not
133+
appear in the next evaluation, it will be marked as resolved and will no longer
134+
remain in the `Firing` state.
135+
136+
If the `KEEP FIRING FOR` clause is not specified, an alert instance will be
137+
marked as resolved in the first evaluation where it no longer appears in the
138+
query results after entering the `Firing` state.
139+
102140
### Labels and Annotations clauses
103141

104142
The LABELS and ANNOTATIONS clauses allow you to attach static key-value pairs
@@ -189,4 +227,3 @@ DROP TRIGGER IF EXISTS load1_monitor;
189227
## Example
190228

191229
Please refer to the [Trigger](/enterprise/trigger.md) documentation in the enterprise user guide for examples.
192-

0 commit comments

Comments
 (0)