Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WeChat issue #1229

Merged
merged 2 commits into from
Feb 11, 2018
Merged

Fix WeChat issue #1229

merged 2 commits into from
Feb 11, 2018

Conversation

songjiayang
Copy link
Contributor

@songjiayang songjiayang commented Feb 7, 2018

For issue #1218

Changes:

  1. change weChatCreateMessage and weChatCloseMessage to weChatMessage
  2. cache WeChat AccessToken with accessTokenAt
  3. code refactor & format.

Test config:

global:
  resolve_timeout: 5m
  wechat_api_secret: 'xxxx'

route:
  group_by: ['alertname']
  receiver: 'wechat'

receivers:
- name: 'wechat'
  wechat_configs:
  - corp_id: 'xxxx'
    to_party: '1'
    agent_id: 'xxx'

Work with this config.

@songjiayang songjiayang force-pushed the master branch 2 times, most recently from 552bce8 to 0054ed5 Compare February 7, 2018 12:23
notify/impl.go Outdated

var accessToken string
// Refresh AccessToken over 2 hours
if n.accessToken != "" && time.Now().Sub(n.accessTokenAt) < 110*time.Minute {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can change 110*time.Minute to 2*time.Hour

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 n.accessToken == "" || time.Now().Sub(n.accessTokenAt) > 2*time.Hour {
        // refresh token
        // Set n.accessToken and n.AccessTokenAt
}

// Down below, use n.accessToken directly

notify/impl.go Outdated

u.RawQuery = parameters.Encode()

level.Debug(n.logger).Log("msg", "Sending Wechat message", "incident", key, "url", u.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

s/Sending/sending/
There is an extra space between Wechat and message that can be removed.

notify/impl.go Outdated

level.Debug(n.logger).Log("msg", "Sending Wechat message", "incident", key, "url", u.String())

resp, err := ctxhttp.Get(ctx, http.DefaultClient, u.String())
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 replace this with

res, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
        return true, err
}

resp, err := http.DefaulClient.Do(req.WithContext(ctx))

I want to start removing the ctxhttp library :)

"io/ioutil"
"net/url"
Copy link
Contributor

Choose a reason for hiding this comment

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

Both "io/ioutil" and "net/url" should be in the first block of imports in this list, with the other stdlib imports.

@stuartnelson3
Copy link
Contributor

Thanks for the contribution! Few easy changes and then this should be good to be merged

@songjiayang
Copy link
Contributor Author

songjiayang commented Feb 8, 2018

demo to show:

img_1829

@stuartnelson3 review again please.

@songjiayang songjiayang force-pushed the master branch 2 times, most recently from 5e20d85 to 0da153d Compare February 8, 2018 21:00
ToUser: `{{ template "wechat.default.to_user" . }}`,
ToParty: `{{ template "wechat.default.to_party" . }}`,
ToTag: `{{ template "wechat.default.to_tag" . }}`,
AgentID: `{{ template "wechat.default.agent_id" . }}`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why were all these fields 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.

I think these fields should be a determined value.

AgentID: `{{ template "wechat.default.agent_id" . }}`,
// TODO: Add a details field with all the alerts.
Message: `{{ template "wechat.default.message" . }}`,
APIURL: DefaultGlobalConfig.WeChatAPIURL,
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to leave this as a templated value for users. There is the check in https://github.com/prometheus/alertmanager/pull/1229/files#diff-3baf47c64847a8fb8aaa8cc2e088513bR250 that will set the api url to the global default.

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 check it again.

@songjiayang
Copy link
Contributor Author

fixed

@stuartnelson3 stuartnelson3 merged commit d07a072 into prometheus:master Feb 11, 2018
hh pushed a commit to ii/alertmanager that referenced this pull request Feb 8, 2019
…theus#1229)

Adds a new label called "type" systemd_unit_state which contains the
Type field from the unit file.  This applies only to the .service and
.mount unit types.  The other unit types do not include the optional
type field.

Fixes prometheus#1210

Signed-off-by: Paul Gier <[email protected]>
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.

2 participants