Most of my dashboards have variables like $region or $customer which can hold multiple values.
When i create an alert and a notification is sent to a MS Teams channel, there is a link to dashboard URL included.
That dashboard URL will redirect user to the dashboard with default variable values pre-selected (e.g. $region : east_usa, $customer : All).
What i would like to do: customize “DashboardURL” property of notification template in such a way so that if an alert happens to be triggered from a region “west_europe” and a customer “siemens”, upon the click of the link in Teams channel the user would be redirected to the dashboard with $region : west_europe and $customer : siemens already pre-selected.
Is such a trick possible?
I would stay yes. Create custom notification template and append your variables (they must be available as labels from the query) as correct get parameters to that default dashboardURL.
Yes, it’s possible to customize the “DashboardURL” property in the notification template to dynamically include the selected variable values. You can modify the URL by appending query parameters for the specific variables, such as $region
and $customer
. This way, when an alert triggers, the notification will include a URL that redirects users to the dashboard with the correct variable selections, ensuring the dashboard loads with the desired values for those variables, like $region=west_europe
and $customer=siemens
, already pre-selected.
then what would be the correct syntax to construct a custom dashboard URL?
I tried using “?” and “&” and variable names, as i was doing when constructing URLs in panel’s Data Links - but it doesn’t work:
{{ $baseURL := .DashboardURL }}
[Go to Grafana dashboard]({{ $baseURL }}?var-metrics_region={{ $region }}&var-metrics_customer={{ $customer }})
Grafana complains about undefined variable:
Both the $region and the $customer variables are defined and available as labels from the query.
Here is the entire notification template with 2 lines highlighted as they were added when trying to craft a custom dashboard URL. If you remove those 2 highlighted lines, you get the template that works perfectly:
{{ define “MY_TEMPLATE” }}
{{ if .Alerts.Firing }}
{{ range $index, $alert := .Alerts.Firing }}
{{ if index .Labels “kubernetes.labels.region.keyword” }}
{{ $region := index .Labels “kubernetes.labels.region.keyword” }}
Region:{{ toUpper $region }}
{{ end }}
{{ if index .Labels “kubernetes.labels.namespace.keyword” }}
{{ $customer := index .Labels “kubernetes.labels.namespace.keyword” }}
{{ $customer := reReplaceAll ^[^-]+-([^-]+)-.*$ $1 $customer }}
Customer: {{ toUpper $customer }}
{{ end }}
{{ if index .Labels “kubernetes.labels.version.keyword” }}
Server version: {{ index .Labels “kubernetes.labels.version.keyword” }}
{{ end }}
{{ if index .Labels “kubernetes.pod_name.keyword” }}
Pod: {{ index .Labels “kubernetes.pod_name.keyword” }}
{{ end }}
{{ $baseURL := .DashboardURL }}
:right_arrow: [Go to Grafana dashboard]({{ $baseURL }}?var-metrics_region={{ $region }}&var-metrics_customer={{ $customer }})
════════════════
{{ end }}
{{ end }}
{{ end }}
I would say that your problem is a variable scope in this case.
A variable’s scope extends to the “end” action of the control structure (“if”, “with”, or “range”) in which it is declared