Need help using variables in an Email notification message

Dear Colleagues,

When configuring a contact point of type “Email”, there is a text box for an optional message to include with the Email. How can I use variables in this message (what syntax)? Also, how can I change the Subject of the notification Email?

When I put something like “This is a mail alert {{ .Annotations.summary }} {{ .Annotations.description }}” into the text box, and then send a test notification, the test Email arrives only with the “This is a mail alert” text without any variable expansion. The syntax of what can be used in the “Message” text box should be documented somewhere, with examples? Can you help with the RTFM, or show some examples you personally use there?

grafana-server 8.3.3, Debian 10

Hi @victorsudakov, welcome to the community!

I’m not sure if the Annotations variable can be used in the message body. The docs here say that they can be used in message templates. Those are defined separately, then they can be referenced in that message text box with the special notation {{template "yourTemplateName".}}

The docs have the steps for creating message templates. There are some examples of custom templates too.

Hello @melori.arellano !

Thank you for your welcome and reply. It is however not working quite as expected. I define a template:

{{ define "Simple" }}
  [{{.Status}}] {{ .Labels.alertname }}

  Labels:
  {{ range .Labels.SortedPairs }}
    {{ .Name }}: {{ .Value }}
  {{ end }}

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

{{ end }}

Then I refer to this template in my Email contact point and test the contact point. But the resulting Email contains just the Status, no labels or annotations. Where could the rest of the template be? Is anything wrong with the template?

Due to some strange restrictions, I’m not permitted to post more than 1 image, so I had to stitch all the screenshots together, sorry for the inconvenience.

When the template looks like this:

{{ define "Simple2" }}
Simple 2 first line
Labels:
Simple 2 second line

Simple 2 third line
{{ end }}

I get the Emails with the text " Simple 2 first line Labels: Simple 2 second line Simple 2 third line" .

When I add variables and the template becomes like this

{{ define "Simple2" }}
Simple 2 first line
Labels:
Simple 2 second line
{{ range .Labels.SortedPairs }}
    {{ .Name }}: {{ .Value }}
  {{ end }}
Simple 2 third line
{{ end }}

The Email becomes " Simple 2 first line Labels: Simple 2 second line " so everything after the first “{{” is dropped. Why is that?

Hi, did you find any solution?
I’m trying to personalize email message using templates… but seems to me very difficult and I don’t know how to remove and personalize parts.

THANKS Alen

No, unfortunately I have found neither a solution nor community help with the problem. BTW I have a similar problem with Telegram notification templates, just cannot make them work and nobody is willing to share a working example.

1 Like

OK thanks… now I try to modify directly the HTML file…
into docker app…
/usr/share/grafana/public/emails
ALEN

So you have chosen the Dark Side? :slight_smile:

1 Like

Hi victor - are you using Grafana 8 alerting? I just want to double check that you’re not using the older “Legacy” Alerting.

If you are using Grafana 8 alerting, each message to a contact point can include multiple alerts. So in your examples you need to wrap a {{ range .Alerts }} around your annotations.

{{ define "Simple2" }}
Simple 2 first line
Labels:
Simple 2 second line
{{ range .Alerts }}
{{ range .Labels.SortedPairs }}
    {{ .Name }}: {{ .Value }}
  {{ end }}{{ end }}
Simple 2 third line
{{ end }}
1 Like

Hello Joeb,

I’m currently using Grafana v9.0.3 and I’m not aware of any legacy alerting. I just click on the bell icon in the Web UI and configure Alert rules, Contact points etc.

I’ll try your advice. Please tell me if it works only for E-mail type alerts, or for Telegram alerts too ? I currently need a good-looking Telegram alert, the default is really excessive and ugly. Is there a working example of a concise Telegram alert somewhere?

I would like to see a sample code I could paste into the Optional Telegram seettings → Message text area, with variables. If I enter a plain text message there, I receive it in Telegram all right. The problem is how to add variables.

1 Like

Ran into the same issue on 9.0.3, solution for me was to wrap it in:
{{ range .Alerts.Firing }}
{{ end }}

You might want to also add checks to prevent any unseen internal exceptions, like so:
{{ if gt (len .Alerts.Firing) 0 }}
{{ range .Alerts.Firing }}
{{ if gt (len .Annotations) 0 }}
{{ .Annotations.summary }}
{{ .Annotations.description }}
{{ end }}
{{ end }}
{{ end }}

Hopefully the documentation can be updated to make this clearer.

Thanks a lot for the hint @barbarianx3 ! It helped. Do you have a nice Telegram message at hand? I have come up with the following so far for Telegram, but maybe there are ways to make it nicer and more informative?

{{ if gt (len .Alerts.Firing) 0 }}
FIRING: {{ len .Alerts.Firing }} alerts
{{ range .Alerts.Firing }}
{{ if gt (len .Annotations) 0 }}
Summary: {{ .Annotations.summary }}
Description: {{ .Annotations.description }}
Labels: {{ range .Labels.SortedPairs }} {{ .Name }}: {{ .Value }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
RESOLVED: {{ len .Alerts.Resolved }} alerts
{{ range .Alerts.Resolved }}
{{ if gt (len .Annotations) 0 }}
Summary: {{ .Annotations.summary }}
Description: {{ .Annotations.description }}
Labels: {{ range .Labels.SortedPairs }} {{ .Name }}: {{ .Value }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}

1 Like

I have yet to receive a discord alert from a simple sql count query. Any help please? These alerts can be such a bear! :bear:

This is where one needs to go to grasp the details. wonderful video!

I politely disagree. This video may be good for beginners, but even beginners do not always need a walk through the GUI, because it’s pretty obvious.

What I would really like to see is a document on message templates and using variables therein. The Message templating | Grafana documentation is not particularly good as a tutorial or even as a reference.

2 Likes

I am below beginner :smile: yeah the documentation on alerts is wack. Out of all things grafana alerts have been causing me a fit

Yeah, Zabbix alerts (triggers) are much better designed.

My wish is a vs code plugin for alerts with intlisense without the {{{{{{{{

And I may one day lose my temper and move all my Grafana alerts to Zabbix (which has a Prometheus preprocessor natively).

I have nothing better than what you created. I only use the alerts for a simple mail to inform people every monday which devices need attention the coming week based on some variables. I just fill the description in the alert rule, and use the template to combine them in one mail/pushover message. I have an alert setup that usually produces multiple notifications. Apparently in Grafana each of those results becomes a notification in the backend, and I group those based on one of the labels using the notification pool. So I think the main improvement that Grafana could use in this case is to make the naming more uniform, so that a notification in the frontend would correspond to a notification in the template (which is now confusingly also called an alert).

I agree the documentation is currently very weak on this, it helps that I’m a programmer and could follow their line of thinking, but you’d never figure it out without a working example.

2 Likes