alertmanager
alertmanager copied to clipboard
Webhook supports custom message, Make webhook notification more generic. (e.g. dingtalk/wechat ...)
Signed-off-by: 孙林耀 [email protected]
Thanks for your contribution! It's already been discussed before (see https://github.com/prometheus/alertmanager/issues/701) but we're not sure yet how it can be solved properly. In any case, generating JSON data from Go templates isn't the right approach IMHO as it's going to be error prone and brittle.
I use virnet patch with minor mods (included), so we can notify to webhooks with formencoded content POST API.
in alertmanager.yml :
- '/usr/local/etc/alertmanager/template/form.tmpl'
receivers:
- name: 'astreinte_sms'
webhook_configs:
- url: 'https://sms.service.com/api/sms/send'
content_type: 'application/x-www-form-urlencoded'
text: '{{ template "sms_service.text" . }}'
In template/form.tmpl :
cat << EOF >> template/form.tpl
{{ define "sms_service.text" }}key=__insert_token__&message={{ .GroupLabels.status }}:{{ .GroupLabels.alertname }}:{{ .CommonAnnotations.description }}&destinataires="+0123456789"&expediteur=sender{{ end }}
EOF
What's stopping this from being merged? This would be really nice to have.
See Simon's comment.
I agree that it is awesome to be able to customize the message, but go templates are far from ideal for this.
@roidelapluie @simonpasquier what do you think about jsonnet as an alternative?
@yuri-tceretian I think that @jan--f did an experiment but it wasn't really successful (the processing wasn't really efficient). As much as I like jsonnet (or at least not dislike it), I'm wary that there would be a usability gap as not so many people are familiar with it.
Yes the experiment can still be found at https://github.com/jan--f/alertmanager/tree/webhook-text-templates.
This uses github.com/google/go-jsonnet 0.17, so maybe something has changed since then.
Simply run go test ./notify/webhook/ -bench . -memprofile mem.pprof -cpuprofile cpu.pprof -count 10
on one of the last three commits on that branch (they implement different approaches).
This is a very naive implementation no doubt but I think I saw 40x - 80x slowdown even for a simple oneline template.
Thanks for your contribution! It's already been discussed before (see #701) but we're not sure yet how it can be solved properly. In any case, generating JSON data from Go templates isn't the right approach IMHO as it's going to be error prone and brittle.
I also think it's not very wise to generate JSON through go templates, but I haven't found a better solution.
When you say 'it's going to be error prone and brick', do you mean that there are special symbols that can cause JSON formatting exceptions when generating JSON?
I have come up with a way to use the custom function toJson/safeJson to escape any special characters that may occur, thus avoiding this issue.
My main worry is that it will very hard for people to use and debug. Go templates aren't made to generate structured format like JSON.