From 0b4d58fbdb3c7e99d10e2db01739a23a0bf96634 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Thu, 1 Oct 2015 14:53:49 +0200 Subject: [PATCH] Switch to model.Alert --- notify/notify_test.go | 32 +++++++------ test/mock.go | 15 +++--- types/types.go | 48 +++++-------------- .../prometheus/common/model/alert.go | 47 ++++++++++++++++++ vendor/vendor.json | 4 +- 5 files changed, 86 insertions(+), 60 deletions(-) create mode 100644 vendor/github.com/prometheus/common/model/alert.go diff --git a/notify/notify_test.go b/notify/notify_test.go index 12c196df..d813c4b0 100644 --- a/notify/notify_test.go +++ b/notify/notify_test.go @@ -45,27 +45,27 @@ func TestDedupingNotifier(t *testing.T) { now := time.Now() alerts := []*types.Alert{ - { + {Alert: model.Alert{ Labels: model.LabelSet{"alertname": "1"}, - }, - { + }}, + {Alert: model.Alert{ Labels: model.LabelSet{"alertname": "2"}, - }, - { + }}, + {Alert: model.Alert{ Labels: model.LabelSet{"alertname": "3"}, EndsAt: now.Add(-20 * time.Minute), - }, - { + }}, + {Alert: model.Alert{ Labels: model.LabelSet{"alertname": "4"}, EndsAt: now.Add(-10 * time.Minute), - }, - { + }}, + {Alert: model.Alert{ Labels: model.LabelSet{"alertname": "5"}, EndsAt: now.Add(-10 * time.Minute), - }, - { + }}, + {Alert: model.Alert{ Labels: model.LabelSet{"alertname": "6"}, - }, + }}, } var fps []model.Fingerprint @@ -247,7 +247,9 @@ func TestRoutedNotifier(t *testing.T) { var ( ctx = context.WithValue(context.Background(), NotifyName, route) alert = &types.Alert{ - Labels: model.LabelSet{"route": model.LabelValue(route)}, + Alert: model.Alert{ + Labels: model.LabelSet{"route": model.LabelValue(route)}, + }, } ) err := routed.Notify(ctx, alert) @@ -311,7 +313,9 @@ func TestMutingNotifier(t *testing.T) { var inAlerts []*types.Alert for _, lset := range in { - inAlerts = append(inAlerts, &types.Alert{Labels: lset}) + inAlerts = append(inAlerts, &types.Alert{ + Alert: model.Alert{Labels: lset}, + }) } if err := muteNotifer.Notify(nil, inAlerts...); err != nil { diff --git a/test/mock.go b/test/mock.go index d0fd1fd4..599d3237 100644 --- a/test/mock.go +++ b/test/mock.go @@ -15,7 +15,7 @@ import ( type TestAlert struct { labels model.LabelSet - annotations types.Annotations + annotations model.LabelSet startsAt, endsAt float64 } @@ -51,7 +51,7 @@ func Alert(keyval ...interface{}) *TestAlert { } a := &TestAlert{ labels: model.LabelSet{}, - annotations: types.Annotations{}, + annotations: model.LabelSet{}, } for i := 0; i < len(keyval); i += 2 { @@ -67,10 +67,11 @@ func Alert(keyval ...interface{}) *TestAlert { // nativeAlert converts the declared test alert into a full alert based // on the given paramters. func (a *TestAlert) nativeAlert(opts *AcceptanceOpts) *types.Alert { - na := &types.Alert{ - Labels: a.labels, - Annotations: a.annotations, - } + na := &types.Alert{} + + na.Labels = a.labels + na.Annotations = a.annotations + if a.startsAt > 0 { na.StartsAt = opts.expandTime(a.startsAt) } @@ -88,7 +89,7 @@ func (a *TestAlert) Annotate(keyval ...interface{}) *TestAlert { for i := 0; i < len(keyval); i += 2 { ln := model.LabelName(keyval[i].(string)) - lv := keyval[i+1].(string) + lv := model.LabelValue(keyval[i+1].(string)) a.annotations[ln] = lv } diff --git a/types/types.go b/types/types.go index e0732467..771440bd 100644 --- a/types/types.go +++ b/types/types.go @@ -17,8 +17,6 @@ type Reloadable interface { ApplyConfig(*config.Config) bool } -type Annotations map[model.LabelName]string - type AlertStatus string const ( @@ -26,34 +24,25 @@ const ( AlertResolved AlertStatus = "resolved" ) +// Alert wraps a model.Alert with additional information relevant +// to the Alertmanager. type Alert struct { - // Label value pairs for purpose of aggregation, matching, and disposition - // dispatching. This must minimally include an "alertname" label. - Labels model.LabelSet `json:"labels"` - - // Extra key/value information which does not define alert identity. - Annotations Annotations `json:"annotations"` - - StartsAt time.Time `json:"startsAt,omitempty"` - EndsAt time.Time `json:"endsAt,omitempty"` + model.Alert // The authoritative timestamp. - UpdatedAt time.Time `json:"-"` - Timeout bool `json:"-"` + UpdatedAt time.Time + Timeout bool } func (a *Alert) MarshalJSON() ([]byte, error) { - b := *a - if b.Timeout { - b.EndsAt = time.Time{} + v := a.Alert + // If the end timestamp was set as the expected value in case + // of a timeout, do not expose it. + if a.Timeout { + v.EndsAt = time.Time{} } - return json.Marshal(b) -} - -// Name returns the name of the alert. It is equivalent to the "alertname" label. -func (a *Alert) Name() string { - return string(a.Labels[model.AlertNameLabel]) + return json.Marshal(&v) } // Merges the timespan of two alerts based and overwrites annotations @@ -79,21 +68,6 @@ func (a *Alert) Fingerprint() model.Fingerprint { return a.Labels.Fingerprint() } -func (a *Alert) String() string { - s := fmt.Sprintf("%s[%s]", a.Name(), a.Fingerprint()) - if a.Resolved() { - return s + "[resolved]" - } - return s + "[active]" -} - -func (a *Alert) Resolved() bool { - if a.EndsAt.IsZero() { - return false - } - return !a.EndsAt.After(time.Now()) -} - // alertTimeline is a list of alerts sorted by their timestamp. type AlertTimeline []*Alert diff --git a/vendor/github.com/prometheus/common/model/alert.go b/vendor/github.com/prometheus/common/model/alert.go new file mode 100644 index 00000000..c159222c --- /dev/null +++ b/vendor/github.com/prometheus/common/model/alert.go @@ -0,0 +1,47 @@ +package model + +import ( + "fmt" + "time" +) + +// Alert is a generic representation of an alert in the Prometheus eco-system. +type Alert struct { + // Label value pairs for purpose of aggregation, matching, and disposition + // dispatching. This must minimally include an "alertname" label. + Labels LabelSet `json:"labels"` + + // Extra key/value information which does not define alert identity. + Annotations LabelSet `json:"annotations"` + + // The known time range for this alert. Both ends are optional. + StartsAt time.Time `json:"startsAt,omitempty"` + EndsAt time.Time `json:"endsAt,omitempty"` +} + +// Name returns the name of the alert. It is equivalent to the "alertname" label. +func (a *Alert) Name() string { + return string(a.Labels[AlertNameLabel]) +} + +// Fingerprint returns a unique hash for the alert. It is equivalent to +// the fingerprint of the alert's label set. +func (a *Alert) Fingerprint() Fingerprint { + return a.Labels.Fingerprint() +} + +func (a *Alert) String() string { + s := fmt.Sprintf("%s[%s]", a.Name(), a.Fingerprint().String()[:7]) + if a.Resolved() { + return s + "[resolved]" + } + return s + "[active]" +} + +// Resolved returns true iff the activity interval ended in the past. +func (a *Alert) Resolved() bool { + if a.EndsAt.IsZero() { + return false + } + return !a.EndsAt.After(time.Now()) +} diff --git a/vendor/vendor.json b/vendor/vendor.json index a40a1cf0..db6e1a23 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -54,8 +54,8 @@ }, { "path": "github.com/prometheus/common/model", - "revision": "210d6543ee217ec38b676a40549af1df9431d80d", - "revisionTime": "2015-09-28T11:56:39+02:00" + "revision": "f555d9fa6327ebbadf0f02985b8f41256fabef7b", + "revisionTime": "2015-10-01T14:40:36+02:00" }, { "path": "github.com/prometheus/common/route",