The new alert templates support built-in functions. You can use the built-in functions to configure the formats and styles of alert notifications. This topic describes the syntax of the built-in functions and provides examples on how to use the built-in functions.
General functions
Numeric functions
Function | Description | Filter | Example |
float(value, default=0.0) | Converts an integer or a string to a floating-point number. By default, if the conversion fails, the function returns 0.0. You can also specify a custom return value in the event of a conversion failure by using the default parameter. | Supported |
|
int(value, default=0) | Converts a string or a number to an integer. By default, if the conversion fails, the function returns 0. You can also specify a custom return value in the event of a conversion failure by using the default parameter. | Supported |
|
length(value) | Obtains the length or number of elements in an object such as a string, list, or tuple. | Supported |
|
abs(value) | Obtains the absolute value of a number. | Supported | The result returned by {{ abs(-1) }} is 1. |
min(value) | Obtains the minimum value among the values that you specify. | Supported | The result returned by {{ min([1, 3, 2]) }} is 1. |
max(value) | Obtains the maximum value among the values that you specify. | Supported | The result returned by {{ max([1, 3, 2]) }} is 3. |
ceil(value) | Rounds a number up to the nearest integer. | Supported | The result returned by {{ ceil(1.23) }} is 2. |
floor(value) | Rounds a number down to the nearest integer. | Supported | The result returned by {{ floor(1.23) }} is 1. |
round(value, 1) | Rounds a number to the nearest integer.
| Supported |
|
sum(value) | Obtains the sum of the values that you specify. | Supported | The result returned by {{ sum([1, 2, 3]) }} is 6. |
String functions
Function | Description | Filter | Example |
string(value) | Converts an object to a string. | Supported | The result returned by {{ string(1.23) }} is 1.23. The value 1.23 is a string. |
capitalize(value) | Converts the first letter of a string to an uppercase letter and converts the other letters of the string to lowercase letters. | Supported | The result returned by {{ capitalize("heLLO World") }} is Hello world. |
lower(value) | Converts the uppercase letters in a string to lowercase letters. | Supported | The result returned by {{ lower("FOO") }} is foo. |
upper(value) | Converts the lowercase letters in a string to uppercase letters. | Supported | The result returned by {{ upper("foo") }} is FOO. |
title(value) | Converts a string to title case. In title case, the first letter of each word in a string is in uppercase, whereas the other letters of each word are in lowercase. | Supported | The result returned by {{ title("hello world") }} is Hello World. |
trim(value) | Deletes the empty characters at the beginning and end of a string. | Supported | The result returned by {{ trim(" foo\n") }} is foo. |
replace(value, old, new) | Replaces the specified characters of a string with new characters. | Not supported | The result returned by {{ replace("foo", "oo", "ly") }} is fly. |
wordcount(value) | Counts the number of words in a string. | Supported | The result returned by {{ wordcount("hello world") }} is 2. |
truncate(value, n, end='') | Truncates a string.
| Not supported |
|
quote(value) | Encloses a string in a pair of double quotation marks (""). | Supported |
|
indent(value, n=4) | Indents each line of strings with a specified number of spaces. By default, each line of strings is indented with four spaces. You can use the n parameter to specify the number of spaces with which you want to indent each line of strings. | Supported |
|
startswith(value, prefix) | Checks whether a string starts with a specified substring. | Supported | The result returned by {{ startswith("football", "foo") }} is true. |
endswith(value, suffix) | Checks whether a string ends with a specified substring. | Supported | The result returned by {{ endswith("football", "all") }} is true. |
removeprefix(value, prefix) | Removes a specified substring from the start of a string. | Supported | The result returned by {{ removeprefix("football", "foot") }} is ball. |
removesuffix(value, suffix) | Removes a specified substring from the end of a string. | Supported | The result returned by {{ removesuffix("football", "ball") }} is foot. |
split(value, sep=None, maxsplit=-1) | Splits a string.
| Supported |
|
List functions and object functions
Function | Description | Filter | Example |
enumerate(value) | Converts an iterable object to an indexed sequence and lists the original elements and the subscripts of the original elements. | Not supported | The result returned by {{ enumerate(["foo", "bar"]) }} is [(0, 'foo'), (1, 'bar')]. |
list(value) | Converts an iterable object to a list. | Supported |
|
dict(value) | Creates a dictionary. The function works in a way similar to the method in which {} is used to create a dictionary. | Not supported | The result returned by {{ dict(foo=1, bar="hello") }} is {'foo': 1, 'bar': 'hello'}. |
first(value) | Returns the first item of a list. | Supported | The result returned by {{ first([1, 2, 3]) }} is 1. |
last(value) | Returns the last item of a list. | Supported | The result returned by {{ last([1, 2, 3]) }} is 3. |
sort(value, reverse=true) | Sorts the elements of a list. If you want to sort the elements of a list in reversed order, specify reverse=true. | Supported |
|
dictsort(value) | Sorts the key-value pairs in an object based on keys and returns an array that consists of the sorted key-value pairs. | Supported |
|
join(value, d='') | Joins the elements of a list by using a specified character. You can use the d parameter to specify the character that is used to join the elements of the list. | Supported |
|
Format functions
Function | Description | Filter | Example |
escape_markdown(value) | Escapes special characters in Markdown. | Supported | The result returned by {{ escape_markdown("__a__ **b** #c") }} is __a__ **b** #c . |
escape_html(value) | Escapes special characters in HTML. | Supported | The result returned by {{ escape_html("<div>") }} is <div> . |
to_json(value) | Converts an object to the JSON format. | Supported |
|
parse_json(value) | Parses a string and returns a value in the JSON format. | Supported |
|
Encoding functions and decoding functions
Function | Description | Filter | Example |
base64_encoding(value) | Encodes the input value by using Base64. | Supported | The result returned by {{ base64_encoding("foo") }} is Zm9v. |
base64_decoding(value) | Decodes the input value by using Base64. | Supported | The result returned by {{ base64_decoding("Zm9v") }} is foo. |
md5_encoding(value) | Encodes the input value by using MD5. | Supported | The result returned by {{ md5_encoding("foo") }} is acbd18db4cc2f85cedef654fccc4a4d8. |
url_encoding(value) | Encodes the input value by using URL encoding. | Supported | The result returned by {{ url_encoding("https://example.com?a=b&c=d") }} is https%3A%2F%2Fexample.com%3Fa%3Db%26c%3Dd. |
url_decoding(value) | Decodes the input value by using URL decoding. | Supported | The result returned by {{ url_decoding("https%3A%2F%2Fexample.com%3Fa%3Db%26c%3Dd") }} is https://example.com?a=b&c=d. |
Date and time functions
Function | Description | Filter | Example |
parse_date(value, fmt="%Y-%m-%d %H:%M:%S") | Converts the input value to a datetime expression that can return a timestamp value. You can use the fmt parameter to specify the format of the datetime expression. | Supported |
|
format_date(value, tz=None, fmt="%Y-%m-%d %H:%M:%S") | Formats the input value. You can use the fmt parameter to specify the format of the datetime expression. If the input value is not a date object, the function converts the input value to a date object before the function formats the input value. For more information about the directives that are used to format date and time strings, see Date and time formatting directives. For more information about time zones, see Time zones. | Not supported |
|
timestamp(value) | Converts a date and time string to a UNIX timestamp. If the input value is not a date object, the function converts the input value to a date object before the function formats the input value. | Supported |
|
format_duration(value, locale='en-US', sep='') | Formats an interval. The unit of value is seconds. You can use the locale parameter to specify the language in which the text description is written. For more information about the valid values of the locale parameter, see the "Valid values of the locale parameter in alert functions" section of this topic. | Supported |
|
Alert functions
- The alert attributes, such as the severity level and status of the triggered alert.
- The language of the alert template. For example, the language can be Chinese or English.
- The notification method, such as DingTalk and email.
Function | Description | Filter | Example |
format_type(alert.type, locale=None) | Converts the type of an alert to a text description. You can use the locale parameter to specify the language in which the text description is written. For more information about the valid values of the locale parameter, see the "Valid values of the locale parameter in alert functions" section of this topic. | Supported |
|
format_region(alert.region, locale=None) | Converts the region where an alert is triggered to a text description. You can use the locale parameter to specify the language in which the text description is written. For more information about the valid values of the locale parameter, see the "Valid values of the locale parameter in alert functions" section of this topic. | Supported |
|
format_severity(alert.severity, locale=None) | Converts the severity level of an alert to a text description. The function supports colored fonts. Note Only DingTalk, Enterprise WeChat, Email, and Message Center support colored fonts. You can use the locale parameter to specify the language in which the text description is written. For more information about the valid values of the locale parameter, see the "Valid values of the locale parameter in alert functions" section of this topic. | Supported |
|
format_status(alert.status, locale=None) | Converts the status of an alert to a text description. The function supports colored fonts. Note Only DingTalk, Enterprise WeChat, Email, and Message Center support colored fonts. If you use a notification method that does not support colored fonts, the alert notification remains unchanged after the function is invoked. You can use the locale parameter to specify the language in which the text description is written. For more information about the valid values of the locale parameter, see the "Valid values of the locale parameter in alert functions" section of this topic. | Supported |
|
to_list(value) | Converts an array or object to a list. | Supported |
|
annotations_to_list(alert.annotations, locale=None) | Converts the annotations of an alert to a list. The function is similar to the to_list(alert.annotations) function. The difference between these two functions lies in that the annotations_to_list function can automatically convert a standard name to a text description. For example, the annotations_to_list function can convert the title field to Title . For more information about standard names, see the "Annotations in alert functions" section of this topic. You can use the locale parameter to specify the language in which the text description is written. For more information about the valid values of the locale parameter, see the "Valid values of the locale parameter in alert functions" section of this topic. | Supported |
|
blockquote(value) | Adds a reference style to an alert notification.
| Supported |
|
References
- Valid values of the locale parameter in alert functions
Valid value Description None or an empty string Specifies to use the language that you select when you configure the alert template. en-US Specifies to use English. zh-CN Specifies to use Chinese. - Annotations in alert functions
Annotation Mapping value (English) title Title desc Description anomaly_score Anomaly Score job_id Job ID model_id Model ID severity Anomaly Severity __pub_alert_app__ Application __pub_alert_protocol__ Protocol __pub_alert_region__ Region __pub_alert_service__ Service __ensure_url__ Anomaly Confirmation __mismatch_url__ False Positive Confirmation __plot_image__ Time Series Chart __host_ip__ Machine Address __host_group_name__ Machine Group Name __cloud_monitor_type__ CloudMonitor