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"
|
2024-11-06 09:09:57 +00:00
|
|
|
"log/slog"
|
2022-11-08 12:54:38 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
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 (
|
2024-03-21 11:26:46 +00:00
|
|
|
// nolint:godot
|
2022-11-08 12:54:38 +00:00
|
|
|
// 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
|
2024-11-06 09:09:57 +00:00
|
|
|
logger *slog.Logger
|
2022-11-09 14:16:00 +00:00
|
|
|
client *http.Client
|
|
|
|
retrier *notify.Retrier
|
2022-11-08 12:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Webex notifier.
|
2024-11-06 09:09:57 +00:00
|
|
|
func New(c *config.WebexConfig, t *template.Template, l *slog.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
|
2022-11-08 12:54:38 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-11-06 09:09:57 +00:00
|
|
|
n.logger.Debug("extracted group key", "key", key)
|
2022-11-08 12:54:38 +00:00
|
|
|
|
|
|
|
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 {
|
2024-11-06 09:09:57 +00:00
|
|
|
n.logger.Debug("message truncated due to exceeding maximum allowed length by webex", "truncated_message", message)
|
2022-11-08 12:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w := webhook{
|
2022-12-15 15:05:55 +00:00
|
|
|
Markdown: message,
|
2024-05-30 13:20:38 +00:00
|
|
|
RoomID: tmpl(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
|
|
|
|
}
|