|
|
|
|
@ -20,10 +20,13 @@ import ( |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/go-kit/log" |
|
|
|
|
"github.com/prometheus/common/model" |
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/model/labels" |
|
|
|
|
"github.com/prometheus/prometheus/model/relabel" |
|
|
|
|
"github.com/prometheus/prometheus/model/timestamp" |
|
|
|
|
"github.com/prometheus/prometheus/notifier" |
|
|
|
|
"github.com/prometheus/prometheus/promql" |
|
|
|
|
"github.com/prometheus/prometheus/promql/parser" |
|
|
|
|
"github.com/prometheus/prometheus/storage" |
|
|
|
|
@ -659,3 +662,53 @@ func TestQueryForStateSeries(t *testing.T) { |
|
|
|
|
testFunc(tst) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TestSendAlertsDontAffectActiveAlerts tests a fix for https://github.com/prometheus/prometheus/issues/11424.
|
|
|
|
|
func TestSendAlertsDontAffectActiveAlerts(t *testing.T) { |
|
|
|
|
rule := NewAlertingRule( |
|
|
|
|
"TestRule", |
|
|
|
|
nil, |
|
|
|
|
time.Minute, |
|
|
|
|
labels.FromStrings("severity", "critical"), |
|
|
|
|
labels.EmptyLabels(), labels.EmptyLabels(), "", true, nil, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Set an active alert.
|
|
|
|
|
lbls := labels.FromStrings("a1", "1") |
|
|
|
|
h := lbls.Hash() |
|
|
|
|
al := &Alert{State: StateFiring, Labels: lbls, ActiveAt: time.Now()} |
|
|
|
|
rule.active[h] = al |
|
|
|
|
|
|
|
|
|
expr, err := parser.ParseExpr("foo") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
rule.vector = expr |
|
|
|
|
|
|
|
|
|
// The relabel rule reproduced the bug here.
|
|
|
|
|
opts := notifier.Options{ |
|
|
|
|
QueueCapacity: 1, |
|
|
|
|
RelabelConfigs: []*relabel.Config{ |
|
|
|
|
{ |
|
|
|
|
SourceLabels: model.LabelNames{"a1"}, |
|
|
|
|
Regex: relabel.MustNewRegexp("(.+)"), |
|
|
|
|
TargetLabel: "a1", |
|
|
|
|
Replacement: "bug", |
|
|
|
|
Action: "replace", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
nm := notifier.NewManager(&opts, log.NewNopLogger()) |
|
|
|
|
|
|
|
|
|
f := SendAlerts(nm, "") |
|
|
|
|
notifyFunc := func(ctx context.Context, expr string, alerts ...*Alert) { |
|
|
|
|
require.Len(t, alerts, 1) |
|
|
|
|
require.Equal(t, al, alerts[0]) |
|
|
|
|
f(ctx, expr, alerts...) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
rule.sendAlerts(context.Background(), time.Now(), 0, 0, notifyFunc) |
|
|
|
|
nm.Stop() |
|
|
|
|
|
|
|
|
|
// The relabel rule changes a1=1 to a1=bug.
|
|
|
|
|
// But the labels with the AlertingRule should not be changed.
|
|
|
|
|
require.Equal(t, labels.FromStrings("a1", "1"), rule.active[h].Labels) |
|
|
|
|
} |
|
|
|
|
|