模板变量 #
简介 #
自定义告警触发事件内容时,除了自己撰写的固定文案外,事件标题、事件内容等也支持模板语法。可以使用事件中的字段实现文案的渲染。
模板变量 #
用于渲染字段的语法为{{ .字段名 }}
,可用于模板内容渲染的变量字段如下:
变量字段名 | 字段类型 | 说明 | 示例 |
---|---|---|---|
rule_id | string | rule uuid | c9f663tath2e5a0vksjg |
rule_name | string | rule name | High CPU usage |
resource_id | string | resource uuid | c9f663tath2e5a0vksjg |
resource_name | string | resource name | es-v716 |
event_id | string | identifier for check details | c9f663tath2e5a0vksjx |
timestamp | number | Millisecond timestamp | 1654595042399 |
first_group_value | string | The first value of group_values in results | c9aikmhpdamkiurn1vq0 |
first_threshold | string | The first value of threshold in results | 90 |
priority | string | The highest priority in results | critical |
title | string | event title | Node ({{.first_group_value}} ) disk used >= 90% |
message | string | event content | EventID:{{.event_id}} ; Cluster:{{.resource_name}} |
results | array | result of groups | |
┗ threshold | array | [“90”] | |
┗ priority | string | high | |
┗ group_values | array | [“cluster-xxx”, “node-xxx”] | |
┗ issue_timestamp | number | Millisecond timestamp | 1654595042399 |
┗ result_value | float | 91.2 | |
┗ relation_values | map | {a:100, b:91.2} |
变量使用示例 #
示例1:
{"content":"【Alerting】Event ID: {{.event_id}}, Cluster:{{.resource_name}}"}
示例2(数组遍历):
{{range .results}} Cluster ID: {{index .group_values 0}} {{end}}
模板函数 #
除了直接展示告警事件中的字段值外,还支持使用模板函数对字段值进行进一步处理,优化输出。
函数支持额外参数,当无需或不传递参数时,可以直接使用以下语法进行使用:
{{ <模板变量> | <模板函数> }}
具体实例如下:
模板函数不带参数:
告警事件触发时间:{{ .timestamp | datetime }}
模板函数带参数:
告警事件触发时间:{{ .timestamp | datetime_in_zone "Asia/Shanghai" }}
多个函数组合使用:
字节类型的数值格式化后再转位大写:{{.result_value | format_bytes 2 | to_upper}}
完整的模板函数列表如下:
模板函数 | 参数 | 说明 |
---|---|---|
to_fixed | 固定小数位数 | float类型数值保留N位小数位 示例: {{.result_value | to_fixed 2}} 输出:10.35 |
format_bytes | 固定小数位数 | 字节类型数值格式化 示例: {{.result_value | format_bytes 2}} 输出:10.35gb |
date | 时间戳转为UTC日期 示例: {{.timestamp | date}} 输出:2022-05-01 | |
date_in_zone | 时区 | 时间戳转为当前区域日期 示例: {{.timestamp | date_in_zone "Asia/Shanghai"}} 输出:2022-05-01 |
datetime | 时间戳转为UTC时间 示例: {{.timestamp | datetime}} 输出:2022-05-01 10:10:10 | |
datetime_in_zone | 时区 | 时间戳转为当前区域时间 示例: {{.timestamp | datetime_in_zone "Asia/Shanghai"}} 输出:2022-05-01 10:10:10 |
to_lower | 英文字符转为小写 示例: {{.resource_name | to_lower }} 输出:cluster | |
to_upper | 英文字符转为大写 示例: {{.resource_name | to_upper }} 输出:CLUSTER | |
add | 数值类型 | 数值相加 示例: {{.result_value | add 1 }} 输出:2 |
sub | 数值类型 | 数值相减 示例: {{sub .result_value 1 }} 输出:0 |
mul | 数值类型 | 数值相乘 示例: {{mul .result_value 3 2 }} 输出:6 |
div | 数值类型 | 数值相除 示例: {{div .result_value 2 }} 输出:0.5 |
常用模板语法 #
array 数组遍历:
{{range .results}} priority: {{.priority}} {{end}}
通过数组下标取值:
示例:group_values = ["value1","value2","value3"]
{{index .group_values 0}}
#输出值为:value1
{{index .group_values 2}}
输出值为:value3
if 条件分支:
{{if pipeline}} T1 {{else}} T0 {{end}}
示例:
{{if eq .priority "critical"}} "#C91010" {{else if eq .priority "high"}} "#EB4C21" {{else}} "#FFB449" {{end}}
完整的比较运算符用法:
eq
Returns the boolean truth of arg1 == arg2
ne
Returns the boolean truth of arg1 != arg2
lt
Returns the boolean truth of arg1 < arg2
le
Returns the boolean truth of arg1 <= arg2
gt
Returns the boolean truth of arg1 > arg2
ge
Returns the boolean truth of arg1 >= arg2