Alerting: Pause dash alerts on migration (#62798)

* Alerting: Pause dash alerts on migration
This commit is contained in:
George Robinson
2023-02-02 21:49:05 +00:00
committed by GitHub
parent 27d429e3b1
commit f49efa6e27
3 changed files with 32 additions and 0 deletions

View File

@@ -37,6 +37,7 @@ type alertRule struct {
Updated time.Time
Annotations map[string]string
Labels map[string]string
IsPaused bool
}
type alertRuleVersion struct {
@@ -61,6 +62,7 @@ type alertRuleVersion struct {
For duration
Annotations map[string]string
Labels map[string]string
IsPaused bool
}
func (a *alertRule) makeVersion() *alertRuleVersion {
@@ -84,6 +86,7 @@ func (a *alertRule) makeVersion() *alertRuleVersion {
For: a.For,
Annotations: a.Annotations,
Labels: map[string]string{},
IsPaused: a.IsPaused,
}
}
@@ -120,6 +123,11 @@ func (m *migration) makeAlertRule(cond condition, da dashAlert, folderUID string
name := normalizeRuleName(da.Name, uid)
isPaused := false
if da.State == "paused" {
isPaused = true
}
ar := &alertRule{
OrgID: da.OrgId,
Title: name, // TODO: Make sure all names are unique, make new name on constraint insert error.
@@ -135,6 +143,7 @@ func (m *migration) makeAlertRule(cond condition, da dashAlert, folderUID string
Annotations: annotations,
Labels: lbls,
RuleGroupIndex: 1,
IsPaused: isPaused,
}
ar.NoDataState, err = transNoData(da.ParsedSettings.NoDataState)

View File

@@ -117,6 +117,27 @@ func TestMakeAlertRule(t *testing.T) {
require.Equal(t, ar.Title, ar.RuleGroup)
})
})
t.Run("alert is not paused", func(t *testing.T) {
m := newTestMigration(t)
da := createTestDashAlert()
cnd := createTestDashAlertCondition()
ar, err := m.makeAlertRule(cnd, da, "folder")
require.NoError(t, err)
require.False(t, ar.IsPaused)
})
t.Run("paused dash alert is paused", func(t *testing.T) {
m := newTestMigration(t)
da := createTestDashAlert()
da.State = "paused"
cnd := createTestDashAlertCondition()
ar, err := m.makeAlertRule(cnd, da, "folder")
require.NoError(t, err)
require.True(t, ar.IsPaused)
})
}
func createTestDashAlert() dashAlert {

View File

@@ -5,6 +5,7 @@ import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/prometheus/alertmanager/silence/silencepb"
)
// newTestMigration generates an empty migration to use in tests.
@@ -19,5 +20,6 @@ func newTestMigration(t *testing.T) *migration {
seenUIDs: uidSet{
set: make(map[string]struct{}),
},
silences: make(map[int64][]*silencepb.MeshSilence),
}
}