Reference log content in alert notifications

更新时间:
复制 MD 格式

Use the annotations, fire_results, and results variables in an alert template to include log query data in alert notifications.

Simple Log Service (SLS) can embed alert details directly in alert notifications, so you see actionable context the moment an alert arrives. To include log data in a notification, configure the required parameters when creating an alert monitoring rule, then reference the annotations, fire_results, or results variable in the alert template. For more information, see Variables in alert templates (new version).

The three variables serve different purposes:

Variable

What it contains

Best for

annotations

Key-value pairs you define, plus two SLS defaults (title, desc)

Summarizing alert context in human-readable form

fire_results

The raw data rows that triggered the alert

Listing all matching records at a glance

results

Full query metadata plus raw results and the triggering row

Debugging or building detailed notification content

  • annotations: the annotations of an alert

  • fire_results: the data records for which an alert is triggered

  • results: the query parameters and intermediate results

For example, to monitor NGINX access errors, run the following query to get the HTTP status code, the source IP address, and the error count. Then create an alert monitoring rule and reference this data in the alert template. When a status code reaches 400 or above, SLS sends a notification that includes the status code, source IP, and count.

  • Query statement

    status >=400 | SELECT status, __source__ AS ip, count(*) AS cnt GROUP BY status,ip
  • Query results Query results

Use the annotations variable

Add the annotations variable to an alert template to include annotation key-value pairs in alert notifications.

  • Alert monitoring rule

    Add annotation key-value pairs manually, or turn on Auto-Add Annotations to let SLS populate them automatically when the alert fires. For more information, see Annotations.

    Note

    SLS provides two annotations by default.

    For example, to include the status code, source IP, and error count in alert messages, add annotations such as status:${status}, ip:${ip}, and cnt:${cnt}.

    • Manually add information about annotations image

    • Turn on Auto-Add Annotations to automatically add information about annotations image

  • Alert message

    After an alert fires, SLS generates an alert message containing the annotations you configured:

    {
        "annotations": {
            "title": "Alert is triggered by an NGINX access error",
            "desc": "The error that is indicated by the status code 400 occurred 15 times"
            "status": "400"
            "ip": "127.0.0.1"
            "cnt": "15"
            "__count__": "1"
        }
    }
  • Alert template

    Add the annotations variable to the alert template to reference specific log content in notifications. For more information about creating an alert template, see Create an alert template.

    Alert template

Use the fire_results variable

Add the fire_results variable to an alert template to list every data row that triggered the alert.

  • Alert monitoring rule

    SLS generates alert messages based on the alert monitoring rule you create. image

  • Alert message

    After an alert fires, SLS generates an alert message containing all rows that matched the trigger condition — in this example, the status code, source IP, and error count for each matching row.

    {
        "fire_results": [
            { "status": "401", "ip": "127.0.0.1", "cnt": "3" },
            { "status": "400", "ip": "127.0.0.1", "cnt": "7" },
            { "status": "501", "ip": "127.0.0.1", "cnt": "4" },
            { "status": "404", "ip": "127.0.0.1", "cnt": "4" },
            { "status": "402", "ip": "127.0.0.1", "cnt": "6" },
            null
        ]
    }
  • Alert template

    Add the fire_results variable to the alert template to reference specific log content in notifications. For more information about creating an alert template, see Create an alert template.

    Alert template

Use the results variable

Add the results variable to an alert template to include full query context in alert notifications.

  • Alert monitoring rule

    SLS generates alert messages based on the alert monitoring rule you create. image

  • Alert message

    After an alert fires, SLS generates an alert message containing the full query context.

    Note

    If an alert monitoring rule contains multiple query statements, the results array contains multiple items. Each item corresponds to a query statement.

    {
        "results": [{
            "store_type": "log",
            "region": "cn-hangzhou",
            "project": "test-alert",
            "store": "nginx-access-log",
            "query": "status >= 400 | select status, __source__ as ip, count(*) as cnt group by status, ip",
            "start_time": 1640006894,
            "end_time": 1640007014,
            "dashboard_id": "",
            "raw_results": [
                { "status": "401", "ip": "127.0.0.1", "cnt": "3" },
                { "status": "400", "ip": "127.0.0.1", "cnt": "7" },
                { "status": "501", "ip": "127.0.0.1", "cnt": "4" },
                { "status": "404", "ip": "127.0.0.1", "cnt": "4" },
                { "status": "402", "ip": "127.0.0.1", "cnt": "6" },
                null
            ],
            "raw_result_count": 6,
            "fire_result": {
                "status": "401",
                "ip": "127.0.0.1",
                "cnt": "3"
            },
            "has_sql": true,
            "truncated": false,
            "role_arn": ""
        }]
    }

    Each item in the results array contains the following fields:

    Field

    Description

    store_type

    The type of data store queried, such as log

    region

    The region of the project

    project

    The SLS project name

    store

    The Logstore name

    query

    The query statement that was executed

    start_time

    The query start time (Unix timestamp)

    end_time

    The query end time (Unix timestamp)

    dashboard_id

    The associated dashboard ID, if any

    raw_results

    All rows returned by the query

    raw_result_count

    The total number of rows in raw_results

    fire_result

    The specific row that triggered the alert

    has_sql

    Whether the query contains a SQL analysis statement

    truncated

    Whether the result set was truncated

    role_arn

    The RAM role ARN used for the query, if any

  • Alert template

    Add the results variable to the alert template to reference specific log content in notifications. For more information about creating an alert template, see Create an alert template.

    Alert template

Template variables

When you reference a variable in JSON format in an alert template — for example, {{ alert.results[0].raw_results | to_json }} — the value appears as a JSON string in the notification. Use a loop to display multi-row results line by line instead. For more information, see Syntax for new alert templates.

{%- for result in alert.fire_results %}
- status: {{ result.status }}, count: {{ result.cnt }}
{%- endfor %}

SLS provides built-in functions for formatting notification content. For more information, see Syntax for new alert templates and Built-in template functions.