|
package prometheus |
|
|
|
import ( |
|
"bytes" |
|
"encoding/json" |
|
"fmt" |
|
"io" |
|
"net/http" |
|
"strings" |
|
"time" |
|
|
|
"github.com/pkg/errors" |
|
"github.com/target/goalert/alert" |
|
"github.com/target/goalert/integrationkey" |
|
"github.com/target/goalert/permission" |
|
"github.com/target/goalert/retry" |
|
"github.com/target/goalert/util/errutil" |
|
"github.com/target/goalert/util/log" |
|
"github.com/target/goalert/validation/validate" |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type postBody struct { |
|
Status string |
|
ExternalURL string |
|
|
|
Alerts []postBodyAlert |
|
|
|
CommonLabels struct { |
|
Instance string |
|
AlertName string `json:"alertname"` |
|
} |
|
|
|
CommonAnnotations struct { |
|
Summary string |
|
Details string |
|
} |
|
} |
|
type postBodyAlert struct { |
|
Labels struct { |
|
AlertName string |
|
Instance string |
|
} |
|
Annotations struct { |
|
Summary string |
|
Details string |
|
} |
|
GeneratorURL string |
|
} |
|
|
|
func (a postBodyAlert) Summary() string { |
|
if a.Annotations.Summary != "" { |
|
return a.Annotations.Summary |
|
} |
|
|
|
return a.Labels.AlertName + " " + a.Labels.Instance |
|
} |
|
func (a postBodyAlert) gen() string { |
|
if a.GeneratorURL == "" { |
|
return "" |
|
} |
|
|
|
return fmt.Sprintf(" [View](%s)", a.GeneratorURL) |
|
} |
|
func (a postBodyAlert) Details() string { |
|
if a.Annotations.Details != "" { |
|
return a.Annotations.Details + a.gen() |
|
} |
|
|
|
return a.Summary() + a.gen() |
|
} |
|
func (b postBody) Summary() string { |
|
if b.CommonAnnotations.Summary != "" { |
|
return b.CommonAnnotations.Summary |
|
} |
|
if b.CommonLabels.AlertName == "" { |
|
|
|
return b.Alerts[0].Summary() + fmt.Sprintf(" and %d others", len(b.Alerts)-1) |
|
} |
|
|
|
|
|
if b.CommonLabels.Instance != "" { |
|
return b.CommonLabels.AlertName + " " + b.CommonLabels.Instance |
|
} |
|
|
|
var instances []string |
|
for _, a := range b.Alerts { |
|
instances = append(instances, a.Labels.Instance) |
|
} |
|
|
|
return b.CommonLabels.AlertName + " " + strings.Join(instances, ",") |
|
} |
|
|
|
func (b postBody) Details(payload string) string { |
|
var s strings.Builder |
|
if b.ExternalURL != "" { |
|
fmt.Fprintf(&s, "[Prometheus Alertmanager UI](%s)\n\n", b.ExternalURL) |
|
} |
|
if b.CommonAnnotations.Details != "" { |
|
s.WriteString(b.CommonAnnotations.Details + "\n\n") |
|
} else { |
|
for _, a := range b.Alerts { |
|
s.WriteString(a.Details() + "\n\n") |
|
} |
|
} |
|
if payload != "" { |
|
fmt.Fprintf(&s, "## Payload\n\n```json\n%s\n```\n", payload) |
|
} |
|
return s.String() |
|
} |
|
|
|
func clientError(w http.ResponseWriter, code int, err error) bool { |
|
if err == nil { |
|
return false |
|
} |
|
|
|
http.Error(w, http.StatusText(code), code) |
|
return true |
|
} |
|
|
|
func PrometheusAlertmanagerEventsAPI(aDB *alert.Store, intDB *integrationkey.Store) http.HandlerFunc { |
|
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
|
ctx := r.Context() |
|
|
|
err := permission.LimitCheckAny(ctx, permission.Service) |
|
if errutil.HTTPError(ctx, w, err) { |
|
return |
|
} |
|
serviceID := permission.ServiceID(ctx) |
|
|
|
var body postBody |
|
var buf bytes.Buffer |
|
err = json.NewDecoder(io.TeeReader(r.Body, &buf)).Decode(&body) |
|
if clientError(w, http.StatusBadRequest, err) { |
|
log.Logf(ctx, "bad request from prometheus alertmanager: %v", err) |
|
return |
|
} |
|
|
|
var status alert.Status |
|
switch body.Status { |
|
case "firing": |
|
status = alert.StatusTriggered |
|
case "resolved": |
|
status = alert.StatusClosed |
|
default: |
|
log.Logf(ctx, "bad request from prometheus alertmanager: missing or invalid state") |
|
http.Error(w, "invalid state", http.StatusBadRequest) |
|
return |
|
} |
|
|
|
data := make([]byte, buf.Len()) |
|
copy(data, buf.Bytes()) |
|
buf.Reset() |
|
err = json.Indent(&buf, data, "", " ") |
|
if err == nil { |
|
data = buf.Bytes() |
|
} |
|
|
|
summary := validate.SanitizeText(body.Summary(), alert.MaxSummaryLength) |
|
msg := &alert.Alert{ |
|
Summary: summary, |
|
Details: validate.SanitizeText(body.Details(string(data)), alert.MaxDetailsLength), |
|
Status: status, |
|
Source: alert.SourcePrometheusAlertmanager, |
|
ServiceID: serviceID, |
|
Dedup: alert.NewUserDedup(summary), |
|
} |
|
|
|
err = retry.DoTemporaryError(func(int) error { |
|
_, _, err = aDB.CreateOrUpdate(ctx, msg) |
|
return err |
|
}, |
|
retry.Log(ctx), |
|
retry.Limit(10), |
|
retry.FibBackoff(time.Second), |
|
) |
|
if errutil.HTTPError(ctx, w, errors.Wrap(err, "create or update alert for prometheus alertmanager")) { |
|
return |
|
} |
|
} |
|
} |
|
|