Alert manager templates

1,700 views
Skip to first unread message

Emmanuel Alan Ferreyra Montes

unread,
Oct 12, 2021, 11:16:10 AM10/12/21
to Prometheus Users
Hello,

I'm following the suggestions I got from here from AWS in order to convert the Alert Manager message into JSON so I can use it in SNS.

Basically, I'm defining a template for SNS message like this:

{{ define "sns.default.message" }}{{ "{" }}"receiver": "{{ .Receiver }}","status": "{{ .Status }}","alerts": [{{ range $alertIndex, $alerts := .Alerts }}{{ if $alertIndex }}, {{ end }}{{ "{" }}"status": "{{ $alerts.Status }}"{{ if gt (len $alerts.Labels.SortedPairs) 0 -}},"labels": {{ "{" }}{{ range $index, $label := $alerts.Labels.SortedPairs }}{{ if $index }}, {{ end }}"{{ $label.Name }}": "{{ $label.Value }}"{{ end }}{{ "}" }}{{- end }}{{ if gt (len $alerts.Annotations.SortedPairs ) 0 -}},"annotations": {{ "{" }}{{ range $index, $annotations := $alerts.Annotations.SortedPairs }}{{ if $index }}, {{ end }}"{{ $annotations.Name }}": "{{ $annotations.Value }}"{{ end }}{{ "}" }}{{- end }},"startsAt": "{{ $alerts.StartsAt }}","endsAt": "{{ $alerts.EndsAt }}","generatorURL": "{{ $alerts.GeneratorURL }}","fingerprint": "{{ $alerts.Fingerprint }}"{{ "}" }}{{ end }}]{{ if gt (len .GroupLabels) 0 -}},"groupLabels": {{ "{" }}{{ range $index, $groupLabels := .GroupLabels.SortedPairs }}{{ if $index }}, {{ end }}"{{ $groupLabels.Name }}": "{{ $groupLabels.Value }}"{{ end }}{{ "}" }}{{- end }}{{ if gt (len .CommonLabels) 0 -}},"commonLabels": {{ "{" }}{{ range $index, $commonLabels := .CommonLabels.SortedPairs }}{{ if $index }}, {{ end }}"{{ $commonLabels.Name }}": "{{ $commonLabels.Value }}"{{ end }}{{ "}" }}{{- end }}{{ if gt (len .CommonAnnotations) 0 -}},"commonAnnotations": {{ "{" }}{{ range $index, $commonAnnotations := .CommonAnnotations.SortedPairs }}{{ if $index }}, {{ end }}"{{ $commonAnnotations.Name }}": "{{ $commonAnnotations.Value }}"{{ end }}{{ "}" }}{{- end }}{{ "}" }}{{ end }}

The only problem I'm facing now is that the double quotes (") are not being properly escaped so the JSON structure is broken. Any suggestion about what to do in this case?

My options are the following functions:
  • safeHtml: it didn't escape the double-quoted
  • reReplaceAll: I didn't manage to properly use it. Any example?
Any other option you can think of?

Thanks,

Emmanuel

Brian Candler

unread,
Oct 12, 2021, 1:12:57 PM10/12/21
to Prometheus Users
"Note the default functions also provided by Go templating".  Have you tried the function "js"?

js
    Returns the escaped JavaScript equivalent of the textual representation of its arguments.

Emmanuel Alan Ferreyra Montes

unread,
Nov 1, 2021, 7:59:37 AM11/1/21
to Prometheus Users
No, it's still failing. Any other option?

Brian Candler

unread,
Nov 1, 2021, 9:27:24 AM11/1/21
to Prometheus Users
Do you want to show your updated template, and what output it produces?

Emmanuel Alan Ferreyra Montes

unread,
Nov 1, 2021, 9:37:09 AM11/1/21
to Prometheus Users
Here is what I have at the moment:

{\"description\": \"Message deleted \"element-xxx\"\", \"message\": \"xxxxxxxxxx\"}

The problem is I should be having something like this:

{\"description\": \"Message deleted \\\"element-xxx\\\"\", \"message\": \"xxxxxxxxxxx\"}

this is because that's the format that the lambda function in AWS is receiving the JSON notification. As a String.

Emmanuel Alan Ferreyra Montes

unread,
Nov 1, 2021, 9:39:03 AM11/1/21
to Prometheus Users
and my template is:

{{ define "sns.default.message" }}{{ "{" }}"receiver": "{{ .Receiver }}","status": "{{ .Status }}","alerts": [{{ range $alertIndex, $alerts := .Alerts }}{{ if $alertIndex }}, {{ end }}{{ "{" }}"status": "{{ $alerts.Status }}"{{ if gt (len $alerts.Labels.SortedPairs) 0 -}},"labels": {{ "{" }}{{ range $index, $label := $alerts.Labels.SortedPairs }}{{ if $index }}, {{ end }}"{{ $label.Name }}": "{{ $label.Value | js }}"{{ end }}{{ "}" }}{{- end }}{{ if gt (len $alerts.Annotations.SortedPairs ) 0 -}},"annotations": {{ "{" }}{{ range $index, $annotations := $alerts.Annotations.SortedPairs }}{{ if $index }}, {{ end }}"{{ $annotations.Name }}": "{{ $annotations.Value }}"{{ end }}{{ "}" }}{{- end }},"startsAt": "{{ $alerts.StartsAt }}","endsAt": "{{ $alerts.EndsAt }}","generatorURL": "{{ $alerts.GeneratorURL }}","fingerprint": "{{ $alerts.Fingerprint }}"{{ "}" }}{{ end }}]{{ if gt (len .GroupLabels) 0 -}},"groupLabels": {{ "{" }}{{ range $index, $groupLabels := .GroupLabels.SortedPairs }}{{ if $index }}, {{ end }}"{{ $groupLabels.Name }}": "{{ $groupLabels.Value }}"{{ end }}{{ "}" }}{{- end }}{{ if gt (len .CommonLabels) 0 -}},"commonLabels": {{ "{" }}{{ range $index, $commonLabels := .CommonLabels.SortedPairs }}{{ if $index }}, {{ end }}"{{ $commonLabels.Name }}": "{{ $commonLabels.Value }}"{{ end }}{{ "}" }}{{- end }}{{ if gt (len .CommonAnnotations) 0 -}},"commonAnnotations": {{ "{" }}{{ range $index, $commonAnnotations := .CommonAnnotations.SortedPairs }}{{ if $index }}, {{ end }}"{{ $commonAnnotations.Name }}": "{{ $commonAnnotations.Value }}"{{ end }}{{ "}" }}{{- end }}{{ "}" }}{{ end }}

I also tried with html but it seems that doesn't work either.

Emmanuel Alan Ferreyra Montes

unread,
Nov 1, 2021, 9:43:47 AM11/1/21
to Prometheus Users
is there any way I can do something like this:

{{ $label.Value | Replace "(\"" ,"\\\"")}}

Brian Candler

unread,
Nov 2, 2021, 4:25:10 AM11/2/21
to Prometheus Users

Emmanuel Alan Ferreyra Montes

unread,
Nov 4, 2021, 1:09:22 PM11/4/21
to Prometheus Users
I finally fixed using reReplaceAll;

{{ reReplaceAll $labels.Value "\"" "\\\"" }}

I did the same for annotations, commonLabels and commonAnnotations.

I don't know why your suggestion didn't work Brian but thanks for your help.
Reply all
Reply to author
Forward
0 new messages