Skip to content

Custom Event Notification Templates


With template syntax, you can customize event notification content:

  • Dynamic rendering: insert alert fields directly using {{ field name }} (e.g., {{ host }}, {{ df_status }});
  • Data processing: format data with {{ variable | function() }} (e.g., timestamp to date, number to percentage);
  • Conditional branching: use {% if ... %} to send differentiated notifications based on status;
  • Real-time queries: embed DQL ("query statement") to retrieve related data (e.g., host IP, application information).

Validate and Preview Templates

The Event Content of monitors, Data Gap Event Content, and the Event Content of Smart Monitoring support syntax validation. After editing, when you move the cursor out of the input box, or when you save the monitor, the system checks the template and flags syntax issues.

After the syntax check passes, click the Preview button in the event content editor, fill in test values for variables as needed, and then click Preview to verify the rendered result. Regular monitors preview both the current title and content, while Smart Monitoring previews the event content. You can switch the test values of variables such as df_status to check conditional branches such as anomaly and recovery.

Preview is used to check the template effect; actual alerts are based on the data at the time the event occurs. When the template contains an embedded DQL, the preview reads the relevant data from the current workspace.

Basic Template Variables

The basic syntax of template variables is {{ field name }}, which can be used to render dynamic event-related information. The following are common template variables and their purposes:

