2019-06-18 13:34:46 +00:00
|
|
|
// Copyright 2019 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 webhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-10-19 13:07:38 +00:00
|
|
|
"fmt"
|
2024-11-06 09:09:57 +00:00
|
|
|
"log/slog"
|
2019-06-18 13:34:46 +00:00
|
|
|
"net/http"
|
2023-03-03 14:31:14 +00:00
|
|
|
"os"
|
2023-05-16 11:27:22 +00:00
|
|
|
"strings"
|
2019-06-18 13:34:46 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Notifier implements a Notifier for generic webhooks.
|
|
|
|
type Notifier struct {
|
2019-08-02 14:17:40 +00:00
|
|
|
conf *config.WebhookConfig
|
|
|
|
tmpl *template.Template
|
2024-11-06 09:09:57 +00:00
|
|
|
logger *slog.Logger
|
2019-08-02 14:17:40 +00:00
|
|
|
client *http.Client
|
|
|
|
retrier *notify.Retrier
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Webhook.
|
2024-11-06 09:09:57 +00:00
|
|
|
func New(conf *config.WebhookConfig, t *template.Template, l *slog.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
|
2021-11-10 16:28:47 +00:00
|
|
|
client, err := commoncfg.NewClientFromConfig(*conf.HTTPConfig, "webhook", httpOpts...)
|
2019-06-18 13:34:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Notifier{
|
|
|
|
conf: conf,
|
|
|
|
tmpl: t,
|
|
|
|
logger: l,
|
|
|
|
client: client,
|
2019-08-02 14:17:40 +00:00
|
|
|
// Webhooks are assumed to respond with 2xx response codes on a successful
|
|
|
|
// request and 5xx response codes are assumed to be recoverable.
|
2024-04-12 09:28:59 +00:00
|
|
|
retrier: ¬ify.Retrier{},
|
2019-06-18 13:34:46 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message defines the JSON object send to webhook endpoints.
|
|
|
|
type Message struct {
|
|
|
|
*template.Data
|
|
|
|
|
|
|
|
// The protocol version.
|
2020-06-04 08:07:33 +00:00
|
|
|
Version string `json:"version"`
|
|
|
|
GroupKey string `json:"groupKey"`
|
|
|
|
TruncatedAlerts uint64 `json:"truncatedAlerts"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func truncateAlerts(maxAlerts uint64, alerts []*types.Alert) ([]*types.Alert, uint64) {
|
|
|
|
if maxAlerts != 0 && uint64(len(alerts)) > maxAlerts {
|
|
|
|
return alerts[:maxAlerts], uint64(len(alerts)) - maxAlerts
|
|
|
|
}
|
|
|
|
|
|
|
|
return alerts, 0
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify implements the Notifier interface.
|
|
|
|
func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, error) {
|
2020-06-04 08:07:33 +00:00
|
|
|
alerts, numTruncated := truncateAlerts(n.conf.MaxAlerts, alerts)
|
2019-06-18 13:34:46 +00:00
|
|
|
data := notify.GetTemplateData(ctx, n.tmpl, alerts, n.logger)
|
|
|
|
|
|
|
|
groupKey, err := notify.ExtractGroupKey(ctx)
|
|
|
|
if err != nil {
|
2024-11-06 09:09:57 +00:00
|
|
|
// @tjhop: should we `return false, err` here as we do in most
|
|
|
|
// other Notify() implementations?
|
|
|
|
n.logger.Error("error extracting group key", "err", err)
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 09:09:57 +00:00
|
|
|
// @tjhop: should we debug log the key here like most other Notify() implementations?
|
|
|
|
|
2019-06-18 13:34:46 +00:00
|
|
|
msg := &Message{
|
2020-06-04 08:07:33 +00:00
|
|
|
Version: "4",
|
|
|
|
Data: data,
|
|
|
|
GroupKey: groupKey.String(),
|
|
|
|
TruncatedAlerts: numTruncated,
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(msg); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-03-03 14:31:14 +00:00
|
|
|
var url string
|
|
|
|
if n.conf.URL != nil {
|
|
|
|
url = n.conf.URL.String()
|
|
|
|
} else {
|
|
|
|
content, err := os.ReadFile(n.conf.URLFile)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("read url_file: %w", err)
|
|
|
|
}
|
2023-05-16 11:27:22 +00:00
|
|
|
url = strings.TrimSpace(string(content))
|
2023-03-03 14:31:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := notify.PostJSON(ctx, n.client, url, &buf)
|
2019-06-18 13:34:46 +00:00
|
|
|
if err != nil {
|
2023-02-02 16:23:51 +00:00
|
|
|
return true, notify.RedactURL(err)
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
2022-10-19 13:07:38 +00:00
|
|
|
defer notify.Drain(resp)
|
2019-06-18 13:34:46 +00:00
|
|
|
|
2023-04-07 15:05:17 +00:00
|
|
|
shouldRetry, err := n.retrier.Check(resp.StatusCode, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return shouldRetry, notify.NewErrorWithReason(notify.GetFailureReasonFromStatusCode(resp.StatusCode), err)
|
|
|
|
}
|
|
|
|
return shouldRetry, err
|
2022-10-19 13:07:38 +00:00
|
|
|
}
|