Template variables

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 NameTypeDescrictioneg
rule_idstringrule uuidc9f663tath2e5a0vksjg
rule_namestringrule nameHigh CPU usage
resource_idstringresource uuidc9f663tath2e5a0vksjg
resource_namestringresource namees-v716
event_idstringidentifier for check detailsc9f663tath2e5a0vksjx
timestampnumberMillisecond timestamp1654595042399
first_group_valuestringThe first value of group_values in resultsc9aikmhpdamkiurn1vq0
first_thresholdstringThe first value of threshold in results90
prioritystringThe highest priority in resultscritical
titlestringevent titleNode ({{.first_group_value}}) disk used >= 90%
messagestringevent contentEventID:{{.event_id}}; Cluster:{{.resource_name}}
resultsarrayresult of groups
┗ thresholdarray[“90”]
┗ prioritystringhigh
┗ group_valuesarray[“cluster-xxx”, “node-xxx”]
┗ issue_timestampnumberMillisecond timestamp1654595042399
┗ result_valuefloat91.2
┗ relation_valuesmap{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:

to_fixedfixed number of decimal placesThe float type value retains N decimal places
Example:{{.result_value | to_fixed 2}}
format_bytesfixed number of decimal placesByte type numeric formatting
Example:{{.result_value | format_bytes 2}}
dateConvert timestamp to UTC date
Example:{{.timestamp | date}}
date_in_zoneTime zoneConvert timestamp to current zone date
Example:{{.timestamp | date_in_zone "Asia/Shanghai"}}
datetimeConvert timestamp to UTC time
Example:{{.timestamp | datetime}}
Output:2022-05-01 10:10:10
datetime_in_zoneTime zoneConvert timestamp to current zone time
Example:{{.timestamp | datetime_in_zone "Asia/Shanghai"}}
Output:2022-05-01 10:10:10
to_lowerConvert characters to lowercase
Example:{{.resource_name | to_lower }}
to_upperConvert characters to uppercase
Example:{{.resource_name | to_upper }}
addnumberExample: a+b
{{.result_value | add 1 }}
subnumberExample: a - b
{{sub .result_value 1 }}
mulnumberExample: a * b * c
{{mul .result_value 3 2 }}
divnumberExample: a/b
{{div .result_value 2 }}

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}}


{{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:

	Returns the boolean truth of arg1 == arg2
	Returns the boolean truth of arg1 != arg2
	Returns the boolean truth of arg1 < arg2
	Returns the boolean truth of arg1 <= arg2
	Returns the boolean truth of arg1 > arg2
	Returns the boolean truth of arg1 >= arg2