Slack newline using {{ if }} in alertmanager

3,279 views
Skip to first unread message

matthew...@anatwine.com

unread,
Feb 7, 2018, 9:35:53 AM2/7/18
to Prometheus Users
I've got a Slack receiver setup in Alertmanager and it uses an if statement to determine if a particular annotation is in use, and if so to change the output. The issue here is that the if statement seems to add a newline and thus makes the Slack message untidy. This is obviously not a huge issue in the grand scheme of things but I'm curious as to whether there is a way around this or if it's an issue with how it generates the output.

This is the relevant part of the receiver config:

    text: >-
     {{ range .Alerts }}
       *Alert:* {{ .Annotations.summary }} - `{{ .Labels.severity }}`
       {{ if ( ne ( .Annotations.description | len ) 0 ) and ( ne ( .Annotations.grafana | len ) 0 ) }}
         *Grafana:* <{{ .Annotations.grafana }}|:chart_with_upwards_trend:>
         *Description:* {{ .Annotations.description }}
       {{ else if ( ne ( .Annotations.grafana | len ) 0 ) and ( eq ( .Annotations.description | len ) 0 ) }}
         *Grafana:* <{{ .Annotations.grafana }}|:chart_with_upwards_trend:>
       {{ else if ( ne ( .Annotations.description | len ) 0 ) and ( eq ( .Annotations.grafana | len ) 0 ) }}
         *Description:* {{ .Annotations.description }}
       {{ end }}
         *Details:*
       {{ range .Labels.SortedPairs }} *{{ .Name }}:* `{{ .Value }}`
       {{ end }}
     {{ end }}



and this is what the output looks like:

Brian Brazil

unread,
Feb 7, 2018, 9:37:24 AM2/7/18
to matthew...@anatwine.com, Prometheus Users
On 7 February 2018 at 14:35, <matthew...@anatwine.com> wrote:
I've got a Slack receiver setup in Alertmanager and it uses an if statement to determine if a particular annotation is in use, and if so to change the output. The issue here is that the if statement seems to add a newline and thus makes the Slack message untidy. This is obviously not a huge issue in the grand scheme of things but I'm curious as to whether there is a way around this or if it's an issue with how it generates the output.

Newer versions of Go have a way to make this a bit easier, see https://golang.org/pkg/text/template/#hdr-Text_and_spaces

Brian
 

This is the relevant part of the receiver config:

    text: >-
     {{ range .Alerts }}
       *Alert:* {{ .Annotations.summary }} - `{{ .Labels.severity }}`
       {{ if ( ne ( .Annotations.description | len ) 0 ) and ( ne ( .Annotations.grafana | len ) 0 ) }}
         *Grafana:* <{{ .Annotations.grafana }}|:chart_with_upwards_trend:>
         *Description:* {{ .Annotations.description }}
       {{ else if ( ne ( .Annotations.grafana | len ) 0 ) and ( eq ( .Annotations.description | len ) 0 ) }}
         *Grafana:* <{{ .Annotations.grafana }}|:chart_with_upwards_trend:>
       {{ else if ( ne ( .Annotations.description | len ) 0 ) and ( eq ( .Annotations.grafana | len ) 0 ) }}
         *Description:* {{ .Annotations.description }}
       {{ end }}
         *Details:*
       {{ range .Labels.SortedPairs }} *{{ .Name }}:* `{{ .Value }}`
       {{ end }}
     {{ end }}



and this is what the output looks like:

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-users+unsubscribe@googlegroups.com.
To post to this group, send email to prometheus-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/60e85748-3905-4b2a-968c-4aa21fba0921%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

matthew...@anatwine.com

unread,
Feb 8, 2018, 8:07:43 AM2/8/18
to Prometheus Users
Yeah, that's sorted it. Cheers.

Matt


On Wednesday, 7 February 2018 14:37:24 UTC, Brian Brazil wrote:
On 7 February 2018 at 14:35, <matthew...@anatwine.com> wrote:
I've got a Slack receiver setup in Alertmanager and it uses an if statement to determine if a particular annotation is in use, and if so to change the output. The issue here is that the if statement seems to add a newline and thus makes the Slack message untidy. This is obviously not a huge issue in the grand scheme of things but I'm curious as to whether there is a way around this or if it's an issue with how it generates the output.

Newer versions of Go have a way to make this a bit easier, see https://golang.org/pkg/text/template/#hdr-Text_and_spaces

Brian
 
--

wcooley...@gmail.com

unread,
Mar 20, 2018, 9:28:16 AM3/20/18
to Prometheus Users
Would you mind posting your updated slack template?
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages