Use variables in alert templates to dynamically populate notification content and subjects.
Variable reference
-
Variable names must be an exact match.
-
If a variable does not exist or a reference is invalid, it defaults to an empty string.
-
Referenced objects are converted to JSON strings.
-
-
Variable names can contain letters, digits, and underscores (_) but must not start with a digit.
-
If a variable name follows these rules, you can retrieve its value by using dot notation, for example,
{{alert.xxx}}. -
If a variable name contains special characters, such as
__tag__:__namespace__, you must use bracket notation to retrieve its value, such as{{alert.annotations["__tag__:__namespace__"] }}.
-
When you configure an action policy, select an alert template that defines the notification content and subject. In the Send Content and Subject/Title fields, reference template variables by using the {{ alert.xxx }} syntax. Simple Log Service (SLS) replaces each variable with its actual value when sending notifications. For example, {{ alert.project }} is replaced with the project name of the alert rule.
For example, in a DingTalk channel template, set the Subject to SLS Alert and use variables like {{ alert.aliuid }}, {{ alert.project }}, {{ alert.alert_name }}, and {{ alert.severity }} in the Content to include the account ID, project name, alert rule name, and severity.
You can also use control flow and built-in functions to process variable data. For more information, see Alert Template Syntax (New) and Built-in Template Functions.
Alert attributes
|
Parameter |
Description |
Type |
Value |
Reference example |
|
aliuid |
The Alibaba Cloud account ID of the Project. |
string |
117918634953**** |
The alert rule for user |
|
alert_instance_id |
The ID of the instance that triggered the alert. |
string |
ee16a8f435485f3f-5be6b81edc520-3d6**** |
The instance ID is |
|
alert_id |
The unique ID of the alert rule within the Project. |
string |
alert-12345 |
The alert rule ID is |
|
alert_name |
The name of the alert rule. |
string |
Test alert rule |
Alert rule |
|
alert_type |
The alert type.
|
string |
sls_alert |
The alert type is |
|
region |
The region where the alert was triggered. |
string |
cn-hangzhou |
The alert was triggered in region |
|
project |
The Project to which the alert rule belongs. |
string |
my-project |
An alert rule was triggered in Project |
|
next_eval_interval |
The next evaluation interval, in seconds. |
int |
300 |
Next evaluation in |
|
alert_time |
The UTC timestamp of the current evaluation. |
int |
1616744734 |
The current evaluation time is |
|
fire_time |
The UTC timestamp when the alert first fired. |
int |
1616059834 |
The alert first fired at |
|
status |
The current status of the alert.
|
string |
firing |
The alert status is |
|
resolve_time |
The UTC timestamp when the alert was resolved.
|
int |
0 |
The alert was resolved at |
|
severity |
The severity of the alert.
|
int |
10 |
The alert severity is |
|
labels |
A set of user-defined key-value labels. |
map |
{"env":"test"} |
The alert labels are |
|
annotations |
A set of user-defined key-value annotations. |
map |
{ "title": "Alert title","desc": "Alert description" } |
The alert annotations are |
|
results |
An array of query parameters and intermediate results. For more information about the values of this variable, see QueryData structure. |
array |
See the appendix at the end of this topic. |
The start time of the first query is |
|
fire_results |
The data that triggered the alert, which is the result of the set operation. A maximum of 100 rows are returned. If the size of the fire_results variable exceeds 2 KB, or if the content of a single field exceeds 1 KB, the data is truncated. For information about how to handle truncated data, see What do I do if an alert is triggered for too many logs and the notification displays incomplete information? |
array |
See the appendix at the end of this topic. |
The data that triggered the alert is |
|
fire_results_count |
The total number of data rows that triggered the alert. This value can be greater than 100, for example, after a Cartesian product operation. |
int |
3 |
The alert was triggered by |
|
condition |
The evaluation expression that triggered the alert, with variables replaced by their actual trigger values enclosed in square brackets []. The format is |
string |
|
The alert evaluation expression is |
|
raw_condition |
The original evaluation expression with variables not replaced by their actual values. The format is |
string |
|
The original evaluation expression is |
|
policy |
The alert policy or action policy. For more information about the values of this variable, see Policy structure. |
map |
See the appendix at the end of this topic. |
The alert policy ID is |
|
dashboard |
The name of the dashboard associated with the alert. |
string |
mydashboard |
The associated dashboard is |
|
alert_url |
The URL to the alert details page. |
string |
https://sls.console.alibabacloud.com/lognext/project/test-xxxx/alert/alert-1617164106-940166 |
The alert URL is |
|
query_url |
The URL for the search page of the first query. |
string |
https://sls.console.alibabacloud.com/lognext/project/test-xxx/logsearch/test-alert-access?encode=base64&endTime=1617175989&queryString=KiB8IHNlbGVjdCBjb3VudCgxKSBhcyBjbn****&queryTimeType=99&startTime=1617175089 |
The URL for the first query page is |
|
alert_history_dashboard_url |
The URL of the Alert History Statistics dashboard. |
string |
https://sls.console.alibabacloud.com/lognext/project/test-xx/dashboard/internal-alert-analysis |
The URL of the Alert History Statistics dashboard is |
|
dashboard_url |
The URL of the dashboard associated with the alert. |
string |
https://sls.console.alibabacloud.com/next/project/myproject/dashboard/mydashboard |
The URL of the associated dashboard is |
|
fingerprint |
The alert fingerprint. For more information, see Deduplicate alerts based on alert fingerprints. |
string |
478325709134bc5c |
The alert fingerprint is |
|
signin_url |
A URL for viewing alert details without logging in to the console. For more information, see View alert details without logging in. |
string |
https://sls.console.alibabacloud.com/console/AlertAjax/slsSignIn.json?token=xxxx |
|
Policy structure
The policy variable contains the following fields.
|
Parameter |
Description |
Type |
Value example |
|
alert_policy_id |
The ID of the alert policy. |
string |
sls.test-alert |
|
action_policy_id |
The ID of the action policy configured for the alert rule. Available only when the alert rule uses a Dynamic Action Policy. |
string |
sls.test-action |
|
repeat_interval |
The notification resend interval for an active alert. Available only when the alert rule uses a Dynamic Action Policy. |
string |
4h |
Query data structure
The results variable contains the following fields.
|
Parameter |
Description |
Type |
Value |
|
store_type |
The storage type. Valid values are:
|
string |
log |
|
region |
The region where the destination store is located. This variable is empty if the value of |
string |
cn-hangzhou |
|
project |
The project where the destination store is located. This variable is empty if the value of |
string |
sls-test-alert |
|
store |
The name of the destination store. |
string |
test-LogStore |
|
query |
The query statement. |
string |
error | select count(1) as cnt |
|
start_time |
The query start time, specified as a UTC timestamp. This variable is empty if the value of |
int |
1616741485 |
|
end_time |
The query end time, specified as a UTC timestamp. This variable is empty if the value of |
int |
1616745085 |
|
raw_results |
An array of the query results, containing up to 100 rows. If the raw_results variable exceeds 2 KB, or if a field in the results exceeds 1 KB, the excess data is truncated. |
array |
|
|
raw_results_count |
The total number of rows in the result set, which can be greater than 100. |
int |
20 |
|
fire_result |
The first record of the alert trigger result, even if the result set contains multiple records. |
map |
|
|
query_url |
The URL of the query page in the console. This variable is empty if the value of |
string |
https://sls.console.alibabacloud.com/lognext/project/test-xxx/logsearch/test-alert-access?encode=base64&endTime=1617175989&queryString=KiB8IHNlbGVjdCBjb3VudCgxKSBhcy*******&queryTimeType=99&startTime=1617175089 |
|
dashboard_url |
The URL of the dashboard associated with the query. |
string |
https://sls.console.alibabacloud.com/next/project/myproject/dashboard/mydashboard |
|
role_arn |
The Alibaba Cloud Resource Name (ARN) of the service role. |
string |
acs:ram::117918634953****:role/aliyunslsalertmonitorrole |
FAQ
Appendix
-
resultssample:[{ "store_type": "log", "region": "cn-hangzhou", "project": "sls-alert-test", "store": "test", "query": "* | select count(1) as cnt", "start_time": 1616741485, "end_time": 1616745085, "dashboard_id": "mydashboard", "raw_results": [{ "cnt": "4" }], "raw_result_count": 1, "fire_result": { "cnt": "4" }, "truncated": false, "role_arn": "" }] -
fire_resultssample:[{ "host": "example.com", "host__1": "example.com", "pv": "836", "slbid": "slb-02", "status": "200" }, { "host": "example.com", "host__1": "example.com", "pv": "836", "slbid": "slb-02", "status": "200" }] -
policysample:{ "alert_policy_id": "sls.test-alert", "action_policy_id": "sls.test-action", "repeat_interval": "5m0s" }
Example
The following example defines notification content by using the new content template:
-
Alert content:
{ "alert_id": "test-alert", "alert_name": "PV/UV Alert", "project": "project-1", "status": "firing", "severity": 6, "labels": { "app": "nginx", "host": "host-1" }, "results": [ { "project": "project-1", "logstore": "logstore-1", "query": "* | select count(*) as pv" }, { "project": "project-2", "logstore": "logstore-2", "query": "* | select count(distinct user_id) as uv" } ] } -
Alert template configuration:
- Alert ID: {{ alert.alert_id }} - Alert Name: {{ alert.alert_name }} - Project: {{ alert.project }} - Status: {% if alert.status == "firing" %}FIRING{% else %}RESOLVED{% endif %} - Labels: {%- for key, val in alert.labels.items() %} - {{ key }}: {{ val }} {%- endfor %} - Query: {{ alert.results[0].query }} -
Output result:
- Alert ID: test-alert - Alert Name: PV/UV Alert - Project: project-1 - Status: FIRING - Labels: - app: nginx - host: host-1 - Query: * | select count(*) as pv