Switch to model.Alert

This commit is contained in:
Fabian Reinartz 2015-10-01 14:53:49 +02:00
parent ad1408e8b9
commit 0b4d58fbdb
5 changed files with 86 additions and 60 deletions

View File

@ -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 {

View File

@ -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
}

View File

@ -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

47
vendor/github.com/prometheus/common/model/alert.go generated vendored Normal file
View File

@ -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())
}

4
vendor/vendor.json vendored
View File

@ -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",