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 victorops
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-10-25 13:40:17 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2019-06-18 13:34:46 +00:00
|
|
|
|
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/common/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/config"
|
|
|
|
"github.com/prometheus/alertmanager/notify"
|
|
|
|
"github.com/prometheus/alertmanager/template"
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
)
|
|
|
|
|
2022-11-25 19:47:20 +00:00
|
|
|
// https://help.victorops.com/knowledge-base/incident-fields-glossary/ - 20480 characters.
|
|
|
|
const maxMessageLenRunes = 20480
|
|
|
|
|
2019-06-18 13:34:46 +00:00
|
|
|
// Notifier implements a Notifier for VictorOps notifications.
|
|
|
|
type Notifier struct {
|
2019-08-02 14:17:40 +00:00
|
|
|
conf *config.VictorOpsConfig
|
|
|
|
tmpl *template.Template
|
|
|
|
logger log.Logger
|
|
|
|
client *http.Client
|
|
|
|
retrier *notify.Retrier
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new VictorOps notifier.
|
2021-04-16 09:30:31 +00:00
|
|
|
func New(c *config.VictorOpsConfig, t *template.Template, l log.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
|
2021-11-10 16:28:47 +00:00
|
|
|
client, err := commoncfg.NewClientFromConfig(*c.HTTPConfig, "victorops", httpOpts...)
|
2019-06-18 13:34:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Notifier{
|
|
|
|
conf: c,
|
|
|
|
tmpl: t,
|
|
|
|
logger: l,
|
|
|
|
client: client,
|
2019-08-02 14:17:40 +00:00
|
|
|
// Missing documentation therefore assuming only 5xx response codes are
|
|
|
|
// recoverable.
|
|
|
|
retrier: ¬ify.Retrier{},
|
2019-06-18 13:34:46 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
victorOpsEventTrigger = "CRITICAL"
|
|
|
|
victorOpsEventResolve = "RECOVERY"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Notify implements the Notifier interface.
|
|
|
|
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
|
|
|
|
var err error
|
|
|
|
var (
|
|
|
|
data = notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
|
|
|
|
tmpl = notify.TmplText(n.tmpl, data, &err)
|
|
|
|
apiURL = n.conf.APIURL.Copy()
|
|
|
|
)
|
2022-10-25 13:40:17 +00:00
|
|
|
|
|
|
|
var apiKey string
|
|
|
|
if n.conf.APIKey != "" {
|
|
|
|
apiKey = string(n.conf.APIKey)
|
|
|
|
} else {
|
|
|
|
content, fileErr := os.ReadFile(n.conf.APIKeyFile)
|
|
|
|
if fileErr != nil {
|
2023-11-24 21:17:35 +00:00
|
|
|
return false, fmt.Errorf("failed to read API key from file: %w", fileErr)
|
2022-10-25 13:40:17 +00:00
|
|
|
}
|
|
|
|
apiKey = strings.TrimSpace(string(content))
|
|
|
|
}
|
|
|
|
|
|
|
|
apiURL.Path += fmt.Sprintf("%s/%s", apiKey, tmpl(n.conf.RoutingKey))
|
2021-01-29 13:40:33 +00:00
|
|
|
if err != nil {
|
2023-11-24 21:17:35 +00:00
|
|
|
return false, fmt.Errorf("templating error: %w", err)
|
2021-01-29 13:40:33 +00:00
|
|
|
}
|
2019-06-18 13:34:46 +00:00
|
|
|
|
|
|
|
buf, err := n.createVictorOpsPayload(ctx, as...)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := notify.PostJSON(ctx, n.client, apiURL.String(), buf)
|
|
|
|
if err != nil {
|
|
|
|
return true, notify.RedactURL(err)
|
|
|
|
}
|
|
|
|
defer notify.Drain(resp)
|
|
|
|
|
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
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the JSON payload to be sent to the VictorOps API.
|
|
|
|
func (n *Notifier) createVictorOpsPayload(ctx context.Context, as ...*types.Alert) (*bytes.Buffer, error) {
|
|
|
|
victorOpsAllowedEvents := map[string]bool{
|
|
|
|
"INFO": true,
|
|
|
|
"WARNING": true,
|
|
|
|
"CRITICAL": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := notify.ExtractGroupKey(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
alerts = types.Alerts(as...)
|
|
|
|
data = notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
|
|
|
|
tmpl = notify.TmplText(n.tmpl, data, &err)
|
|
|
|
|
|
|
|
messageType = tmpl(n.conf.MessageType)
|
|
|
|
stateMessage = tmpl(n.conf.StateMessage)
|
|
|
|
)
|
|
|
|
|
|
|
|
if alerts.Status() == model.AlertFiring && !victorOpsAllowedEvents[messageType] {
|
|
|
|
messageType = victorOpsEventTrigger
|
|
|
|
}
|
|
|
|
|
|
|
|
if alerts.Status() == model.AlertResolved {
|
|
|
|
messageType = victorOpsEventResolve
|
|
|
|
}
|
|
|
|
|
2022-11-25 19:47:20 +00:00
|
|
|
stateMessage, truncated := notify.TruncateInRunes(stateMessage, maxMessageLenRunes)
|
2019-06-18 13:34:46 +00:00
|
|
|
if truncated {
|
2022-12-13 23:26:14 +00:00
|
|
|
level.Warn(n.logger).Log("msg", "Truncated state_message", "incident", key, "max_runes", maxMessageLenRunes)
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := map[string]string{
|
|
|
|
"message_type": messageType,
|
|
|
|
"entity_id": key.Hash(),
|
|
|
|
"entity_display_name": tmpl(n.conf.EntityDisplayName),
|
|
|
|
"state_message": stateMessage,
|
|
|
|
"monitoring_tool": tmpl(n.conf.MonitoringTool),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2023-11-24 21:17:35 +00:00
|
|
|
return nil, fmt.Errorf("templating error: %w", err)
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add custom fields to the payload.
|
|
|
|
for k, v := range n.conf.CustomFields {
|
|
|
|
msg[k] = tmpl(v)
|
|
|
|
if err != nil {
|
2023-11-24 21:17:35 +00:00
|
|
|
return nil, fmt.Errorf("templating error: %w", err)
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(msg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &buf, nil
|
|
|
|
}
|