Skip to content

Add notify support for Chinese User wechat#1059

Merged
stuartnelson3 merged 10 commits intoprometheus:masterfrom
whybeyoung:master
Dec 9, 2017
Merged

Add notify support for Chinese User wechat#1059
stuartnelson3 merged 10 commits intoprometheus:masterfrom
whybeyoung:master

Conversation

@whybeyoung
Copy link
Contributor

In China ,every people use wechat. Add support for wechat will make the alermanager more helpful and popular.

@whybeyoung
Copy link
Contributor Author

why failed ci ? i make test self and not problem. but ci give the reason that confused me

@josedonizetti
Copy link
Contributor

josedonizetti commented Oct 27, 2017

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

@whybeyoung
Copy link
Contributor Author

whybeyoung commented Oct 28, 2017

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?

@stuartnelson3
Copy link
Contributor

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.

@josedonizetti
Copy link
Contributor

josedonizetti commented Oct 28, 2017

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.

@whybeyoung
Copy link
Contributor Author

whybeyoung commented Oct 29, 2017

sorry, my words maybe excited.i forgot to point out taht wechat has two version,one is for communication ,and the other for enterprise
. so mine is support for qywechat which means enterprise wechat.And most people use qywechat to be alert component. Thanks @josedonizetti
Your help will be very much appreciated by each one of every qywechat users in china.

@josedonizetti
Copy link
Contributor

@berlinsaint Thanks for clarifying. Indeed Wechat Work appears to be just like Slack and Hipchat. :)
https://work.weixin.qq.com/

@whybeyoung
Copy link
Contributor Author

@josedonizetti yes. In chinese we hardly use slack or hipchat, and you know the reason.
i think the promethues project can have the more pluralism . and i will conintue to contribute or give more my opinion about the prometheus and alertmanger.Thanks for your reply.

@stuartnelson3
Copy link
Contributor

Sorry for the long delay, thanks @josedonizetti for following up on this!

@stuartnelson3
Copy link
Contributor

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")
}


Copy link
Contributor

Choose a reason for hiding this comment

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

Can you remove this extra whitespace?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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"`
Copy link
Contributor

Choose a reason for hiding this comment

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

the yaml and json values should match the struct member names:

`yaml:"wechat_api_host,omitempty" json:"wechat_api_host,omitempty"`

for example

Copy link
Contributor

@josedonizetti josedonizetti Nov 11, 2017

Choose a reason for hiding this comment

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

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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

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
Copy link
Contributor

Choose a reason for hiding this comment

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

This can just be

postmessage_url := n.conf.ApiHost + "message/send?access_token=" + wechatToken.AccessToken

Copy link
Contributor

Choose a reason for hiding this comment

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

Golang style uses camel case, so postMessageURL instead of postmessage_url

notify/impl.go Outdated
Type: "text",
Safe: "0"}
default:
apiURL = postmessage_url
Copy link
Contributor

Choose a reason for hiding this comment

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

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

The error needs to be checked before you read the response body.

}
defer resp.Body.Close()

return false, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

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
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be removed

notify/impl.go Outdated
return false, nil
}

//Wechat implements a Notfier for wechat notifications
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a space between // and the first word, so it should be // Wechat ...

notify/impl.go Outdated
}
alerts = types.Alerts(as...)
)
//get token
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks , i will correct it these days;

@whybeyoung whybeyoung closed this Dec 1, 2017
@whybeyoung whybeyoung reopened this Dec 1, 2017
@whybeyoung
Copy link
Contributor Author

@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 == "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

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"`
Copy link
Contributor

Choose a reason for hiding this comment

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

If you use camel case on the naming CorpID, you got to separate it with an underscore corp_id

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"`
Copy link
Contributor

Choose a reason for hiding this comment

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

naming, ToUser config must be to_user

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"`
Copy link
Contributor

Choose a reason for hiding this comment

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

naming, ToParty config must be to_party

config/config.go Outdated
return fmt.Errorf("no global Wechat URL set")
}
}
wcc.ApiSecret = c.Global.WeChatAPISecret
Copy link
Contributor

@josedonizetti josedonizetti Dec 2, 2017

Choose a reason for hiding this comment

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

Got be consistent, using APISecret on the global config but ApiSecret on the local one, better be APISecret both places

return err
}
if c.ApiSecret == "" {
return fmt.Errorf("missing Wechat ApiSecret in Wechat config")
Copy link
Contributor

Choose a reason for hiding this comment

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

return fmt.Errorf("missing Wechat ApiSecret in Wechat config")
}
if c.CorpID == "" {
return fmt.Errorf("missing Wechat CorpID in Wechat config")
Copy link
Contributor

Choose a reason for hiding this comment

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

add test

defer resp.Body.Close()
return n.retry(resp.StatusCode)
}
func (n *Wechat) retry(statusCode int) (bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

notify/impl.go Outdated
}
alerts = types.Alerts(as...)
)
var params = "corpid=" + n.conf.CorpID + "&corpsecret=" + n.conf.ApiSecret
Copy link
Contributor

@josedonizetti josedonizetti Dec 2, 2017

Choose a reason for hiding this comment

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

can you use url.Values here instead of concacting? see https://github.com/prometheus/alertmanager/blob/master/notify/impl.go#L1026

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks your review~

@whybeyoung
Copy link
Contributor Author

i change the bindata.go, but i dont know how make it to pass the ci check. @josedonizetti

Copy link
Contributor

@stuartnelson3 stuartnelson3 left a comment

Choose a reason for hiding this comment

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

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"
Copy link
Contributor

Choose a reason for hiding this comment

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

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>
@whybeyoung
Copy link
Contributor Author

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

@whybeyoung
Copy link
Contributor Author

@josedonizetti @stuartnelson3
is there any other things i need to do ,need i write the doc? and the above error i can't solve it thougn i execute the make assets.

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.

@stuartnelson3
Copy link
Contributor

@berlinsaint hey sorry for the delay, I'll will try tomorrow and see if I can get this to build successfully

@stuartnelson3
Copy link
Contributor

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 bindata.go:

rm template/internal/deftmpl/bindata.go && make assets

@whybeyoung
Copy link
Contributor Author

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.

@whybeyoung
Copy link
Contributor Author

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)
now is it become a ok status ?

@stuartnelson3
Copy link
Contributor

Not sure why it isn't showing up, but I checked any the changes you have build fine. Thanks for the effort!

@stuartnelson3 stuartnelson3 merged commit 6bab629 into prometheus:master Dec 9, 2017
@whybeyoung
Copy link
Contributor Author

@stuartnelson3 And shall i need to write some doc about this notifier??

@jenningsloy318
Copy link

@berlinsaint can you share the config to use wechat for notification?

@stuartnelson3
Copy link
Contributor

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

@tj13
Copy link

tj13 commented Mar 5, 2018

@berlinsaint could you share some configuration docs about wechat integration?

@opera443399
Copy link

no wechat related docs found...

@simonpasquier
Copy link
Member

There's a pending PR (prometheus/docs#977) for updating the docs.

hh pushed a commit to ii/alertmanager that referenced this pull request Oct 4, 2018
* Add node_exporter script for init.d

Signed-off-by: gentlejo <josungil@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants