Advanced template configuration
Grafana IRM 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 IRM maps to specific fields, such as:
title, message, image, grouping, and auto-resolve.
To learn more about mapping your alert payload to Grafana IRM fields, refer to map payloads to Grafana IRM fields.
Loops
Monitoring systems can send an array of values. Use Jinja to iterate and format the alert payloads. For example:
*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:
{% 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:
{{ payload | tojson_pretty }}Some commonly used built-in functions include:
abscapitalizetrim
For a full list of Jinja built-in functions, see the Jinja documentation on GitHub
Variables added by Grafana IRM
There are a few extra variables available in the template context of an alert group notification:
grafana_oncall_alert_group_id: The ID of the alert groupgrafana_oncall_link: The absolute URL to the alert groupintegration_name: The name of the integration where the alert group was receivedsource_link: Source link as defined by the corresponding integration templateexternal_id: If alert group is connected to an external resource (e.g. ServiceNow incident), this would be the ID of that resource in the external serviceexternal_link: The link to the external resource the alert group is connected tolabels: Labels (key, value) assigned to the alert group
Functions added by Grafana IRM
Grafana IRM enhances Jinja with additional functions:
time: Current timetojson: Dumps a structure to JSONtojson_pretty: Same astojson, but prettifiediso8601_to_time: Converts ISO8601 time (2015-02-17T18:30:20.000Z) to datetimeiso8601_to_timestamp: Converts ISO8601 time (2015-02-17T18:30:20.000Z) to Unix timestamp (e.g.,1713796200). Note: microseconds are not included in the output. If you need microsecond precision, useiso8601_to_timeinstead:{{ ("2015-02-17T18:30:20.123Z" | iso8601_to_time).timestamp() }}should return1424197820.123.datetimeformat: Converts datetime to string according to strftime format codes (%H:%M / %d-%m-%Yby default)datetimeformat_as_timezone: Converts datetime to string with timezone conversion (UTCby default)- Usage example:
{{ payload.alerts.startsAt | iso8601_to_time | datetimeformat_as_timezone('%Y-%m-%dT%H:%M:%S%z', 'America/Chicago') }}
- Usage example:
datetimeparse: Converts string to datetime according to strftime format codes (%H:%M / %d-%m-%Yby default)timedeltaparse: Converts a time range (e.g.,5s,2m,6h,3d) to a timedelta that can be added to or subtracted from a datetime- Usage example:
{% set delta = alert.window | timedeltaparse %}{{ alert.startsAt | iso8601_to_time - delta | datetimeformat }}
- Usage example:
timestamp_to_datetime: Converts a Unix/Epoch time to a datetime objectregex_replace: Performs a regex find and replaceregex_match: Performs a regex match, returnsTrueorFalse- Usage example:
{{ payload.ruleName | regex_match(".*") }}
- Usage example:
regex_search: Performs a regex search, returnsTrueorFalse- Usage example:
{{ payload.message | regex_search("Severity: (High|Critical)") }}
- Usage example:
b64decode: Performs a base64 string decode- Usage example:
{{ payload.data | b64decode }}
- Usage example:
parse_json: Parses a JSON string to an object- Usage example:
{{ (payload.data | b64decode | parse_json).name }}
- Usage example:



