Add notify support for Chinese User wechat#1059
Add notify support for Chinese User wechat#1059stuartnelson3 merged 10 commits intoprometheus:masterfrom whybeyoung:master
Conversation
|
why failed ci ? i make test self and not problem. but ci give the reason that confused me |
|
Alertmanager team is not actively adding new notifiers, it's recommended that you implement this custom notification via the webhook receiver. See prometheus/docs#843 |
|
Wechat In Chinese have a huge number users. Not Every users know webhook or implement the webhook notification. I strongly recommend to merge this contribute. All the notifier's in alertmanger now is never used in chinese .BTW i think alertmanger will get more users in chinese, this is good things ,right? |
|
I will take a look at this when I have a chance. Please be patient, as I've been relatively busy with non-OSS things lately. |
|
Wechat might be big, but it's a communication app just like WhatsApp, Line, and Telegram. Each of those apps is big in different places, but they are not directed to devops like most of the notifiers currently supported. |
|
sorry, my words maybe excited.i forgot to point out taht wechat has two version,one is for communication ,and the other for enterprise |
|
@berlinsaint Thanks for clarifying. Indeed Wechat Work appears to be just like Slack and Hipchat. :) |
|
@josedonizetti yes. In chinese we hardly use slack or hipchat, and you know the reason. |
|
Sorry for the long delay, thanks @josedonizetti for following up on this! |
|
My main concern was that I have no experience with or potentially way to interact with wechat, so this notifier will have to be maintained by you or someone with the ability to confirm that a request leaving alertmanager is correctly accepted by wechat. I'm happy to add this and review code, but the burden of verifying this works will be on the alertmanager users who use wechat. |
config/config.go
Outdated
| return fmt.Errorf("no global Wechat ApiSecret set") | ||
| } | ||
|
|
||
|
|
There was a problem hiding this comment.
Can you remove this extra whitespace?
There was a problem hiding this comment.
ok, i will take a look, thanks
config/config.go
Outdated
| OpsGenieAPIHost string `yaml:"opsgenie_api_host,omitempty" json:"opsgenie_api_host,omitempty"` | ||
| WeChatAPIHost string `yaml:"wechat_host,omitempty" json:"wechat_host,omitempty"` | ||
| WeChatAPISecret string `yaml:"wechat_secret,omitempty" json:"wechat_secret,omitempty"` | ||
| WeChatAPICorpId string `yaml:"wechat_corpid,omitempty" json:"wechat_corpid,omitempty"` |
There was a problem hiding this comment.
the yaml and json values should match the struct member names:
`yaml:"wechat_api_host,omitempty" json:"wechat_api_host,omitempty"`
for example
There was a problem hiding this comment.
Also rename to wechat_api_url instead of wechat_api_host
notify/impl.go
Outdated
| var wechatToken WechatToken | ||
| if resp.StatusCode/100 == 2 { | ||
| body, _ := ioutil.ReadAll(resp.Body) | ||
| if err := json.Unmarshal(body, &wechatToken); err != nil { |
There was a problem hiding this comment.
This could be changed to
if err := json.NewDecoder(resp.Body).Decode(&wechatToken); err != nil {
// etc...
}you generally don't want to ignore errors, like what is being done right now with ioutil.ReadAll.
notify/impl.go
Outdated
| } | ||
|
|
||
| access_token := wechatToken.AccessToken | ||
| postmessage_url := n.conf.ApiHost + "message/send?access_token=" + access_token |
There was a problem hiding this comment.
This can just be
postmessage_url := n.conf.ApiHost + "message/send?access_token=" + wechatToken.AccessToken
There was a problem hiding this comment.
Golang style uses camel case, so postMessageURL instead of postmessage_url
notify/impl.go
Outdated
| Type: "text", | ||
| Safe: "0"} | ||
| default: | ||
| apiURL = postmessage_url |
There was a problem hiding this comment.
You're setting apiURL = postmessage_url in both of these branches, so it can just be dropped and then use postmessage_url in resp, err = ctxhttp.Post(ctx, http.DefaultClient, apiURL, contentTypeJSON, &buf) below.
notify/impl.go
Outdated
| } | ||
|
|
||
| resp, err = ctxhttp.Post(ctx, http.DefaultClient, apiURL, contentTypeJSON, &buf) | ||
| body, _ := ioutil.ReadAll(resp.Body) |
There was a problem hiding this comment.
The error needs to be checked before you read the response body.
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| return false, nil |
There was a problem hiding this comment.
You should implement a retry method based on the wechat api documentation for when a request should be retried or not. See this example: https://github.com/prometheus/alertmanager/blob/master/notify/impl.go#L874-L884
config/config.go
Outdated
| ogc.APIHost += "/" | ||
| } | ||
| } | ||
| //ADD WECHAT BY YYB |
notify/impl.go
Outdated
| return false, nil | ||
| } | ||
|
|
||
| //Wechat implements a Notfier for wechat notifications |
There was a problem hiding this comment.
There's a space between // and the first word, so it should be // Wechat ...
notify/impl.go
Outdated
| } | ||
| alerts = types.Alerts(as...) | ||
| ) | ||
| //get token |
There was a problem hiding this comment.
Thanks , i will correct it these days;
|
@stuartnelson3 sorry for long delay, i have corrected the code. can it be merged or what other things that i should do now? |
config/config.go
Outdated
| } | ||
| for _, wcc := range rcv.WechatConfigs { | ||
| wcc.ApiURL = c.Global.WeChatAPIURL | ||
| if wcc.ApiURL == "" { |
There was a problem hiding this comment.
can u rename ApiURL to APIURL? Keep the same pattern found on other provider configs.
config/config.go
Outdated
| OpsGenieAPIURL string `yaml:"opsgenie_api_url,omitempty" json:"opsgenie_api_url,omitempty"` | ||
| WeChatAPIURL string `yaml:"wechat_api_url,omitempty" json:"wechat_api_url,omitempty"` | ||
| WeChatAPISecret string `yaml:"wechat_api_secret,omitempty" json:"wechat_api_secret,omitempty"` | ||
| WeChatAPICorpID string `yaml:"wechat_api_corpid,omitempty" json:"wechat_api_corpid,omitempty"` |
There was a problem hiding this comment.
If you use camel case on the naming CorpID, you got to separate it with an underscore corp_id
config/notifiers.go
Outdated
| CorpID string `yaml:"corp_id,omitempty" json:"corp_id,omitempty"` | ||
| Message string `yaml:"message,omitempty" json:"message,omitempty"` | ||
| ApiURL string `yaml:"api_url,omitempty" json:"api_url,omitempty"` | ||
| ToUser string `yaml:"touser,omitempty" json:"touser,omitempty"` |
There was a problem hiding this comment.
naming, ToUser config must be to_user
config/notifiers.go
Outdated
| Message string `yaml:"message,omitempty" json:"message,omitempty"` | ||
| ApiURL string `yaml:"api_url,omitempty" json:"api_url,omitempty"` | ||
| ToUser string `yaml:"touser,omitempty" json:"touser,omitempty"` | ||
| ToParty string `yaml:"toparty,omitempty" json:"toparty,omitempty"` |
There was a problem hiding this comment.
naming, ToParty config must be to_party
config/config.go
Outdated
| return fmt.Errorf("no global Wechat URL set") | ||
| } | ||
| } | ||
| wcc.ApiSecret = c.Global.WeChatAPISecret |
There was a problem hiding this comment.
Got be consistent, using APISecret on the global config but ApiSecret on the local one, better be APISecret both places
config/notifiers.go
Outdated
| return err | ||
| } | ||
| if c.ApiSecret == "" { | ||
| return fmt.Errorf("missing Wechat ApiSecret in Wechat config") |
There was a problem hiding this comment.
This can be tested. see https://github.com/prometheus/alertmanager/blob/master/config/notifiers_test.go#L147
| return fmt.Errorf("missing Wechat ApiSecret in Wechat config") | ||
| } | ||
| if c.CorpID == "" { | ||
| return fmt.Errorf("missing Wechat CorpID in Wechat config") |
| defer resp.Body.Close() | ||
| return n.retry(resp.StatusCode) | ||
| } | ||
| func (n *Wechat) retry(statusCode int) (bool, error) { |
There was a problem hiding this comment.
notify/impl.go
Outdated
| } | ||
| alerts = types.Alerts(as...) | ||
| ) | ||
| var params = "corpid=" + n.conf.CorpID + "&corpsecret=" + n.conf.ApiSecret |
There was a problem hiding this comment.
can you use url.Values here instead of concacting? see https://github.com/prometheus/alertmanager/blob/master/notify/impl.go#L1026
There was a problem hiding this comment.
thanks your review~
|
i change the bindata.go, but i dont know how make it to pass the ci check. @josedonizetti |
stuartnelson3
left a comment
There was a problem hiding this comment.
All the code in this pr needs to have gofmt run on it. You can do this with make format.
Try regenerating the assets with:
rm template/internal/deftmpl/bindata.go && make assets
notify/impl.go
Outdated
| "strings" | ||
| "time" | ||
|
|
||
| "io/ioutil" |
There was a problem hiding this comment.
there needs to be a linebreak between the stdlib packages and the third-party github packages. So, just add a newline below this.
Signed-off-by: yb_home <berlinsaint@126.com>
|
i used the windows to compile and have regenerated the bindata.go, one ci is ok, but it still not passed by the travis ci. it errors on step git-diff,(i really don't now what this step do) can you help me to sovle this? @josedonizetti |
|
@josedonizetti @stuartnelson3 this is my first pr on github, and i'm very glad and excited to see your review, you two are very nice and because of you i made a greate progress.Thanks , i will keep patience to notice the prometheus project, and i think will make more pr for you. |
|
@berlinsaint hey sorry for the delay, I'll will try tomorrow and see if I can get this to build successfully |
|
I was able to get your branch to successfully build against ci: https://github.com/prometheus/alertmanager/compare/stn/test-ci-wechat I executed the following command, and then committed the changes to |
|
i execute the command and ,i m using windows ,so i don't have make , i use the makefile's command bin-data tool, and make the bindata.go .but it looks like a little different(length is the same but some bindata not the same ) with your bindata.go. Thank you for you review, now is it ok to be merged? @stuartnelson3 i checkout the bindata.go in test-wechat-ci branch. |
|
i don't know why there is only one check , there were two check travis ci ,circle ci in the old(and always travis ci failed) |
|
Not sure why it isn't showing up, but I checked any the changes you have build fine. Thanks for the effort! |
|
@stuartnelson3 And shall i need to write some doc about this notifier?? |
|
@berlinsaint can you share the config to use wechat for notification? |
|
@berlinsaint sorry for the delay, I didn't see this message until now. Adding documentation to https://github.com/prometheus/docs/blob/b20ed77d9307f06e0e2ff169abb51bc8d3460ebc/content/docs/alerting/configuration.md would be very much appreciated! |
|
@berlinsaint could you share some configuration docs about wechat integration? |
|
no wechat related docs found... |
|
There's a pending PR (prometheus/docs#977) for updating the docs. |
* Add node_exporter script for init.d Signed-off-by: gentlejo <josungil@gmail.com>
In China ,every people use wechat. Add support for wechat will make the alermanager more helpful and popular.