Template Variable Type Description
date, timestamp Integer Event generation time. In seconds.
df_date_range Integer Time range. In seconds.
df_check_range_start Integer Detection range start time. Unix timestamp, in seconds.
df_check_range_end Integer Detection range end time. Unix timestamp, in seconds.
df_status String(Enum) Event status. Possible values are:

  • Critical critical
  • Important error
  • Warning warning
  • OK ok
  • No Data nodata
  • df_event_id String Unique event ID.
    df_event_link String Link to the event details page.
    df_dimension_tags String Event dimensions. Used to identify the detection target.

    Example: {"host":"web-001"}
    df_monitor_id String Alert Policy ID.
    df_monitor_name String Alert Policy name.
    df_monitor_checker_id String Monitor ID.
    df_monitor_checker_name String Monitor name.
    df_monitor_checker_value String Detection value, i.e., the value detected by the monitor.

    ❗️The detection value is forcibly converted to the String type to ensure compatibility.
    df_monitor_checker_event_ref String Monitor event reference.
    Computed from the monitor ID and the event's df_dimension_tags.
    df_fault_id String ID of the current fault round. Its value is the df_event_id of the first fault event.
    df_fault_status String(Enum) Status of the current fault round. It is a redundant field of df_status. Possible values are:

  • OK ok
  • Fault fault
  • df_fault_start_time Integer Time when the current fault occurred. Unix timestamp, in seconds.
    df_fault_duration Integer Duration of the current fault. In seconds.
    df_user_id String When recovering manually, the operator's user ID.
    df_user_name String When recovering manually, the operator's user name.
    df_user_email String When recovering manually, the operator's user email.
    df_crontab_exec_mode String(Enum) Monitor execution mode. Possible values are:

  • Automatically triggered crontab
  • Manually executed manual
  • df_site_name String The current Guance node name.
    df_workspace_name String The workspace it belongs to.
    df_workspace_uuid String The ID of the workspace it belongs to.
    df_label List Monitor label list.
    df_check_condition Dict The detection condition that was met.
    df_check_detail Dict Applicable to mutation detection. Represents the mutation compared to the original value. Contains three fields:

  • Displayed detection value (change_value)
  • Comparison value (comparison_value)
  • Changed value (detection_value)
  • df_check_condition.operator String Operator of the met detection condition, such as >, >=, etc.
    df_check_condition.operands List List of operands of the met detection condition.
    Usually there is only 1 operand, but operators such as between have 2 operands.
    df_check_condition.operands[#] Integer, Float Operands of the met detection condition.
    Result Integer, Float The detected value. Like df_monitor_checker_value, it is a value generated at detection time, but its field type is the original type at detection time and is not forcibly converted to String.
    Fields in df_dimension_tags String Fields in df_dimension_tags are extracted.
    df_event Dict Complete event data.
    df_studio_env_name Current environment name.
    df_studio_console_base_url Current environment console URL.
    df_studio_monitor_base_link Current environment monitor URL.

    Template Variable Example

    Assume the monitor's by is configured with region and host, and the event content template is as follows:

    Event Name:

    The monitor {{ df_monitor_checker_name }} found a fault in {{ df_dimension_tags }}
    

    Event Content:

    - Region: {{ region }}
    - Host: {{ host }}
    - Level: {{ df_status }}
    - Detection value: {{ Result }}
    - Monitor: {{ df_monitor_checker_name }} (Alert Policy: {{ df_monitor_name }})
    

    Then, after an error event is generated, the rendered event output is as follows:

    Output Event Name:

    The monitor Monitor001 found a fault in {"region":"hangzhou","host":"web-001"}
    

    Output Event Content:

    - Region: hangzhou
    - Host: web-001
    - Level: error
    - Detection value: 90.12345
    - Monitor: Monitor001 (Alert Policy: Team001)
    

    Special Scenario Variables

    User Access Metrics Detection

    In User Access Metrics Detection, in addition to the general template variables above, the following template variables are also supported:

    Template Variable Type Description
    app_id String Application ID
    app_name String Application name
    app_type String Application type

    Handling Fields with Special Characters

    If the Dimensions field in the detection configuration contains special characters (such as - or @), for example host-name or @level, it cannot be used directly as a normal variable name, which causes template rendering to fail.

    The solution is to reference them using the following formats:

    • Use {{ df_event['host-name'] }} instead of {{ host-name }}
    • Use {{ df_event['@level'] }} instead of {{ @level }}

    Template Functions

    In addition to directly displaying field values from an event, you can also use template functions to further process field values and improve the output.

    The basic syntax is as follows:

    {{ <template variable> | <template function> }}
    

    For example:

    Event generation time: {{ date | to_datetime }}
    

    If a template function requires parameters, the syntax is as follows:

    Event generation time: {{ date | to_datetime('America/Chicago') }}
    
    Warning

    If you need to perform arithmetic on a template variable before applying a template function, do not forget to add parentheses, for example:

    CPU usage: {{ (Result * 100) | to_round(2) }}
    

    The available template functions are as follows:

    Template Function Parameters Description
    to_datetime Timezone Converts a timestamp to a date (the default timezone is Asia/Shanghai).
    Example: {{ date | to_datetime }}
    Output: 2022-01-01 01:23:45
    to_date_range_human Converts df_fault_duration to a human-readable form.
    Example: {{ df_fault_duration | to_date_range_human }}
    Output: X days Y hours Z minutes W seconds
    to_status_human Converts df_status to a human-readable form.
    Example: {{ df_status | to_status_human }}
    Output: Critical
    to_fixed Decimal places Outputs a number with a fixed number of decimal places (defaults to 0 decimal places).
    Example: {{ Result | to_fixed(3) }}
    Output: 1.230
    to_round Decimal places Rounds a number (defaults to 0 decimal places).
    Example: {{ Result | to_round(2) }}
    Output: 1.24
    to_percent Decimal places Outputs a decimal as a percentage (defaults to 0 decimal places).
    Example: {{ Result | to_percent(1) }}
    Output: 12.3%
    to_pretty_tags Beautifies tag output.
    Example: {{ df_dimension_tags | to_pretty_tags }}
    Output: region:hanghzou, host:web-001
    limit_lines Limits the number of lines.
    limit_chars / limit_text Limits the number of characters.
    type_name Outputs the data type name.
    Example: {{ data | type_name }}
    Output: dict
    to_int Converts to an integer.
    Example: {{ data | to_int }}<br>Output:1`
    to_float Converts to a float.
    Example: {{ data | to_float }}
    Output: 1.234
    to_str Converts to a string.
    Example: {{ data | to_str }}
    Output: 1.234
    to_json_dumps Converts to a JSON-serialized string.
    Example: {{ data | to_json_dumps }}
    Output: {"key1":"value1","key2":"value2"}
    length Gets the length of the data.
    Example: {{ data | length }}
    Output: 10

    Template Function Example

    For more details, refer to Examples of Each Template Function.

    Assume the configured monitor's by is configured with region and host, and the alert template is as follows:

    Event Name:

    The monitor {{ df_monitor_checker_name }} found a fault in {{ df_dimension_tags | to_pretty_tags }}
    

    Event Content:

    - Target: {{ df_dimension_tags | to_pretty_tags }}
    - Time: {{ date | to_datetime }}
    - Level: {{ df_status | to_status_human }}
    - Detection value: {{ (Result * 100) | to_round(2) }}
    

    Then, after an error event is generated, the rendered event output is as follows:

    Output Event Name:

    The monitor My Monitor found a fault in region:hangzhou, host:web-001
    

    Output Event Content:

    - Detection target: region:hangzhou, host:web-001
    - Detection time: 2022-01-01 01:23:45
    - Fault level: Important
    - Detection value: 9012.35
    

    Template Branching

    Templates also support branch syntax to render different content based on conditions.

    Template Branching Example

    You can use the following syntax to implement branching:

    {% if df_status == 'critical' %}
    Critical issue, please handle it immediately!
    {% elif df_status == 'error' %}
    Important issue, please handle it
    {% elif df_status == 'warning' %}
    There may be an issue, handle it when you have time
    {% elif df_status == 'nodata' %}
    Data interrupted, please handle it immediately!
    {% else %}
    No problem!
    {% endif %}
    

    A more typical example is as follows:

    {% if  df_status != 'ok' %}
    > Level: {{ df_status }}
    > Host: {{ host }}
    > Content: Elasticsearch JVM heap memory usage is {{ Result }}%
    > Suggestion: JVM garbage collection cannot keep up with JVM garbage generation. Please check the business situation promptly
    
    {% else %}
    > Level: {{df_status}}
    > Host: {{host}}
    > Content: The Elasticsearch JVM heap memory alert has recovered
    
    {% endif %}
    

    Embedded DQL Query Function

    In some cases, template variables alone cannot meet the rendering requirements. In such cases, you can use the embedded DQL query function to perform additional data queries.

    The embedded DQL query function supports executing any DQL statement within the current detection time range in this workspace. Typically, the first record returned by the query can be used as a template variable in the template, as follows:

    {% set dql_data = DQL("The DQL statement to execute") %}
    
    Some field: {{ dql_data.some_field }}
    

    Embedded DQL Query Example

    The following embedded DQL statement queries data whose host field is "my_server" and assigns the first record to the dql_data variable:

    {% set dql_data = DQL("O::HOST:(host, host_ip, os, datakit_ver) { host = 'my_server' }") %}
    
    Host OS: {{ dql_data.os }}
    

    In the template that follows, you can use {{ dql_data.os }} to output specific fields from the query result.

    Passing Parameters to the Embedded DQL

    Sometimes, executing a DQL statement requires passing parameters. These parameters can be embedded directly into the DQL statement, and you can choose whether to add a by filter. However, the syntax is slightly different. Usually, you reference a variable with {{df_status}}. But in a DQL statement, you should use the variable name directly without adding {{ }}; therefore, the final form is status.

    Assume the monitor's by is configured with region and host, and the event content template is as follows:

    {% set dql_data = DQL("O::HOST:(host_ip, os) { region = ?, host = ? }", region, host) %}
    
    Host information:
    IP: {{ dql_data.host_ip }}
    OS: {{ dql_data.os }}
    

    Since the event only contains the region and host template variables to identify different data, it does not include more information such as IP addresses or operating systems.

    Then, using the embedded DQL, you can retrieve the corresponding data using region and host as DQL query parameters, and output the related information with {{ dql_data.host_ip }} and so on.

    Embedded DQL Query Function Details

    The embedded DQL query function is called in the following format:

    DQL(dql, param_1, param_2, ...)
    
    • The first parameter is the DQL statement, which can contain parameter placeholders ?
    • The subsequent parameters are the parameter values or variables of the DQL statement

    When the parameter placeholder ? is replaced with an actual value, the system escapes it automatically.

    Assume the variable host has the value "my_server". The embedded DQL function and the final DQL statement executed are as follows:

    DQL("O::HOST:(host, host_ip, os, datakit_ver) { host = ? }",  host)
    
    O::HOST:(host, host_ip, os, datakit_ver) { host = 'my_server' }
    
    Warning
    • The embedded DQL query should be placed at the beginning of the template
    • The query result name (dql_data here) follows the naming conventions of general programming languages. It can be any string that starts with an English letter and contains only English letters, digits, and underscores. Using emoji is not recommended.
    • The query result name must not conflict with any existing template variable or template function; otherwise, unpredictable problems may occur
    • If a function is applied to a field in the DQL, it is recommended to use AS to alias the field for later use (e.g., O::HOST:( last(host) AS last_host )).
    • If a field name in the DQL contains special characters, render it with {{ dql_data['host-name'] }} or {{ dql_data['@level'] }}, just like template variables.

    More DQL Documentation

    Feedback

    Is this page helpful?