115 lines
3.8 KiB
Go
115 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
json "encoding/json/v2"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/prometheus/alertmanager/notify/webhook"
|
|
|
|
"github.com/samcharles93/archipelago/sdk/connector"
|
|
connectorv1 "github.com/samcharles93/archipelago/sdk/connector/v1"
|
|
)
|
|
|
|
// eventType is the one event type this connector emits.
|
|
const eventType = "alert"
|
|
|
|
// webhookVersion is the Alertmanager webhook payload version this parses.
|
|
const webhookVersion = "4"
|
|
|
|
const alertSchema = `{
|
|
"type": "object",
|
|
"required": ["status", "fingerprint", "starts_at", "labels"],
|
|
"properties": {
|
|
"status": {"enum": ["firing", "resolved"]},
|
|
"alertname": {"type": "string"},
|
|
"fingerprint": {"type": "string"},
|
|
"labels": {"type": "object", "additionalProperties": {"type": "string"}},
|
|
"annotations": {"type": "object", "additionalProperties": {"type": "string"}},
|
|
"starts_at": {"type": "string"},
|
|
"ends_at": {"type": "string"},
|
|
"generator_url": {"type": "string"},
|
|
"receiver": {"type": "string"},
|
|
"group_key": {"type": "string"}
|
|
}
|
|
}`
|
|
|
|
// alert is the event's fields, matching alertSchema.
|
|
type alert struct {
|
|
Status string `json:"status"`
|
|
Alertname string `json:"alertname,omitempty"`
|
|
Fingerprint string `json:"fingerprint"`
|
|
Labels map[string]string `json:"labels"`
|
|
Annotations map[string]string `json:"annotations,omitempty"`
|
|
StartsAt string `json:"starts_at"`
|
|
EndsAt string `json:"ends_at,omitempty"`
|
|
GeneratorURL string `json:"generator_url,omitempty"`
|
|
Receiver string `json:"receiver,omitempty"`
|
|
GroupKey string `json:"group_key,omitempty"`
|
|
}
|
|
|
|
type alertmanager struct{}
|
|
|
|
func (alertmanager) Describe() connector.Description {
|
|
return connector.Description{Events: []*connectorv1.EventType{{
|
|
Name: eventType,
|
|
Description: "One Alertmanager alert, firing or resolved.",
|
|
Schema: []byte(alertSchema),
|
|
}}}
|
|
}
|
|
|
|
// Parse turns one webhook notification into one event per alert. A
|
|
// re-delivered notification repeats each alert's source event id, and a
|
|
// resolve carries the link key of the firing it ends.
|
|
func (alertmanager) Parse(_ context.Context, body []byte, _ map[string]string) ([]connector.Event, error) {
|
|
var msg webhook.Message
|
|
if err := json.Unmarshal(body, &msg); err != nil {
|
|
return nil, fmt.Errorf("not an Alertmanager notification: %w", err)
|
|
}
|
|
if msg.Data == nil {
|
|
return nil, errors.New("notification carries no alerts")
|
|
}
|
|
if msg.Version != webhookVersion {
|
|
return nil, fmt.Errorf("webhook version %q, want %q", msg.Version, webhookVersion)
|
|
}
|
|
events := make([]connector.Event, 0, len(msg.Alerts))
|
|
for i, a := range msg.Alerts {
|
|
if a.Fingerprint == "" || a.StartsAt.IsZero() {
|
|
return nil, fmt.Errorf("alert %d has no fingerprint or start time", i)
|
|
}
|
|
if a.Status != "firing" && a.Status != "resolved" {
|
|
return nil, fmt.Errorf("alert %d has status %q", i, a.Status)
|
|
}
|
|
start := a.StartsAt.UTC().Format(time.RFC3339Nano)
|
|
fields, err := json.Marshal(alert{
|
|
Status: a.Status, Alertname: a.Labels["alertname"], Fingerprint: a.Fingerprint,
|
|
Labels: a.Labels, Annotations: a.Annotations, StartsAt: start, EndsAt: endsAt(a.EndsAt),
|
|
GeneratorURL: a.GeneratorURL, Receiver: msg.Receiver, GroupKey: msg.GroupKey,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
occurred := a.StartsAt
|
|
if a.Status == "resolved" && !a.EndsAt.IsZero() {
|
|
occurred = a.EndsAt
|
|
}
|
|
events = append(events, connector.Event{
|
|
Type: eventType,
|
|
SourceEventID: a.Fingerprint + ":" + start + ":" + a.Status,
|
|
Fields: fields,
|
|
LinkKey: a.Fingerprint + ":" + start,
|
|
OccurredAt: occurred,
|
|
})
|
|
}
|
|
return events, nil
|
|
}
|
|
|
|
func endsAt(t time.Time) string {
|
|
if t.IsZero() {
|
|
return ""
|
|
}
|
|
return t.UTC().Format(time.RFC3339Nano)
|
|
}
|