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 opsgenie
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2021-07-30 08:11:43 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2019-07-23 07:49:15 +00:00
|
|
|
"github.com/pkg/errors"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Notifier implements a Notifier for OpsGenie notifications.
|
|
|
|
type Notifier struct {
|
2019-08-02 14:17:40 +00:00
|
|
|
conf *config.OpsGenieConfig
|
|
|
|
tmpl *template.Template
|
|
|
|
logger log.Logger
|
|
|
|
client *http.Client
|
|
|
|
retrier *notify.Retrier
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new OpsGenie notifier.
|
2021-04-16 09:30:31 +00:00
|
|
|
func New(c *config.OpsGenieConfig, t *template.Template, l log.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
|
2021-04-21 10:01:34 +00:00
|
|
|
client, err := commoncfg.NewClientFromConfig(*c.HTTPConfig, "opsgenie", append(httpOpts, commoncfg.WithHTTP2Disabled())...)
|
2019-06-18 13:34:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-02 14:17:40 +00:00
|
|
|
return &Notifier{
|
|
|
|
conf: c,
|
|
|
|
tmpl: t,
|
|
|
|
logger: l,
|
|
|
|
client: client,
|
|
|
|
retrier: ¬ify.Retrier{RetryCodes: []int{http.StatusTooManyRequests}},
|
|
|
|
}, nil
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type opsGenieCreateMessage struct {
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
Details map[string]string `json:"details"`
|
|
|
|
Source string `json:"source"`
|
|
|
|
Responders []opsGenieCreateMessageResponder `json:"responders,omitempty"`
|
|
|
|
Tags []string `json:"tags,omitempty"`
|
|
|
|
Note string `json:"note,omitempty"`
|
|
|
|
Priority string `json:"priority,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type opsGenieCreateMessageResponder struct {
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
Type string `json:"type"` // team, user, escalation, schedule etc.
|
|
|
|
}
|
|
|
|
|
|
|
|
type opsGenieCloseMessage struct {
|
|
|
|
Source string `json:"source"`
|
|
|
|
}
|
|
|
|
|
2021-03-19 20:13:50 +00:00
|
|
|
type opsGenieUpdateMessageMessage struct {
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type opsGenieUpdateDescriptionMessage struct {
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-06-18 13:34:46 +00:00
|
|
|
// Notify implements the Notifier interface.
|
|
|
|
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
|
2021-03-19 20:13:50 +00:00
|
|
|
requests, retry, err := n.createRequests(ctx, as...)
|
2019-06-18 13:34:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return retry, err
|
|
|
|
}
|
|
|
|
|
2021-03-19 20:13:50 +00:00
|
|
|
for _, req := range requests {
|
|
|
|
resp, err := n.client.Do(req)
|
2021-03-19 22:23:29 +00:00
|
|
|
notify.Drain(resp)
|
2021-03-19 20:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
2021-03-19 22:23:29 +00:00
|
|
|
shouldRetry, err := n.retrier.Check(resp.StatusCode, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return shouldRetry, err
|
2021-03-19 20:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Like Split but filter out empty strings.
|
|
|
|
func safeSplit(s string, sep string) []string {
|
|
|
|
a := strings.Split(strings.TrimSpace(s), sep)
|
|
|
|
b := a[:0]
|
|
|
|
for _, x := range a {
|
|
|
|
if x != "" {
|
|
|
|
b = append(b, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create requests for a list of alerts.
|
2021-03-19 20:13:50 +00:00
|
|
|
func (n *Notifier) createRequests(ctx context.Context, as ...*types.Alert) ([]*http.Request, bool, error) {
|
2019-06-18 13:34:46 +00:00
|
|
|
key, err := notify.ExtractGroupKey(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
|
|
|
|
|
2021-08-04 11:53:44 +00:00
|
|
|
level.Debug(n.logger).Log("alert", key)
|
2019-06-18 13:34:46 +00:00
|
|
|
|
|
|
|
tmpl := notify.TmplText(n.tmpl, data, &err)
|
|
|
|
|
2020-06-09 05:38:38 +00:00
|
|
|
details := make(map[string]string)
|
2020-06-04 07:09:12 +00:00
|
|
|
|
2020-06-09 11:51:46 +00:00
|
|
|
for k, v := range data.CommonLabels {
|
|
|
|
details[k] = v
|
2020-06-09 05:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range n.conf.Details {
|
|
|
|
details[k] = tmpl(v)
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 20:13:50 +00:00
|
|
|
requests := []*http.Request{}
|
|
|
|
|
2019-06-18 13:34:46 +00:00
|
|
|
var (
|
|
|
|
alias = key.Hash()
|
|
|
|
alerts = types.Alerts(as...)
|
|
|
|
)
|
|
|
|
switch alerts.Status() {
|
|
|
|
case model.AlertResolved:
|
2021-03-19 20:13:50 +00:00
|
|
|
resolvedEndpointURL := n.conf.APIURL.Copy()
|
|
|
|
resolvedEndpointURL.Path += fmt.Sprintf("v2/alerts/%s/close", alias)
|
|
|
|
q := resolvedEndpointURL.Query()
|
2019-06-18 13:34:46 +00:00
|
|
|
q.Set("identifierType", "alias")
|
2021-03-19 20:13:50 +00:00
|
|
|
resolvedEndpointURL.RawQuery = q.Encode()
|
|
|
|
var msg = &opsGenieCloseMessage{Source: tmpl(n.conf.Source)}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(msg); err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", resolvedEndpointURL.String(), &buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
requests = append(requests, req.WithContext(ctx))
|
2019-06-18 13:34:46 +00:00
|
|
|
default:
|
|
|
|
message, truncated := notify.Truncate(tmpl(n.conf.Message), 130)
|
|
|
|
if truncated {
|
2021-08-04 11:53:44 +00:00
|
|
|
level.Debug(n.logger).Log("msg", "truncated message", "truncated_message", message, "alert", key)
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 20:13:50 +00:00
|
|
|
createEndpointURL := n.conf.APIURL.Copy()
|
|
|
|
createEndpointURL.Path += "v2/alerts"
|
2019-06-18 13:34:46 +00:00
|
|
|
|
|
|
|
var responders []opsGenieCreateMessageResponder
|
|
|
|
for _, r := range n.conf.Responders {
|
|
|
|
responder := opsGenieCreateMessageResponder{
|
|
|
|
ID: tmpl(r.ID),
|
|
|
|
Name: tmpl(r.Name),
|
|
|
|
Username: tmpl(r.Username),
|
|
|
|
Type: tmpl(r.Type),
|
|
|
|
}
|
|
|
|
|
|
|
|
if responder == (opsGenieCreateMessageResponder{}) {
|
|
|
|
// Filter out empty responders. This is useful if you want to fill
|
|
|
|
// responders dynamically from alert's common labels.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
responders = append(responders, responder)
|
|
|
|
}
|
|
|
|
|
2021-03-19 20:13:50 +00:00
|
|
|
var msg = &opsGenieCreateMessage{
|
2019-06-18 13:34:46 +00:00
|
|
|
Alias: alias,
|
|
|
|
Message: message,
|
|
|
|
Description: tmpl(n.conf.Description),
|
|
|
|
Details: details,
|
|
|
|
Source: tmpl(n.conf.Source),
|
|
|
|
Responders: responders,
|
|
|
|
Tags: safeSplit(string(tmpl(n.conf.Tags)), ","),
|
|
|
|
Note: tmpl(n.conf.Note),
|
|
|
|
Priority: tmpl(n.conf.Priority),
|
|
|
|
}
|
2021-03-19 20:13:50 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(msg); err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", createEndpointURL.String(), &buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
requests = append(requests, req.WithContext(ctx))
|
|
|
|
|
|
|
|
if n.conf.UpdateMessage {
|
|
|
|
updateMessageEndpointUrl := n.conf.APIURL.Copy()
|
|
|
|
updateMessageEndpointUrl.Path += fmt.Sprintf("v2/alerts/%s/message", alias)
|
|
|
|
q := updateMessageEndpointUrl.Query()
|
|
|
|
q.Set("identifierType", "alias")
|
|
|
|
updateMessageEndpointUrl.RawQuery = q.Encode()
|
|
|
|
updateMsgMsg := &opsGenieUpdateMessageMessage{
|
|
|
|
Message: msg.Message,
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(updateMsgMsg); err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("PUT", updateMessageEndpointUrl.String(), &buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
requests = append(requests, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.conf.UpdateDescription {
|
|
|
|
updateDescriptionEndpointURL := n.conf.APIURL.Copy()
|
|
|
|
updateDescriptionEndpointURL.Path += fmt.Sprintf("v2/alerts/%s/description", alias)
|
|
|
|
q := updateDescriptionEndpointURL.Query()
|
|
|
|
q.Set("identifierType", "alias")
|
|
|
|
updateDescriptionEndpointURL.RawQuery = q.Encode()
|
|
|
|
updateDescMsg := &opsGenieUpdateDescriptionMessage{
|
|
|
|
Description: msg.Description,
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(updateDescMsg); err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("PUT", updateDescriptionEndpointURL.String(), &buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
requests = append(requests, req.WithContext(ctx))
|
|
|
|
}
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
apiKey := tmpl(string(n.conf.APIKey))
|
|
|
|
|
|
|
|
if err != nil {
|
2019-07-23 07:49:15 +00:00
|
|
|
return nil, false, errors.Wrap(err, "templating error")
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 20:13:50 +00:00
|
|
|
for _, req := range requests {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("GenieKey %s", apiKey))
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 20:13:50 +00:00
|
|
|
return requests, true, nil
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|