Skip to content

Comments

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

Closed
virnet wants to merge 10 commits intoprometheus:mainfrom
MicroOps-cn:master
Closed

Webhook supports custom message, Make webhook notification more generic. (e.g. dingtalk/wechat ...)#2378
virnet wants to merge 10 commits intoprometheus:mainfrom
MicroOps-cn:master

Conversation

@virnet
Copy link

@virnet virnet commented Sep 24, 2020

Signed-off-by: 孙林耀 sunlinyao@vip.qq.com

Signed-off-by: 孙林耀 <sunlinyao@vip.qq.com>
@simonpasquier
Copy link
Member

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.

@stale stale bot added the stale label Nov 24, 2020
return false, fmt.Errorf("Json格式异常: %s ", body)
}
} else if n.conf.Text != "" {
body, err := n.tmpl.ExecuteTextString(n.conf.Json, data)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/n.conf.Json/n.conf.Text/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@stale stale bot removed the stale label Jul 23, 2021
Comment on lines 119 to 122
err = json.NewEncoder(&buf).Encode(body)
if err != nil {
return false, err
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to json encode if this is Text template imho, "buf.WriteString(body)" should be sufficient

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to json encode if this is Text template imho, "buf.WriteString(body)" should be sufficient

If neither the JSON template nor the text template is configured, then the message should be converted into JSON according to the original logic.

@yo000
Copy link

yo000 commented Jul 23, 2021

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

@thequailman
Copy link

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

@MicroOps-cn MicroOps-cn force-pushed the master branch 4 times, most recently from 57cd3d4 to dc3a414 Compare March 28, 2022 17:25
@roidelapluie
Copy link
Member

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.

@yuri-tceretian
Copy link
Contributor

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

@simonpasquier
Copy link
Member

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

@jan--f
Copy link
Contributor

jan--f commented Apr 11, 2023

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.

… alarm messages and avoid disrupting JSON formats due to line breaks, quotation marks, etc.

Signed-off-by: 孙林耀 <sunlinyao@vip.qq.com>
Signed-off-by: 孙林耀 <sunlinyao@vip.qq.com>
@MicroOps-cn MicroOps-cn force-pushed the master branch 2 times, most recently from f3e3637 to 3e9f2a1 Compare May 4, 2023 04:50
Signed-off-by: 孙林耀 <sunlinyao@vip.qq.com>
@MicroOps-cn
Copy link

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.

@simonpasquier
Copy link
Member

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.

@TheMeier
Copy link
Contributor

TheMeier commented Nov 3, 2025

IMHO that overlaps with #2184

@123-soleil
Copy link

I applied this PR to release 0.29 before building because I needed this feature, and it works perfectly:
No build errors
No errors during operation

@SoloJacobs
Copy link
Contributor

I am closing this PR in favor of #5011 since it has not had any activity.

Kind regards

@SoloJacobs SoloJacobs closed this Feb 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.