alertmanager icon indicating copy to clipboard operation
alertmanager copied to clipboard

Webhook supports custom message, Make webhook notification more generic. (e.g. dingtalk/wechat ...)

Open virnet opened this issue 3 years ago • 4 comments

Signed-off-by: 孙林耀 [email protected]

virnet avatar Sep 24 '20 07:09 virnet

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.

simonpasquier avatar Sep 25 '20 15:09 simonpasquier

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

patch_ctype_after_PR2378.diff.txt

yo000 avatar Jul 23 '21 13:07 yo000

What's stopping this from being merged? This would be really nice to have.

thequailman avatar Mar 28 '22 01:03 thequailman

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 avatar Mar 29 '22 00:03 roidelapluie

@roidelapluie @simonpasquier what do you think about jsonnet as an alternative?

yuri-tceretian avatar Mar 22 '23 19:03 yuri-tceretian

@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.

simonpasquier avatar Apr 07 '23 15:04 simonpasquier

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.

jan--f avatar Apr 11 '23 09:04 jan--f

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.

MicroOps-cn avatar May 04 '23 08:05 MicroOps-cn

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.

simonpasquier avatar May 04 '23 14:05 simonpasquier