2021-05-13 09:05:33 -05:00
|
|
|
package store_test
|
2021-03-08 14:19:21 -06:00
|
|
|
|
|
|
|
import (
|
2022-02-08 02:52:03 -06:00
|
|
|
"context"
|
2022-10-06 01:22:58 -05:00
|
|
|
"fmt"
|
2021-03-08 14:19:21 -06:00
|
|
|
"testing"
|
2022-08-09 09:28:36 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-03-08 14:19:21 -06:00
|
|
|
|
2023-01-13 17:29:29 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2021-03-08 14:19:21 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
2021-05-13 09:05:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/tests"
|
2023-01-13 17:29:29 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-03-08 14:19:21 -06:00
|
|
|
)
|
|
|
|
|
2021-04-30 12:21:57 -05:00
|
|
|
const baseIntervalSeconds = 10
|
|
|
|
|
2022-10-06 01:22:58 -05:00
|
|
|
func BenchmarkAlertInstanceOperations(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
ctx := context.Background()
|
|
|
|
_, dbstore := tests.SetupTestEnv(b, baseIntervalSeconds)
|
|
|
|
|
|
|
|
const mainOrgID int64 = 1
|
|
|
|
|
|
|
|
alertRule := tests.CreateTestAlertRule(b, ctx, dbstore, 60, mainOrgID)
|
|
|
|
|
|
|
|
// Create some instances to write down and then delete.
|
|
|
|
count := 10_003
|
|
|
|
instances := make([]models.AlertInstance, 0, count)
|
|
|
|
keys := make([]models.AlertInstanceKey, 0, count)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
labels := models.InstanceLabels{"test": fmt.Sprint(i)}
|
|
|
|
_, labelsHash, _ := labels.StringAndHash()
|
|
|
|
instance := models.AlertInstance{
|
|
|
|
AlertInstanceKey: models.AlertInstanceKey{
|
|
|
|
RuleOrgID: alertRule.OrgID,
|
|
|
|
RuleUID: alertRule.UID,
|
|
|
|
LabelsHash: labelsHash,
|
|
|
|
},
|
|
|
|
CurrentState: models.InstanceStateFiring,
|
|
|
|
CurrentReason: string(models.InstanceStateError),
|
|
|
|
Labels: labels,
|
|
|
|
}
|
|
|
|
instances = append(instances, instance)
|
|
|
|
keys = append(keys, instance.AlertInstanceKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2023-04-06 11:06:25 -05:00
|
|
|
for _, instance := range instances {
|
|
|
|
_ = dbstore.SaveAlertInstance(ctx, instance)
|
2022-10-06 01:22:58 -05:00
|
|
|
}
|
2023-04-06 11:06:25 -05:00
|
|
|
_ = dbstore.DeleteAlertInstances(ctx, keys...)
|
2022-10-06 01:22:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 04:04:03 -05:00
|
|
|
func TestIntegrationAlertInstanceOperations(t *testing.T) {
|
2022-06-10 10:46:21 -05:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
2022-02-08 02:52:03 -06:00
|
|
|
ctx := context.Background()
|
2021-08-25 08:11:22 -05:00
|
|
|
_, dbstore := tests.SetupTestEnv(t, baseIntervalSeconds)
|
2021-03-08 14:19:21 -06:00
|
|
|
|
2021-09-29 09:16:40 -05:00
|
|
|
const mainOrgID int64 = 1
|
|
|
|
|
2023-01-13 17:29:29 -06:00
|
|
|
containsHash := func(t *testing.T, instances []*models.AlertInstance, hash string) {
|
|
|
|
t.Helper()
|
|
|
|
for _, i := range instances {
|
|
|
|
if i.LabelsHash == hash {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Fail(t, "%v does not contain an instance with hash %s", instances, hash)
|
|
|
|
}
|
|
|
|
|
2022-02-08 02:52:03 -06:00
|
|
|
alertRule1 := tests.CreateTestAlertRule(t, ctx, dbstore, 60, mainOrgID)
|
2021-04-30 12:21:57 -05:00
|
|
|
orgID := alertRule1.OrgID
|
2021-03-08 14:19:21 -06:00
|
|
|
|
2022-02-08 02:52:03 -06:00
|
|
|
alertRule2 := tests.CreateTestAlertRule(t, ctx, dbstore, 60, mainOrgID)
|
2021-04-30 12:21:57 -05:00
|
|
|
require.Equal(t, orgID, alertRule2.OrgID)
|
2021-03-08 14:19:21 -06:00
|
|
|
|
2022-02-08 02:52:03 -06:00
|
|
|
alertRule3 := tests.CreateTestAlertRule(t, ctx, dbstore, 60, mainOrgID)
|
2021-04-30 12:21:57 -05:00
|
|
|
require.Equal(t, orgID, alertRule3.OrgID)
|
2021-03-08 14:19:21 -06:00
|
|
|
|
2022-02-08 02:52:03 -06:00
|
|
|
alertRule4 := tests.CreateTestAlertRule(t, ctx, dbstore, 60, mainOrgID)
|
2021-04-30 12:21:57 -05:00
|
|
|
require.Equal(t, orgID, alertRule4.OrgID)
|
2021-03-08 14:19:21 -06:00
|
|
|
|
|
|
|
t.Run("can save and read new alert instance", func(t *testing.T) {
|
2022-10-06 01:22:58 -05:00
|
|
|
labels := models.InstanceLabels{"test": "testValue"}
|
|
|
|
_, hash, _ := labels.StringAndHash()
|
|
|
|
instance := models.AlertInstance{
|
|
|
|
AlertInstanceKey: models.AlertInstanceKey{
|
|
|
|
RuleOrgID: alertRule1.OrgID,
|
|
|
|
RuleUID: alertRule1.UID,
|
|
|
|
LabelsHash: hash,
|
|
|
|
},
|
|
|
|
CurrentState: models.InstanceStateFiring,
|
|
|
|
CurrentReason: string(models.InstanceStateError),
|
|
|
|
Labels: labels,
|
|
|
|
}
|
2023-04-06 11:06:25 -05:00
|
|
|
err := dbstore.SaveAlertInstance(ctx, instance)
|
2021-03-08 14:19:21 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-26 14:38:53 -05:00
|
|
|
listCmd := &models.ListAlertInstancesQuery{
|
2022-10-06 01:22:58 -05:00
|
|
|
RuleOrgID: instance.RuleOrgID,
|
|
|
|
RuleUID: instance.RuleUID,
|
2021-03-08 14:19:21 -06:00
|
|
|
}
|
2023-03-28 03:34:35 -05:00
|
|
|
alerts, err := dbstore.ListAlertInstances(ctx, listCmd)
|
2021-03-08 14:19:21 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Len(t, alerts, 1)
|
|
|
|
require.Equal(t, instance.Labels, alerts[0].Labels)
|
|
|
|
require.Equal(t, alertRule1.OrgID, alerts[0].RuleOrgID)
|
|
|
|
require.Equal(t, alertRule1.UID, alerts[0].RuleUID)
|
|
|
|
require.Equal(t, instance.CurrentReason, alerts[0].CurrentReason)
|
2021-03-08 14:19:21 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("can save and read new alert instance with no labels", func(t *testing.T) {
|
2022-10-06 01:22:58 -05:00
|
|
|
labels := models.InstanceLabels{}
|
|
|
|
_, hash, _ := labels.StringAndHash()
|
|
|
|
instance := models.AlertInstance{
|
|
|
|
AlertInstanceKey: models.AlertInstanceKey{
|
|
|
|
RuleOrgID: alertRule2.OrgID,
|
|
|
|
RuleUID: alertRule2.UID,
|
|
|
|
LabelsHash: hash,
|
|
|
|
},
|
|
|
|
CurrentState: models.InstanceStateNormal,
|
|
|
|
Labels: labels,
|
2021-03-08 14:19:21 -06:00
|
|
|
}
|
2023-04-06 11:06:25 -05:00
|
|
|
err := dbstore.SaveAlertInstance(ctx, instance)
|
2021-03-08 14:19:21 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-26 14:38:53 -05:00
|
|
|
listCmd := &models.ListAlertInstancesQuery{
|
2022-10-06 01:22:58 -05:00
|
|
|
RuleOrgID: instance.RuleOrgID,
|
|
|
|
RuleUID: instance.RuleUID,
|
2021-03-08 14:19:21 -06:00
|
|
|
}
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
alerts, err := dbstore.ListAlertInstances(ctx, listCmd)
|
2021-03-08 14:19:21 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Len(t, alerts, 1)
|
|
|
|
require.Equal(t, alertRule2.OrgID, alerts[0].RuleOrgID)
|
|
|
|
require.Equal(t, alertRule2.UID, alerts[0].RuleUID)
|
|
|
|
require.Equal(t, instance.Labels, alerts[0].Labels)
|
2021-03-08 14:19:21 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("can save two instances with same org_id, uid and different labels", func(t *testing.T) {
|
2022-10-06 01:22:58 -05:00
|
|
|
labels := models.InstanceLabels{"test": "testValue"}
|
|
|
|
_, hash, _ := labels.StringAndHash()
|
|
|
|
instance1 := models.AlertInstance{
|
|
|
|
AlertInstanceKey: models.AlertInstanceKey{
|
|
|
|
RuleOrgID: alertRule3.OrgID,
|
|
|
|
RuleUID: alertRule3.UID,
|
|
|
|
LabelsHash: hash,
|
|
|
|
},
|
|
|
|
CurrentState: models.InstanceStateFiring,
|
|
|
|
Labels: labels,
|
2021-03-08 14:19:21 -06:00
|
|
|
}
|
|
|
|
|
2023-04-06 11:06:25 -05:00
|
|
|
err := dbstore.SaveAlertInstance(ctx, instance1)
|
2021-03-08 14:19:21 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-10-06 01:22:58 -05:00
|
|
|
labels = models.InstanceLabels{"test": "testValue2"}
|
|
|
|
_, hash, _ = labels.StringAndHash()
|
|
|
|
instance2 := models.AlertInstance{
|
|
|
|
AlertInstanceKey: models.AlertInstanceKey{
|
|
|
|
RuleOrgID: instance1.RuleOrgID,
|
|
|
|
RuleUID: instance1.RuleUID,
|
|
|
|
LabelsHash: hash,
|
|
|
|
},
|
|
|
|
CurrentState: models.InstanceStateFiring,
|
|
|
|
Labels: labels,
|
2021-03-08 14:19:21 -06:00
|
|
|
}
|
2023-04-06 11:06:25 -05:00
|
|
|
err = dbstore.SaveAlertInstance(ctx, instance2)
|
2021-03-08 14:19:21 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
listQuery := &models.ListAlertInstancesQuery{
|
2022-10-06 01:22:58 -05:00
|
|
|
RuleOrgID: instance1.RuleOrgID,
|
|
|
|
RuleUID: instance1.RuleUID,
|
2021-03-08 14:19:21 -06:00
|
|
|
}
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
alerts, err := dbstore.ListAlertInstances(ctx, listQuery)
|
2021-03-08 14:19:21 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Len(t, alerts, 2)
|
2021-03-08 14:19:21 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("can list all added instances in org", func(t *testing.T) {
|
|
|
|
listQuery := &models.ListAlertInstancesQuery{
|
2021-05-03 06:19:15 -05:00
|
|
|
RuleOrgID: orgID,
|
2021-03-08 14:19:21 -06:00
|
|
|
}
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
alerts, err := dbstore.ListAlertInstances(ctx, listQuery)
|
2021-03-08 14:19:21 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Len(t, alerts, 4)
|
2021-03-08 14:19:21 -06:00
|
|
|
})
|
|
|
|
|
2023-01-13 17:29:29 -06:00
|
|
|
t.Run("should ignore Normal state with no reason if feature flag is enabled", func(t *testing.T) {
|
|
|
|
labels := models.InstanceLabels{"test": util.GenerateShortUID()}
|
|
|
|
instance1 := models.AlertInstance{
|
|
|
|
AlertInstanceKey: models.AlertInstanceKey{
|
|
|
|
RuleOrgID: orgID,
|
|
|
|
RuleUID: util.GenerateShortUID(),
|
|
|
|
LabelsHash: util.GenerateShortUID(),
|
|
|
|
},
|
|
|
|
CurrentState: models.InstanceStateNormal,
|
|
|
|
CurrentReason: "",
|
|
|
|
Labels: labels,
|
|
|
|
}
|
|
|
|
instance2 := models.AlertInstance{
|
|
|
|
AlertInstanceKey: models.AlertInstanceKey{
|
|
|
|
RuleOrgID: orgID,
|
|
|
|
RuleUID: util.GenerateShortUID(),
|
|
|
|
LabelsHash: util.GenerateShortUID(),
|
|
|
|
},
|
|
|
|
CurrentState: models.InstanceStateNormal,
|
|
|
|
CurrentReason: models.StateReasonError,
|
|
|
|
Labels: labels,
|
|
|
|
}
|
2023-04-06 11:06:25 -05:00
|
|
|
err := dbstore.SaveAlertInstance(ctx, instance1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = dbstore.SaveAlertInstance(ctx, instance2)
|
2023-01-13 17:29:29 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
listQuery := &models.ListAlertInstancesQuery{
|
2021-05-03 06:19:15 -05:00
|
|
|
RuleOrgID: orgID,
|
2021-03-08 14:19:21 -06:00
|
|
|
}
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
alerts, err := dbstore.ListAlertInstances(ctx, listQuery)
|
2021-03-08 14:19:21 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
containsHash(t, alerts, instance1.LabelsHash)
|
2023-01-13 17:29:29 -06:00
|
|
|
|
|
|
|
f := dbstore.FeatureToggles
|
|
|
|
dbstore.FeatureToggles = featuremgmt.WithFeatures(featuremgmt.FlagAlertingNoNormalState)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
dbstore.FeatureToggles = f
|
|
|
|
})
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
alerts, err = dbstore.ListAlertInstances(ctx, listQuery)
|
2023-01-13 17:29:29 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
containsHash(t, alerts, instance2.LabelsHash)
|
2023-01-13 17:29:29 -06:00
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
for _, instance := range alerts {
|
2023-01-13 17:29:29 -06:00
|
|
|
if instance.CurrentState == models.InstanceStateNormal && instance.CurrentReason == "" {
|
|
|
|
require.Fail(t, "List operation expected to return all states except Normal but the result contains Normal states")
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 14:19:21 -06:00
|
|
|
})
|
|
|
|
|
2022-10-06 01:22:58 -05:00
|
|
|
t.Run("update instance with same org_id, uid and different state", func(t *testing.T) {
|
|
|
|
labels := models.InstanceLabels{"test": "testValue"}
|
|
|
|
_, hash, _ := labels.StringAndHash()
|
|
|
|
instance1 := models.AlertInstance{
|
|
|
|
AlertInstanceKey: models.AlertInstanceKey{
|
|
|
|
RuleOrgID: alertRule4.OrgID,
|
|
|
|
RuleUID: alertRule4.UID,
|
|
|
|
LabelsHash: hash,
|
|
|
|
},
|
|
|
|
CurrentState: models.InstanceStateFiring,
|
|
|
|
Labels: labels,
|
2021-03-08 14:19:21 -06:00
|
|
|
}
|
|
|
|
|
2023-04-06 11:06:25 -05:00
|
|
|
err := dbstore.SaveAlertInstance(ctx, instance1)
|
2021-03-08 14:19:21 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-10-06 01:22:58 -05:00
|
|
|
instance2 := models.AlertInstance{
|
|
|
|
AlertInstanceKey: models.AlertInstanceKey{
|
|
|
|
RuleOrgID: alertRule4.OrgID,
|
|
|
|
RuleUID: instance1.RuleUID,
|
|
|
|
LabelsHash: instance1.LabelsHash,
|
|
|
|
},
|
|
|
|
CurrentState: models.InstanceStateNormal,
|
|
|
|
Labels: instance1.Labels,
|
2021-03-08 14:19:21 -06:00
|
|
|
}
|
2023-04-06 11:06:25 -05:00
|
|
|
err = dbstore.SaveAlertInstance(ctx, instance2)
|
2021-03-08 14:19:21 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
listQuery := &models.ListAlertInstancesQuery{
|
2021-05-03 06:19:15 -05:00
|
|
|
RuleOrgID: alertRule4.OrgID,
|
|
|
|
RuleUID: alertRule4.UID,
|
2021-03-08 14:19:21 -06:00
|
|
|
}
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
alerts, err := dbstore.ListAlertInstances(ctx, listQuery)
|
2021-03-08 14:19:21 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Len(t, alerts, 1)
|
2021-03-08 14:19:21 -06:00
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
require.Equal(t, instance2.RuleOrgID, alerts[0].RuleOrgID)
|
|
|
|
require.Equal(t, instance2.RuleUID, alerts[0].RuleUID)
|
|
|
|
require.Equal(t, instance2.Labels, alerts[0].Labels)
|
|
|
|
require.Equal(t, instance2.CurrentState, alerts[0].CurrentState)
|
2021-03-08 14:19:21 -06:00
|
|
|
})
|
|
|
|
}
|