Documentation of Alert Templating in Serious Need of Improvement!

Hey

could you please show us the actual emoji you used in your alert code?

Hi @yosiasz

I used like below.

{{ range .Alerts }} :warning:PMM | {{ .Labels.severity}} | {{ .Labels.node_name}} | {{ .Annotations.summary }}

Hi @yosiasz

Is it correct way I am using the emoji or any changes needed to get actual emoji color in the mail subject?

Honestly I am not sure. one way it to try things by process of elimination. try to send alerts to something else like slack or teams or telegram

If it does not function well in these then it could be grafana alerts itself.

Sure @yosiasz

Thanks for the help.

Hello @simpres99, and thank you for hearing us. Regarding collecting feedback on the documentation, that sounds like a great idea – instead of you having to hunt through dozens of posts. Please let us know how you would like us to provide what you need. Perhaps a dedicated forum under Alerting? However we can help, we will.

Oops, DM’d the wrong person. @georgerobinson, pls see above.

Hi @georgerobinson

How can I get Label values in the new line?

{{ define “email.body” }}

{{- range .Alerts -}}

{{ if gt (len .Annotations) 0 }}

Summary: {{ .Annotations.summary }}

Description: {{ .Annotations.description }}

Labels:

{{ range .Labels.SortedPairs }} {{ if or (eq .Name “ENV”) (eq .Name “APP_TYPE”) }} • {{ .Name }}: {{ .Value }}

{{ end }}{{ end }}

{{ end }}

{{ end }}

{{ end }}

I am using the above code but I am getting the output like this.

Summary: MariaDB Repl Lag is 4104 Secs Behind for C-DEV4-RO Description: MariaDB Slave is Lagging Behind Of 4104 Seconds From http://test402.test.com to test404: C-DEV4-RO Labels: • APP_TYPE: DEV_ALL_TRACKS • ENV: C-STACK-DEV

But I want output like below.

Summary: MariaDB Repl Lag is 4104 Secs Behind for C-DEV4-RO
Description: MariaDB Slave is Lagging Behind Of 4104 Seconds From http://test402.test.com to test404: C-DEV4-RO
Labels:
APP_TYPE: DEV_ALL_TRACKS
ENV: C-STACK-DEV

@georgerobinson Can you please help me on the above?

Hi! What about the something like this?

Labels:
{{ range .Labels.SortedPairs }}{{ if or (eq .Name "foo") (eq .Name "bar") -}}
- {{ .Name }}: {{ .Value }}
{{ end }}{{ end }}

Screenshot 2022-12-09 at 16.25.42

@georgerobinson Yeah this looks good, let me try and update you on the same.

@georgerobinson Still I am not getting the same issue?

{{ define “email.body” }}

{{- range .Alerts -}}

{{ if gt (len .Annotations) 0 }}

Summary: {{ .Annotations.summary }}

Description: {{ .Annotations.description }}

Labels:

{{ range .Labels.SortedPairs }} {{ if or (eq .Name “ENV”) (eq .Name “APP_TYPE”) -}} - {{ .Name }}: {{ .Value }}

{{ end }}{{ end }}

{{ end }}

{{ end }}

{{ end }}

Hi! What email client is this?

@georgerobinson
Apologies for jumping in here, I have alerts + image rendering to S3 + notifications to teams with images all working (and deployed via helm). Its taken a week and 100’s of searches but I got there…

The final hurdle is making the messages pretty and as noted in this thread, the template documentation is not very clear… I’m copy/pasting/editing bits of the the default template:

into my own template but can’t figure out what part is responsible for the image?
Can you help?

Thanks,
Tom

Hi @georgerobinson

Its a Microsoft Office Outlook.

Hi Tom! Images are added to notifications after templating as its difficult to provide a consistent templating experience for images given the varied contact points supported in Grafana. This is something we could reconsider, but it would be good to know what kind of problem you’re looking to solve and whether it would be possible with templates.

1 Like

Hi @georgerobinson

Its a Microsoft Office Outlook.

Hi @georgerobinson ,

Thanks for the reply.
Thats useful info - I think the template I was playing with sometimes contained errors which caused screenshots to go missing, leading me to think it was a function of the template. I was trying to have some text below the image, but its entirely cosmetic.

Back to the main topic of this thread, I think we need more documentaion around converting json from the alert-api into a configmap or equivalent that can be used to provision alerts.
For example, if I create an alert-rule in the UI (v9.3.1), and then get it via the api:

curl -XGET  http://some:password@somegrafana/api/v1/provisioning/alert-rules/1jIgdlKVk | jq . > alert-rule.json

delete the original in the UI and then post back the json, I always get this error:

curl -XPOST  http://some:password@somegrafana//api/v1/provisioning/alert-rules -d @alert-rule.json      
{"message":"bad request data","traceID":""} 

My thinking is that if I can’t get the json to work via the api, it won’t work as a configmap with sidecar.
The example yaml given here is too simple:

and not all the json is adequately described by the api documentation,

for example, its not clear from “AlertQuery” if this is required or not:

        "datasource": {
          "name": "Expression",
          "type": "__expr__",
          "uid": "__expr__"
        },

As others have asked for, it would be nice if we could have an export function in the UI for alerts where similar to dashboards, you can put the json in the required directory/create a config map out of it for a sidecar, and it “just works”!

Thanks,
Tom

1 Like

Hi @georgerobinson

Its a Microsoft Office Outlook.

Can you please help me on this issue?

Hi! Would it be possible to update Grafana to 9.3 as I recall there being a similar issue for line breaks in some email clients Newlines not being preserved in certain email clients · Issue #53879 · grafana/grafana · GitHub.