Template reference
This documentation topic is designed for Grafana workspaces that support Grafana version 9.x.
For Grafana workspaces that support Grafana version 10.x, see Working in Grafana version 10.
For Grafana workspaces that support Grafana version 8.x, see Working in Grafana version 8.
This section provides reference information for creating your templates.
Template data
The following data is passed to message templates.
Name | Type | Notes |
---|---|---|
|
string |
Name of the contact point that the notification is being sent to. |
|
string |
firing if at least one alert is firing, otherwise resolved. |
|
Alert |
List of alert objects that are included in this notification (see below). |
|
KeyValue |
Labels these alerts were grouped by. |
|
KeyValue |
Labels common to all the alerts included in this notification. |
|
KeyValue |
Annotations common to all the alerts included in this notification. |
|
string |
Back link to the Grafana that sent the notification. If using external Alertmanager, back link to this Alertmanager. |
The Alerts
type exposes two functions for filtering the alerts
returned.
-
Alerts.Firing
– Returns a list of firing alerts. -
Alerts.Resolved
– Returns a list of resolved alerts.
Alert (type)
The alert type contains the following data.
Name | Type | Notes |
---|---|---|
Status |
string |
|
Labels |
KeyValue |
A set of labels attached to the alert. |
Annotations |
KeyValue |
A set of annotations attached to the alert. |
Values | KeyValue | The values of all expressions, including Classic Conditions |
StartsAt |
time.Time |
Time the alert started firing. |
EndsAt |
time.Time |
Only set if the end time of an alert is known. Otherwise set to a configurable timeout period from the time since the last alert was received. |
GeneratorURL |
string |
A back link to Grafana or external Alertmanager. |
SilenceURL |
string |
A link to silence the alert (with labels for this alert pre-filled). Only for Grafana managed alerts. |
DashboardURL |
string |
Link to grafana dashboard, if alert rule belongs to one. Only for Grafana managed alerts. |
PanelURL |
string |
Link to grafana dashboard panel, if alert rule belongs to one. Only for Grafana managed alerts. |
Fingerprint |
string |
Fingerprint that can be used to identify the alert. |
ValueString |
string |
A string that contains the labels and value of each reduced expression in the alert. |
ExtendedData
The ExtendedData object contains the following properties.
Name | Kind | Description | Example |
---|---|---|---|
Receiver |
|
The name of the contact point sending the notification. |
|
Status |
|
The status is |
|
Alerts |
|
List of all firing and resolved alerts in this notification. |
|
Firing alerts |
|
List of all firing alerts in this notification. |
|
Resolved alerts |
|
List of all resolved alerts in this notification. |
|
GroupLabels |
|
The labels that group these alerts int his notification. |
|
CommonLabels |
|
The labels common to all alerts in this notification. |
|
CommonAnnotations |
|
The annotations common to all alerts in this notification. |
|
ExternalURL |
|
A link to the Grafana workspace or Alertmanager that sent this notification. |
|
KeyValue type
The KeyValue
type is a set of key/value string pairs that represent
labels and annotations.
In addition to direct access of the data stored as a
KeyValue
, there are also methods for sorting, removing, and
transforming the data.
Name | Arguments | Returns | Notes | Example |
---|---|---|---|---|
SortedPairs |
Sorted list of key and value string pairs |
|
||
Remove |
[]string |
KeyValue |
Returns a copy of the Key/Value map without the given keys. |
|
Names |
[]string |
List of label names |
|
|
Values |
[]string |
List of label values |
|
Time
Time is from the Go time
Monday, 1st January 2022 at 10:00AM
,
you write the following template:
{{ .StartsAt.Format "Monday, 2 January 2006 at 3:04PM" }}
You can find a reference for Go’s time format here
Template functions
Using template functions you can process labels and annotations to generate dynamic notifications. The following functions are available.
Name | Argument type | Return type | Description |
---|---|---|---|
|
number or string |
string |
Converts a number to a more readable format, using metric prefixes. |
|
number or string |
string |
Like humanize, but uses 1024 as the base rather than 1000. |
|
number or string |
string |
Converts a duration in seconds to a more readable format. |
|
number or string |
string |
Converts a ratio value to a fraction of 100. |
|
number or string |
string |
Converts a Unix timestamp in seconds to a more readable format. |
|
string |
string |
strings.Title, capitalizes first character of each word. |
|
string |
string |
strings.ToUpper, converts all characters to upper case. |
|
string |
string |
strings.ToLower, converts all characters to lower case. |
|
pattern, text |
Boolean |
regexp.MatchString Tests for an unanchored regexp match. |
|
pattern, replacement, text |
string |
Regexp.ReplaceAllString Regexp substitution, unanchored. |
|
string - JSON Object with |
string |
Returns the path to graphical view in Explore for the given expression and data source. |
|
string - JSON Object with |
string |
Returns the path to tabular view in Explore for the given expression and data source. |
|
[]interface{} |
map[string]interface{} |
Converts a list of objects to a map with keys, for example, arg0, arg1. Use this function to pass multiple arguments to templates. |
|
nothing |
string |
Returns a string representing the external URL. |
|
nothing |
string |
Returns the path of the external URL. |
The following table shows examples of using each function.
Function | TemplateString | Input | Expected |
---|---|---|---|
humanize |
{ humanize $value } |
1234567.0 |
1.235M |
humanize1024 |
{ humanize1024 $value } |
1048576.0 |
1Mi |
humanizeDuration |
{ humanizeDuration $value } |
899.99 |
14m 59s |
humanizePercentage |
{ humanizePercentage $value } |
0.1234567 |
12.35% |
humanizeTimestamp |
{ humanizeTimestamp $value } |
1435065584.128 |
2015-06-23 13:19:44.128 +0000 UTC |
title |
{ $value | title } |
aa bB CC |
Aa Bb Cc |
toUpper |
{ $value | toUpper } |
aa bB CC |
AA BB CC |
toLower |
{ $value | toLower } |
aa bB CC |
aa bb cc |
match |
{ match "a+" $labels.instance } |
aa |
true |
reReplaceAll |
{{ reReplaceAll "localhost:(.*)" "my.domain:$1" $labels.instance }} |
localhost:3000 |
my.domain:3000 |
graphLink |
{{ graphLink "{\"expr\": \"up\", \"datasource\": \"gdev-prometheus\"}" }} |
/explore?left=["now-1h","now","gdev-prometheus",{"datasource":"gdev-prometheus","expr":"up","instant":false,"range":true}] |
|
tableLink |
{{ tableLink "{\"expr\":\"up\", \"datasource\":\"gdev-prometheus\"}" }} |
/explore?left=["now-1h","now","gdev-prometheus",{"datasource":"gdev-prometheus","expr":"up","instant":true,"range":false}] |
|
args |
{{define "x"}}{{.arg0}} {{.arg1}}{{end}}{{template "x" (args 1 "2")}} |
1 2 |
|
externalURL |
{ externalURL } |
http://localhost/path/prefix |
|
pathPrefix |
{ pathPrefix } |
/path/prefix |