Define link on Grafana Template

Hi Team,

I am defining panel link on Grafana message template but it is showing up as normal text

But I want something like this on the message.

This is my code:

{{- define “slack.text” -}}
{{- range .Alerts -}}
{{ if gt (len .Annotations) 0 }}
Summary: {{ .Annotations.summary}}

Description: {{ .Annotations.description }}

Go to dashboard: {{ .DashboardURL }}
{{ end }}
{{ end }}
{{ end }}

Can anybody please assist me on this.

Regards,
Zeeshan Khan

1 Like

Is this Microsoft Teams? If so, links need to be formatted as Markdown links with [link name](link url).

2 Likes

[Go to dashboard]: {{ (.DashboardURL) }}

Do i need to write on this way?

More like:

Go to Dashboard: [{{ .DashboardURL }}]({{ .DashboardURL }})
2 Likes

It worked. Thank You :slightly_smiling_face:

You’re welcome! I’m not sure if that works in Slack, so you might need to create a separate template for Microsoft Teams and Slack.

1 Like

Hi, Im trying to do the same for Slack but it is not working, do you know what would be the way for sending this to Slack, please?

Hi all,
I’m working on the email template, and no matter what I’ve done, the Dashboard/Panel/SilenceURL won’t appear in my alert email content.
They are always empty. My grafana version is 9.5.1.

— Below is not working in this grafana instance —

Silence the alert: {{ .SilenceURL }}

Go to dashboard: {{ .DashboardURL }}

Go to panel: {{ .PanelURL }}


Also, this is showing empty as well.

— Below is not working in this grafana instance —

{{ if gt (len .Annotations) 0 }}
Annotations:
{{ range .Annotations.SortedPairs }}
{{ .Name }}: {{ .Value }}
{{ end }}
{{ end }}

{{ if gt (len .CommonAnnotations) 0 }}
CommonAnnotations:
{{ range .CommonAnnotations.SortedPairs }}
{{ .Name }}: {{ .Value }}
{{ end }}
{{ end }}

{{ if gt (len .Labels) 0 }}
Labels:
{{ range .Labels.SortedPairs }}
{{ .Name }}: {{ .Value }}
{{ end }}
{{ end }}

My email is showing empty content for the above.
Any idea please?

Thanks,
Hewon

You need to iterate over the alerts first, for example:

{{ range .Alerts }}
{{ if gt (len .Labels) 0 }}
Labels:
{{ range .Labels.SortedPairs }}
{{ .Name }}: {{ .Value }}
{{ end }}
{{ end }}
{{ end }}

Please see the docs Customize notifications | Grafana documentation.

Thank you!
Do you know why the URL not showing? Are they not supported in v9.5.1?

Silence the alert: {{ .SilenceURL }}

Go to dashboard: {{ .DashboardURL }}

Go to panel: {{ .PanelURL }}

Thanks!

Hi! :wave: Like I said, You need to iterate over the alerts first. For example:

{{ range .Alerts }}
Silence the alert: {{ .SilenceURL }}
{{ end }}

work like a charm!!! Thanks

Thanks , it worked.

Hi,

This is unfortunately not working for me on Grafana 9.2…

This is my template:

{{- /* Example displaying firing and resolved alerts separately in the notification. */ -}}
{{- /* Edit the template name and template content as needed. */ -}}

{{ define "custom.firing_and_resolved_alerts" -}}
{{ len .Alerts.Resolved }} resolved alert(s)
{{ range .Alerts.Resolved -}}
    {{ template "alert.summary_and_description" . -}}
{{ end }}
{{ len .Alerts.Firing }} firing alert(s)
{{ range .Alerts.Firing -}}
  {{ template "alert.summary_and_description" . -}}
{{ end -}}
{{ end -}}

{{ define "alert.summary_and_description" }}
  [testlink](www.google.com)
  {{"\t"}}*Dashboard*: [{{ .DashboardURL }}]({{ .DashboardURL }})
  {{"\t"}}*Panel*: [{{ .PanelURL }}]({{ .PanelURL }})
  {{"\t"}}*Name*: {{ .Labels.alertname }}
  {{"\t"}}*Description*: {{.Annotations.description}}
  {{"\t"}}*Summary*: {{.Annotations.summary}}
  {{"\t"}}*Host*: {{ .Labels.human_name }}
  {{"\t"}}*Starts at*: {{ .StartsAt }}
{{ end -}}

This is the result in Slack:

As you can see it is taking the URL string and rendering it as the link. Instead of respecting the markdown-formatted link.

Do you have any suggestions how to resolve this? Thanks in advance.