Skip to content

Commit

Permalink
update name
Browse files Browse the repository at this point in the history
  • Loading branch information
zhan9san committed May 9, 2023
1 parent 2fa0d71 commit bf3b5ef
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 86 deletions.
6 changes: 3 additions & 3 deletions cmd/alertmanager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ import (
"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/notify/discord"
"github.com/prometheus/alertmanager/notify/email"
"github.com/prometheus/alertmanager/notify/msteams"
"github.com/prometheus/alertmanager/notify/opsgenie"
"github.com/prometheus/alertmanager/notify/pagerduty"
"github.com/prometheus/alertmanager/notify/pushover"
"github.com/prometheus/alertmanager/notify/slack"
"github.com/prometheus/alertmanager/notify/sns"
"github.com/prometheus/alertmanager/notify/teams"
"github.com/prometheus/alertmanager/notify/telegram"
"github.com/prometheus/alertmanager/notify/victorops"
"github.com/prometheus/alertmanager/notify/webex"
Expand Down Expand Up @@ -182,8 +182,8 @@ func buildReceiverIntegrations(nc config.Receiver, tmpl *template.Template, logg
for i, c := range nc.WebexConfigs {
add("webex", i, c, func(l log.Logger) (notify.Notifier, error) { return webex.New(c, tmpl, l) })
}
for i, c := range nc.TeamsConfigs {
add("teams", i, c, func(l log.Logger) (notify.Notifier, error) { return teams.New(c, tmpl, l) })
for i, c := range nc.MSTeamsConfigs {
add("msteams", i, c, func(l log.Logger) (notify.Notifier, error) { return msteams.New(c, tmpl, l) })
}

if errs.Len() > 0 {
Expand Down
14 changes: 7 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func resolveFilepaths(baseDir string, cfg *Config) {
for _, cfg := range receiver.WebexConfigs {
cfg.HTTPConfig.SetDirectory(baseDir)
}
for _, cfg := range receiver.TeamsConfigs {
for _, cfg := range receiver.MSTeamsConfigs {
cfg.HTTPConfig.SetDirectory(baseDir)
}
}
Expand Down Expand Up @@ -531,12 +531,12 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
webex.APIURL = c.Global.WebexAPIURL
}
}
for _, tc := range rcv.TeamsConfigs {
if tc.HTTPConfig == nil {
tc.HTTPConfig = c.Global.HTTPConfig
for _, msteams := range rcv.MSTeamsConfigs {
if msteams.HTTPConfig == nil {
msteams.HTTPConfig = c.Global.HTTPConfig
}
if tc.WebhookURL == nil {
return fmt.Errorf("no teams webhook URL provided")
if msteams.WebhookURL == nil {
return fmt.Errorf("no msteams webhook URL provided")
}
}

Expand Down Expand Up @@ -907,7 +907,7 @@ type Receiver struct {
SNSConfigs []*SNSConfig `yaml:"sns_configs,omitempty" json:"sns_configs,omitempty"`
TelegramConfigs []*TelegramConfig `yaml:"telegram_configs,omitempty" json:"telegram_configs,omitempty"`
WebexConfigs []*WebexConfig `yaml:"webex_configs,omitempty" json:"webex_configs,omitempty"`
TeamsConfigs []*TeamsConfig `yaml:"teams_configs,omitempty" json:"teams_configs,omitempty"`
MSTeamsConfigs []*MSTeamsConfig `yaml:"msteams_configs,omitempty" json:"teams_configs,omitempty"`
}

// UnmarshalYAML implements the yaml.Unmarshaler interface for Receiver.
Expand Down
14 changes: 7 additions & 7 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ var (
ParseMode: "HTML",
}

DefaultTeamsConfig = TeamsConfig{
DefaultMSTeamsConfig = MSTeamsConfig{
NotifierConfig: NotifierConfig{
VSendResolved: true,
},
Title: `{{ template "teams.default.title" . }}`,
Text: `{{ template "teams.default.text" . }}`,
Title: `{{ template "msteams.default.title" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
}
)

Expand Down Expand Up @@ -787,7 +787,7 @@ func (c *TelegramConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
return nil
}

type TeamsConfig struct {
type MSTeamsConfig struct {
NotifierConfig `yaml:",inline" json:",inline"`
HTTPConfig *commoncfg.HTTPClientConfig `yaml:"http_config,omitempty" json:"http_config,omitempty"`
WebhookURL *SecretURL `yaml:"webhook_url,omitempty" json:"webhook_url,omitempty"`
Expand All @@ -796,8 +796,8 @@ type TeamsConfig struct {
Text string `yaml:"text,omitempty" json:"text,omitempty"`
}

func (c *TeamsConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultTeamsConfig
type plain TeamsConfig
func (c *MSTeamsConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultMSTeamsConfig
type plain MSTeamsConfig
return unmarshal((*plain)(c))
}
46 changes: 23 additions & 23 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ discord_configs:
[ - <discord_config>, ... ]
email_configs:
[ - <email_config>, ... ]
msteams_configs:
[ - <msteams_config>, ... ]
opsgenie_configs:
[ - <opsgenie_config>, ... ]
pagerduty_configs:
Expand All @@ -509,8 +511,6 @@ slack_configs:
[ - <slack_config>, ... ]
sns_configs:
[ - <sns_config>, ... ]
teams_configs:
[ - <teams_config>, ... ]
telegram_configs:
[ - <telegram_config>, ... ]
victorops_configs:
Expand Down Expand Up @@ -719,6 +719,27 @@ tls_config:
[ headers: { <string>: <tmpl_string>, ... } ]
```

### `<msteams_config>`

Microsoft Teams notifications are sent via the [Incoming Webhooks](https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/what-are-webhooks-and-connectors) API endpoint.

```yaml
# Whether to notify about resolved alerts.
[ send_resolved: <boolean> | default = true ]

# The Incoming webhook URL.
[ webhook_url: <secret> ]

# Message title template.
[ title: <tmpl_string> | default = '{{ template "teams.default.title" . }}' ]

# Message body template.
[ text: <tmpl_string> | default = '{{ template "teams.default.text" . }}' ]

# The HTTP client's configuration.
[ http_config: <http_config> | default = global.http_config ]
```

### `<opsgenie_config>`

OpsGenie notifications are sent via the [OpsGenie API](https://docs.opsgenie.com/docs/alert-api).
Expand Down Expand Up @@ -1070,27 +1091,6 @@ attributes:
[ role_arn: <string> ]
```

### `<teams_config>`

Teams notifications are sent via the [Incoming Webhooks](https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/what-are-webhooks-and-connectors).

```yaml
# Whether to notify about resolved alerts.
[ send_resolved: <boolean> | default = true ]

# The Incoming webhook URL.
[ webhook_url: <secret> ]

# Message title template.
[ title: <tmpl_string> | default = '{{ template "teams.default.title" . }}' ]

# Message body template.
[ text: <tmpl_string> | default = '{{ template "teams.default.text" . }}' ]

# The HTTP client's configuration.
[ http_config: <http_config> | default = global.http_config ]
```

### `<telegram_config>`

```yaml
Expand Down
38 changes: 22 additions & 16 deletions notify/teams/teams.go → notify/msteams/msteams.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019 Prometheus Team
// Copyright 2023 Prometheus Team
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -11,12 +11,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package teams
package msteams

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/go-kit/log"
Expand All @@ -37,15 +39,16 @@ const (
)

type Notifier struct {
conf *config.TeamsConfig
conf *config.MSTeamsConfig
tmpl *template.Template
logger log.Logger
client *http.Client
retrier *notify.Retrier
webhookURL *config.SecretURL
postJSONFunc func(ctx context.Context, client *http.Client, url string, body io.Reader) (*http.Response, error)
}

// Message card https://learn.microsoft.com/en-us/outlook/actionable-messages/message-card-reference
// Message card reference can be found at https://learn.microsoft.com/en-us/outlook/actionable-messages/message-card-reference.
type teamsMessage struct {
Context string `json:"@context"`
Type string `json:"type"`
Expand All @@ -54,8 +57,9 @@ type teamsMessage struct {
ThemeColor string `json:"themeColor"`
}

func New(c *config.TeamsConfig, t *template.Template, l log.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
client, err := commoncfg.NewClientFromConfig(*c.HTTPConfig, "teams", httpOpts...)
// New returns a new notifier that uses the Microsoft Teams Webhook API.
func New(c *config.MSTeamsConfig, t *template.Template, l log.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
client, err := commoncfg.NewClientFromConfig(*c.HTTPConfig, "msteams", httpOpts...)
if err != nil {
return nil, err
}
Expand All @@ -67,6 +71,7 @@ func New(c *config.TeamsConfig, t *template.Template, l log.Logger, httpOpts ...
client: client,
retrier: &notify.Retrier{},
webhookURL: c.WebhookURL,
postJSONFunc: notify.PostJSON,
}

return n, nil
Expand All @@ -80,7 +85,6 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)

level.Debug(n.logger).Log("incident", key)

alerts := types.Alerts(as...)
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
tmpl := notify.TmplText(n.tmpl, data, &err)
if err != nil {
Expand All @@ -96,11 +100,12 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return false, err
}

alerts := types.Alerts(as...)
color := colorGrey
if alerts.Status() == model.AlertFiring {
switch alerts.Status() {
case model.AlertFiring:
color = colorRed
}
if alerts.Status() == model.AlertResolved {
case model.AlertResolved:
color = colorGreen
}

Expand All @@ -117,14 +122,15 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return false, err
}

resp, err := notify.PostJSON(ctx, n.client, n.webhookURL.String(), &payload)
resp, err := n.postJSONFunc(ctx, n.client, n.webhookURL.String(), &payload)
if err != nil {
return true, notify.RedactURL(err)
}
defer notify.Drain(resp)

shouldRetry, err := n.retrier.Check(resp.StatusCode, resp.Body)
if err != nil {
return shouldRetry, err
}
return false, nil
// https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL#rate-limiting-for-connectors
retry, err := n.retrier.Check(resp.StatusCode, resp.Body)
reasonErr := notify.NewErrorWithReason(notify.GetFailureReason(resp.StatusCode, fmt.Sprintf("%v", err.Error())), err)

return retry, reasonErr
}

0 comments on commit bf3b5ef

Please sign in to comment.