mirror of https://github.com/grafana/grafana
AlertingNG: Correctly set StartsAt, EndsAt, UpdatedAt after alert reception (#33109)
Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>pull/32979/head
parent
e7fd30be17
commit
0a03d5c29e
@ -1,60 +0,0 @@ |
||||
package notifier |
||||
|
||||
import ( |
||||
"context" |
||||
"time" |
||||
|
||||
gokit_log "github.com/go-kit/kit/log" |
||||
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" |
||||
"github.com/prometheus/alertmanager/api/v2/models" |
||||
"github.com/prometheus/alertmanager/provider" |
||||
"github.com/prometheus/alertmanager/provider/mem" |
||||
"github.com/prometheus/alertmanager/types" |
||||
"github.com/prometheus/common/model" |
||||
) |
||||
|
||||
type AlertProvider struct { |
||||
provider.Alerts |
||||
} |
||||
|
||||
// NewAlertProvider returns AlertProvider that provides a method to translate
|
||||
// Grafana alerts to Prometheus Alertmanager alerts before passing it ahead.
|
||||
func NewAlertProvider(m types.Marker) (*AlertProvider, error) { |
||||
alerts, err := mem.NewAlerts(context.Background(), m, 30*time.Minute, gokit_log.NewNopLogger()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return &AlertProvider{Alerts: alerts}, nil |
||||
} |
||||
|
||||
func (ap *AlertProvider) PutPostableAlert(postableAlerts apimodels.PostableAlerts) error { |
||||
alerts := make([]*types.Alert, 0, len(postableAlerts.PostableAlerts)) |
||||
for _, a := range postableAlerts.PostableAlerts { |
||||
alerts = append(alerts, alertForDelivery(a)) |
||||
} |
||||
return ap.Alerts.Put(alerts...) |
||||
} |
||||
|
||||
func alertForDelivery(a models.PostableAlert) *types.Alert { |
||||
lbls := model.LabelSet{} |
||||
annotations := model.LabelSet{} |
||||
for k, v := range a.Labels { |
||||
lbls[model.LabelName(k)] = model.LabelValue(v) |
||||
} |
||||
for k, v := range a.Annotations { |
||||
annotations[model.LabelName(k)] = model.LabelValue(v) |
||||
} |
||||
|
||||
return &types.Alert{ |
||||
Alert: model.Alert{ |
||||
Labels: lbls, |
||||
Annotations: annotations, |
||||
StartsAt: time.Time(a.StartsAt), |
||||
EndsAt: time.Time(a.EndsAt), |
||||
GeneratorURL: a.GeneratorURL.String(), |
||||
}, |
||||
UpdatedAt: time.Time{}, // TODO(codesome) what should this be?
|
||||
Timeout: false, // TODO(codesome).
|
||||
} |
||||
} |
||||
@ -1,113 +0,0 @@ |
||||
package notifier |
||||
|
||||
import ( |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/go-openapi/strfmt" |
||||
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" |
||||
"github.com/prometheus/alertmanager/api/v2/models" |
||||
"github.com/prometheus/alertmanager/provider" |
||||
"github.com/prometheus/alertmanager/types" |
||||
"github.com/prometheus/client_golang/prometheus" |
||||
"github.com/prometheus/common/model" |
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
func TestAlertProvider(t *testing.T) { |
||||
marker := types.NewMarker(prometheus.DefaultRegisterer) |
||||
alertProvider := &mockAlertProvider{} |
||||
|
||||
ap, err := NewAlertProvider(marker) |
||||
require.NoError(t, err) |
||||
ap.Alerts = alertProvider |
||||
|
||||
startTime := time.Now() |
||||
endTime := startTime.Add(2 * time.Hour) |
||||
postableAlerts := apimodels.PostableAlerts{ |
||||
PostableAlerts: []models.PostableAlert{ |
||||
{ // Start and end set.
|
||||
Annotations: models.LabelSet{"msg": "Alert1 annotation"}, |
||||
Alert: models.Alert{ |
||||
Labels: models.LabelSet{"alertname": "Alert1"}, |
||||
GeneratorURL: "http://localhost/url1", |
||||
}, |
||||
StartsAt: strfmt.DateTime(startTime), |
||||
EndsAt: strfmt.DateTime(endTime), |
||||
}, { // Only end is set.
|
||||
Annotations: models.LabelSet{"msg": "Alert2 annotation"}, |
||||
Alert: models.Alert{ |
||||
Labels: models.LabelSet{"alertname": "Alert2"}, |
||||
GeneratorURL: "http://localhost/url2", |
||||
}, |
||||
StartsAt: strfmt.DateTime{}, |
||||
EndsAt: strfmt.DateTime(endTime), |
||||
}, { // Only start is set.
|
||||
Annotations: models.LabelSet{"msg": "Alert3 annotation"}, |
||||
Alert: models.Alert{ |
||||
Labels: models.LabelSet{"alertname": "Alert3"}, |
||||
GeneratorURL: "http://localhost/url3", |
||||
}, |
||||
StartsAt: strfmt.DateTime(startTime), |
||||
EndsAt: strfmt.DateTime{}, |
||||
}, { // Both start and end are not set.
|
||||
Annotations: models.LabelSet{"msg": "Alert4 annotation"}, |
||||
Alert: models.Alert{ |
||||
Labels: models.LabelSet{"alertname": "Alert4"}, |
||||
GeneratorURL: "http://localhost/url4", |
||||
}, |
||||
StartsAt: strfmt.DateTime{}, |
||||
EndsAt: strfmt.DateTime{}, |
||||
}, |
||||
}, |
||||
} |
||||
|
||||
require.NoError(t, ap.PutPostableAlert(postableAlerts)) |
||||
|
||||
// Alerts that should be sent for routing.
|
||||
expProviderAlerts := []*types.Alert{ |
||||
{ |
||||
Alert: model.Alert{ |
||||
Annotations: model.LabelSet{"msg": "Alert1 annotation"}, |
||||
Labels: model.LabelSet{"alertname": "Alert1"}, |
||||
StartsAt: startTime, |
||||
EndsAt: endTime, |
||||
GeneratorURL: "http://localhost/url1", |
||||
}, |
||||
}, { |
||||
Alert: model.Alert{ |
||||
Annotations: model.LabelSet{"msg": "Alert2 annotation"}, |
||||
Labels: model.LabelSet{"alertname": "Alert2"}, |
||||
EndsAt: endTime, |
||||
GeneratorURL: "http://localhost/url2", |
||||
}, |
||||
}, { |
||||
Alert: model.Alert{ |
||||
Annotations: model.LabelSet{"msg": "Alert3 annotation"}, |
||||
Labels: model.LabelSet{"alertname": "Alert3"}, |
||||
StartsAt: startTime, |
||||
GeneratorURL: "http://localhost/url3", |
||||
}, |
||||
}, { |
||||
Alert: model.Alert{ |
||||
Annotations: model.LabelSet{"msg": "Alert4 annotation"}, |
||||
Labels: model.LabelSet{"alertname": "Alert4"}, |
||||
GeneratorURL: "http://localhost/url4", |
||||
}, |
||||
}, |
||||
} |
||||
require.Equal(t, expProviderAlerts, alertProvider.alerts) |
||||
} |
||||
|
||||
type mockAlertProvider struct { |
||||
alerts []*types.Alert |
||||
} |
||||
|
||||
func (a *mockAlertProvider) Subscribe() provider.AlertIterator { return nil } |
||||
func (a *mockAlertProvider) GetPending() provider.AlertIterator { return nil } |
||||
func (a *mockAlertProvider) Get(model.Fingerprint) (*types.Alert, error) { return nil, nil } |
||||
|
||||
func (a *mockAlertProvider) Put(alerts ...*types.Alert) error { |
||||
a.alerts = append(a.alerts, alerts...) |
||||
return nil |
||||
} |
||||
Loading…
Reference in new issue