Configure http client for Wechat

This commit is contained in:
Simon Pasquier 2018-02-06 15:43:49 +01:00
parent 8b93f1085d
commit 955c92f1b6
3 changed files with 12 additions and 2 deletions

View File

@ -336,6 +336,8 @@ func (c *WebhookConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type WechatConfig struct {
NotifierConfig `yaml:",inline" json:",inline"`
HTTPConfig *commoncfg.HTTPClientConfig `yaml:"http_config,omitempty" json:"http_config,omitempty"`
APISecret string `yaml:"api_secret,omitempty" json:"api_secret,omitempty"`
CorpID string `yaml:"corp_id,omitempty" json:"corp_id,omitempty"`
Message string `yaml:"message,omitempty" json:"message,omitempty"`

View File

@ -871,6 +871,11 @@ func (n *Wechat) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
return false, err
}
c, err := commoncfg.NewHTTPClientFromConfig(n.conf.HTTPConfig)
if err != nil {
return false, err
}
// Refresh AccessToken over 2 hours
if n.accessToken == "" || time.Now().Sub(n.accessTokenAt) > 2*time.Hour {
parameters := url.Values{}
@ -895,7 +900,7 @@ func (n *Wechat) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
req.Header.Set("Content-Type", contentTypeJSON)
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
resp, err := c.Do(req.WithContext(ctx))
if err != nil {
return true, err
}
@ -939,7 +944,7 @@ func (n *Wechat) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
return true, err
}
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
resp, err := c.Do(req.WithContext(ctx))
if err != nil {
return true, err
}

View File

@ -15,6 +15,7 @@ import (
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/alertmanager/types"
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
)
@ -274,6 +275,8 @@ func TestWechat(t *testing.T) {
CorpID: "invalidCorpID",
AgentID: "1",
ToUser: "admin",
HTTPConfig: &commoncfg.HTTPClientConfig{},
}
notifier := NewWechat(conf, tmpl, logger)