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"
|
2019-08-02 14:17:40 +00:00
|
|
|
"io"
|
2019-06-18 13:34:46 +00:00
|
|
|
"net/http"
|
|
|
|
|
2021-07-30 08:11:43 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
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
|
|
|
|
logger log.Logger
|
|
|
|
client *http.Client
|
|
|
|
retrier *notify.Retrier
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Webhook.
|
2021-04-16 09:30:31 +00:00
|
|
|
func New(conf *config.WebhookConfig, t *template.Template, l log.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
|
2021-04-21 10:01:34 +00:00
|
|
|
client, err := commoncfg.NewClientFromConfig(*conf.HTTPConfig, "webhook", append(httpOpts, commoncfg.WithHTTP2Disabled())...)
|
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.
|
|
|
|
retrier: ¬ify.Retrier{
|
|
|
|
CustomDetailsFunc: func(int, io.Reader) string {
|
|
|
|
return conf.URL.String()
|
|
|
|
},
|
|
|
|
},
|
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 {
|
|
|
|
level.Error(n.logger).Log("err", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-18 12:34:22 +00:00
|
|
|
resp, err := notify.PostJSON(ctx, n.client, n.conf.URL.String(), &buf)
|
2019-06-18 13:34:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
notify.Drain(resp)
|
|
|
|
|
2019-08-02 14:17:40 +00:00
|
|
|
return n.retrier.Check(resp.StatusCode, nil)
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|