How To Use Alert Message Templates in Grafana

Great info. Would you happen to know how to get rid of the very top text in the Slack alert? My alerts look slightly similar, and the top text is too redundant for me. I’m not sure which portion of the alert it is to make it blank.

I had the same question…I cannot seem to customize this text, plus there are always two rows that contain the same text:

image

Compare the above with Slack alerts coming from InfluxDB:

image

@melori.arellano boa tarde, tudo bem?
Você pode dá um exemplo como montar mensagem de alerta para o Google
Hangouts Chat ?
Desde ja agradeço

@melori.arellano @grant2 Do you know of a solution for this one? The issue is with Classic Condition where you don’t know all of the keys e.g. B0 in advance

Also where does the “with $values” syntax come from? I am not able to find “with” in golang, Grafana or Prometheus docs

@melori.arellano Thank you for your reply. I was able to make this work following your steps (successfully receiving the emails now). I do have one final question - is it currently possible to format the Annotations (newline, etc). I tried to use some HTML syntaxes but they didn’t work.

alert-email

@melori.arellano @grant2
Actually, one more question - I have also realized if I choose to templatize my alert messages like the steps mentioned above, I do not have the ability to send other default annotations in the email anymore (Summary, description, Runbook URL, etc). Am I understanding this correctly?

@milinpatel13298

Yes, if you create a custom template for your alert messages, you should be able to include (in that custom template) the default annotations like Summary, Description, SilenceURL, etc.

@grant2
Thank you for replying so quickly.
Would that be by modifying this to add {{ .Annotations.Summary }} and such? If so, how are you able to handle the space in “Runbook URL”?

1 Like

Here is my template:

{{ define "slack.body" }}
{{- range .Alerts -}}
{{ if gt (len .Annotations) 0 }}
*Summary*: {{ .Annotations.summary }}
*Description*: {{ .Annotations.description }}
{{ end }}
{{ end }}
{{ end }}

For Runbook URL, I have never used/tried that but acc. to this, the syntax is runbook_URL and it should take the URL defined here (note that there is no _ in this syntax).

@grant2
Thanks again!
That’s interesting!
So, then theoretically, I should be able to use the label/variables in my Summary and Description annotations and then bring them into my email message without having to creating a custom AlertValues annotation from the example shown by @melori.arellano, and only needing to create a myalert and mymessage template instead ?

1 Like

@milinpatel13298 I posted a similiar question here. Give me a hint If you succed. Thanks.

Hi @grant2
I was trying to test this so I went and created a Grafana Cloud instance for myself since I do not have admin privileges at work. I have realized that it is possible to parse label values without even needing to create any custom message templates. All I did was use the variables in the default Description annotation (something like below)

{{ with $values }}
{{ $values.B }} value found for {{ $values.B.Labels.state }}. Please investigate.
{{ end }}

This is the preview Alerts output:
[ var=‘B’ labels={state=AL} value=-0.72494383 ], [ var=‘C’ labels={state=AL} value=1 ]

Now, the only difference I could tell between these two Grafana instances is that one is “Cloud Pro” and the other is “Open Source” and the other difference is that the Cloud Pro is v9.2.7-0f96ed148c (0f96ed148c) while the Open Source is v9.1.5 (df015a9301).

Do you know if they fixed this in v9.2.7 (therefore, being a version issue) or is this a cloud vs open source issue?

Also, tagging @melori.arellano in case she has an answer and @georgerobinson since I noticed he is working on some documentation improvements related to this.

I really hope that it’s a version issue so I can just upgrade and have the issue resolved rather than having to create these custom message templates since they don’t have much formatting options like the way the default alert messages look.

Hi! I’m afraid the preview button cannot be used to preview custom labels and annotations (with or without templating) because the preview button is an estimation of the alert condition in the user interface, and doesn’t use alerting to expand custom labels and annotations. We know that this is an issue and are planning to address it soon.

HI @georgerobinson
Thank you for the quick response.
I think there is a misunderstanding. The Preview Alerts content was just to provide context so that the content in the Descrption annotation made sense.

The question really is that it is not possible in Open Source v9.1.5 (df015a9301) to get those values populated in the alert email without creating a custom message template and using it in the email contact point, like @melori.arellano has pointed out and provided steps for in this discussion. But, like I mentioned, I am able to do this when I’m using Cloud Pro v9.2.7-0f96ed148c (0f96ed148c). Therefore, I would like to know if this is a version issue that has already been addressed or a Cloud vs open source issue?

Hi! Yes this feature is available in all versions of Grafana 9.x.

1 Like

@gregbeyer I am working on consolidating this information into an official tutorial and have shared the post with our doc writers. They’ve started to add the information into official docs.

The community is so helpful for us to learn where people get stuck and where our docs can use improvement - the number of views and comments in this post show us that there is more work to do and we’re working on it!

@wrain36 it seems like the annotation is correct. In the alert view, you should be able to see the labels in the UI. If that looks as you expect, then the problem is probably with defining and using the message template. I would suggest using the one in my example to get everything working and then customize from there.

Here’s an example (not exactly your use case) of how you’ll see the labels if they’re parsed correctly:

@sstewart it’s related to using pipelines with go templates:

hi @ardianre , were you able to figure out how to do this? I have the exact same usecase. A webhook API wherein I want to expose only certain fields from the entire JSON payload.
@melori.arellano can you help here? Is this even possible?
My grafana version is 9.2.3