Template variables #
Introduction #
When custom alerting triggers event content, in addition to the fixed copy written by yourself, the event title and event content also support template syntax. The rendering of the text can be achieved using fields in the event.
Variables #
The syntax for rendering fields is {{ .fieldname }}
, and the variable fields that can be used for template content rendering are as follows:
Field Name | Type | Descriction | eg |
---|---|---|---|
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} |
Variable usage example #
Example 1:
{"content":"【Alerting】Event ID: {{.event_id}}, Cluster:{{.resource_name}}"}
Example 2(array traversal):
{{range .results}} Cluster ID: {{index .group_values 0}} {{end}}
Template functions #
In addition to directly displaying the field value in the alerting event, it also supports the use of template functions to further process the field value to optimize the output.
Functions support extra parameters. When no parameters are required or passed, the following syntax can be used directly:
{{ <field> | <function> }}
Specific examples are as follows:
Functions take no parameters:
Alerting event trigger time:{{ .timestamp | datetime }}
Functions take parameters:
Alerting event trigger time:{{ .timestamp | datetime_in_zone "Asia/Shanghai" }}
Use multiple functions in combination:
{{.result_value | format_bytes 2 | to_upper}}
The complete list of template functions is as follows:
Functions | params | Descriction | |
---|---|---|---|
to_fixed | fixed number of decimal places | The float type value retains N decimal places Example: {{.result_value | to_fixed 2}} Output:10.35 | |
format_bytes | fixed number of decimal places | Byte type numeric formatting Example: {{.result_value | format_bytes 2}} Output:10.35gb | |
date | Convert timestamp to UTC date Example: {{.timestamp | date}} Output:2022-05-01 | ||
date_in_zone | Time zone | Convert timestamp to current zone date Example: {{.timestamp | date_in_zone "Asia/Shanghai"}} Output:2022-05-01 | |
datetime | Convert timestamp to UTC time Example: {{.timestamp | datetime}} Output:2022-05-01 10:10:10 | ||
datetime_in_zone | Time zone | Convert timestamp to current zone time Example: {{.timestamp | datetime_in_zone "Asia/Shanghai"}} Output:2022-05-01 10:10:10 | |
to_lower | Convert characters to lowercase Example: {{.resource_name | to_lower }} Output:cluster | ||
to_upper | Convert characters to uppercase Example: {{.resource_name | to_upper }} Output:CLUSTER | ||
add | number | Example: a+b{{.result_value | add 1 }} Output:2 | |
sub | number | Example: a - b{{sub .result_value 1 }} Output:0 | |
mul | number | Example: a _ b _ c{{mul .result_value 3 2 }} Output:6 | |
div | number | Example: a/b{{div .result_value 2 }} Output:0.5 |
Common Template Syntax #
Array traversal:
{{range .results}} priority: {{.priority}} {{end}}
Get values by array subscript:
Example: group_values = [“value1”,“value2”,“value3”]
{{index .group_values 0}}
# output: value1
{{index .group_values 2}}
# output: value3
if conditional branch:
{{if pipeline}} T1 {{else}} T0 {{end}}
Example:
{{if eq .priority "critical"}} "#C91010" {{else if eq .priority "high"}} "#EB4C21" {{else}} "#FFB449" {{end}}
There is also a set of binary comparison operators defined as functions:
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
More template syntax Click me