sensu / sensu-slack-handler

The Sensu Go Slack handler for notifying a channel.
https://sensu.io
MIT License
3 stars 26 forks source link

Add support for templating for feature parity with Sensu Core/Enterprise Slack handler #22

Open asachs01 opened 5 years ago

asachs01 commented 5 years ago

As the issue title says, it would be useful to have support to create a template file to use when posting to the Slack API. This is present in both the Core/Enterprise Slack handlers.

calebhailey commented 5 years ago

We should support templates but avoid the usage of a file for config (because containers). We've already done some work around this in our plugin library that should make this easy to support.

gojkoz commented 4 years ago

We would love to see templating support too

sensu-discourse commented 4 years ago

This issue has been mentioned on Sensu Community. There might be relevant details there:

https://discourse.sensu.io/t/add-custom-output-to-check-http-rb-and-pass-it-to-slack/1656/3

asachs01 commented 4 years ago

I think this issue is resolved by the code that you can find in https://github.com/sensu/sensu-slack-handler/blob/master/main.go#L83. I'll have to play with it a bit more, but I'd say this solves the issue.

gojkoz commented 4 years ago

I saw that commit, but it's not had a release yet, correct? Can you make a new sensu-slack-handler release please to include this change?

Thanks

asachs01 commented 4 years ago

@gojkoz a release has been pushed. Can you give it a shot to see if this provides the functionality you need?

gojkoz commented 4 years ago

It's not a full template modification, but it's a start :) It allowed me to modify the "description" field of the notification output only. I was able to squeeze in something other than .Check.Output in it, that we find useful in the slack notifications.

However it is kind of limiting, for example I can't do any parsing (like wanted to use time.Parse to parse the Timestamp field to something readable. I was not able to add line breaks, they just show up as \n or
in the slack channel. Not sure if you know another way of adding a line break there, just to help with making the output more readable?

I was also not able to use ":" in the template (quoted and everything), sensuctl errors with 'mapping values are not allowed in this context'. I think it wants to parse that column as part of my yaml definition. Ie: command: sensu-slack-handler --channel '#channel' --username 'Sensu' -w $SLACK_WEBHOOK_URL -t '{{ .Check.Output }} Occurrences: {{ .Check.Occurrences }}' This causes sensuctl error when you try to apply it. But if you use it manually on the CLI to invoke the handler binary, it works fine, so the handler itself does not have a problem with that ":".

I guess if these were all fully templated, this would be more flexible. For example just next to (to the right of) the Status field in the slack notification, I'd like to add Occurrences. Not sure how hard it would be to load the whole template definition from a file, similar to what you do for the slack email handler? That and fix to allow some kind of formatting and timestamp parsing would be awesome.

jsnod commented 3 years ago

Agreed, the default alerts take up way too much space:

Screen Shot 2020-12-01 at 2 38 46 PM

Ideally we could customize this with a template string so that we can condense all that wasted whitespace into 1 or 2 lines if desired. Also note I added the occurrences with -t "{{ .Check.Output }} ({{ .Check.Occurrences }} occurrences)" but you can see there is an extra linebreak after the Check.Output. Where does that come from? If it's in the output itself perhaps it should be trimmed?