2022-11-08 12:54:38 +00:00
|
|
|
// Copyright 2022 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
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package webex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
|
|
|
commoncfg "github.com/prometheus/common/config"
|
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/config"
|
|
|
|
"github.com/prometheus/alertmanager/notify"
|
|
|
|
"github.com/prometheus/alertmanager/template"
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// maxMessageSize represents the maximum message length that Webex supports.
|
|
|
|
maxMessageSize = 7439
|
|
|
|
)
|
|
|
|
|
|
|
|
type Notifier struct {
|
2022-11-09 14:16:00 +00:00
|
|
|
conf *config.WebexConfig
|
|
|
|
tmpl *template.Template
|
|
|
|
logger log.Logger
|
|
|
|
client *http.Client
|
|
|
|
retrier *notify.Retrier
|
2022-11-08 12:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Webex notifier.
|
|
|
|
func New(c *config.WebexConfig, t *template.Template, l log.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
|
|
|
|
client, err := commoncfg.NewClientFromConfig(*c.HTTPConfig, "webex", httpOpts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
n := &Notifier{
|
2022-11-09 14:16:00 +00:00
|
|
|
conf: c,
|
|
|
|
tmpl: t,
|
|
|
|
logger: l,
|
|
|
|
client: client,
|
|
|
|
retrier: ¬ify.Retrier{},
|
2022-11-08 12:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type webhook struct {
|
2022-12-15 15:05:55 +00:00
|
|
|
Markdown string `json:"markdown"`
|
|
|
|
RoomID string `json:"roomId,omitempty"`
|
2022-11-08 12:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify implements the Notifier interface.
|
|
|
|
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
|
|
|
|
key, err := notify.ExtractGroupKey(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
level.Debug(n.logger).Log("incident", key)
|
|
|
|
|
|
|
|
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
|
|
|
|
tmpl := notify.TmplText(n.tmpl, data, &err)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message := tmpl(n.conf.Message)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-11-11 15:14:35 +00:00
|
|
|
message, truncated := notify.TruncateInBytes(message, maxMessageSize)
|
2022-11-08 12:54:38 +00:00
|
|
|
if truncated {
|
|
|
|
level.Debug(n.logger).Log("msg", "message truncated due to exceeding maximum allowed length by webex", "truncated_message", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
w := webhook{
|
2022-12-15 15:05:55 +00:00
|
|
|
Markdown: message,
|
|
|
|
RoomID: n.conf.RoomID,
|
2022-11-08 12:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var payload bytes.Buffer
|
|
|
|
if err = json.NewEncoder(&payload).Encode(w); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-12-15 15:51:45 +00:00
|
|
|
resp, err := notify.PostJSON(ctx, n.client, n.conf.APIURL.String(), &payload)
|
2022-11-08 12:54:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, notify.RedactURL(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldRetry, err := n.retrier.Check(resp.StatusCode, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return shouldRetry, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|