Menu
Grafana Cloud

Advanced template configuration

Grafana OnCall uses the Jinja templating language to format alert groups for various platforms such as the Web, Slack, phone calls, SMS messages, and more. This allows you to customize the presentation and content of alerts when they are triggered.

Jinja2 offers a range of functionalities, including loops, conditions, and functions, which can be used to enhance alert template customization. Every alert from a monitoring system is received in a key/value format, which Grafana OnCall maps to specific fields, such as: title, message, image, grouping, and auto-resolve.

To learn more about mapping your alert payload to Grafana OnCall fields, refer to map payloads to OnCall fields.

Loops

Monitoring systems can send an array of values. Use Jinja to iterate and format the alert payloads. For example:

.jinja2
*Values:*
 {% for evalMatch in payload.evalMatches -%}
 `{{ evalMatch['metric'] }}: '{{ evalMatch['value'] -}}'`{{ " " }}
 {%- endfor %}

Conditions

Add conditional instructions based on specific alert rules. For instance, to provide instructions when an alert comes from a specific Grafana alert rule:

jinja2
{% if  payload.ruleId == '1' -%}
*Alert TODOs*
1. Get access to the container
    ```
        kubectl port-forward service/example 3000:80
    ```
2. Check for the exception.
3. Open the container and reload caches.
4. Click Custom Button `Send to Jira`
{%- endif -%}

Built-in Jinja functions

Jinja2 includes various built-in functions that can be used in Grafana OnCall. For example, to prettify JSON:

.jinja2
{{ payload | tojson_pretty }}

Some commonly used built-in functions include:

  • abs
  • capitalize
  • trim

For a full list of Jinja built-in functions, see the Jinja documentation on GitHub

Functions added by Grafana OnCall

Grafana OnCall enhances Jinja with additional functions:

  • time: Current time
  • tojson: Dumps a structure to JSON
  • tojson_pretty: Same as tojson, but prettified
  • iso8601_to_time: Converts ISO8601 time (2015-02-17T18:30:20.000Z) to datetime
  • datetimeformat: Converts datetime to string according to strftime format codes (%H:%M / %d-%m-%Y by default)
  • datetimeformat_as_timezone: Converts datetime to string with timezone conversion (UTC by default)
    • Usage example: {{ payload.alerts.startsAt | iso8601_to_time | datetimeformat_as_timezone('%Y-%m-%dT%H:%M:%S%z', 'America/Chicago') }}
  • datetimeparse: Converts string to datetime according to strftime format codes (%H:%M / %d-%m-%Y by default)
  • regex_replace: Performs a regex find and replace
  • regex_match: Performs a regex match, returns True or False
    • Usage example: {{ payload.ruleName | regex_match(".*") }}
  • regex_search: Performs a regex search, returns True or False
    • Usage example: {{ payload.message | regex_search("Severity: (High|Critical)") }}
  • b64decode: Performs a base64 string decode
    • Usage example: {{ payload.data | b64decode }}
  • parse_json:Parses a JSON string to an object
    • Usage example: {{ (payload.data | b64decode | parse_json).name }}