Alerting: Enable Unified Alerting for open source and enterprise (#49834) (#49845)

This commit enables Unified Alerting for open source and enterprise unless disabled in configuration.

(cherry picked from commit 3b7f871bf4)

Co-authored-by: George Robinson <george.robinson@grafana.com>
This commit is contained in:
Grot (@grafanabot)
2022-05-30 18:13:39 +02:00
committed by GitHub
co-authored by George Robinson
parent 27b20676af
commit ed06e3e4f9
5 changed files with 42 additions and 185 deletions
@@ -1,8 +1,6 @@
package migrations
import (
"os"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/sqlstore/migrations/accesscontrol"
"github.com/grafana/grafana/pkg/services/sqlstore/migrations/ualert"
@@ -52,11 +50,6 @@ func (*OSSMigrations) AddMigration(mg *Migrator) {
addUserAuthTokenMigrations(mg)
addCacheMigration(mg)
addShortURLMigrations(mg)
// TODO Delete when unified alerting is enabled by default unconditionally (Grafana v9)
if err := ualert.CheckUnifiedAlertingEnabledByDefault(mg); err != nil { // this should always go before any other ualert migration
mg.Logger.Error("failed to determine the status of alerting engine. Enable either legacy or unified alerting explicitly and try again", "err", err)
os.Exit(1)
}
ualert.AddTablesMigrations(mg)
ualert.AddDashAlertMigration(mg)
addLibraryElementsMigrations(mg)
@@ -706,48 +706,6 @@ func (u *upgradeNgAlerting) SQL(migrator.Dialect) string {
return "code migration"
}
// CheckUnifiedAlertingEnabledByDefault determines the final status of unified alerting, if it is not enabled explicitly.
// Checks table `alert` and if it is empty, then it changes UnifiedAlerting.Enabled to true. Otherwise, it sets the flag to false.
// After this method is executed the status of alerting should be determined, i.e. both flags will not be nil.
// Note: this is not a real migration but a step that other migrations depend on.
// TODO Delete when unified alerting is enabled by default unconditionally (Grafana v9)
func CheckUnifiedAlertingEnabledByDefault(migrator *migrator.Migrator) error {
// if [unified_alerting][enabled] is explicitly set, we've got nothing to do here.
if migrator.Cfg.UnifiedAlerting.Enabled != nil {
return nil
}
var ualertEnabled bool
// this duplicates the logic in setting.ReadUnifiedAlertingSettings, and is put here just for logical completeness.
if setting.AlertingEnabled != nil && !*setting.AlertingEnabled {
ualertEnabled = true
migrator.Cfg.UnifiedAlerting.Enabled = &ualertEnabled
migrator.Logger.Debug("Unified alerting is enabled because the legacy is disabled explicitly")
return nil
}
resp := &struct {
Count int64
}{}
exist, err := migrator.DBEngine.IsTableExist("alert")
if err != nil {
return fmt.Errorf("failed to verify if the 'alert' table exists: %w", err)
}
if exist {
if _, err := migrator.DBEngine.SQL("SELECT COUNT(1) as count FROM alert").Get(resp); err != nil {
return fmt.Errorf("failed to read 'alert' table: %w", err)
}
}
// if table does not exist then we treat it as absence of legacy alerting and therefore enable unified alerting.
ualertEnabled = resp.Count == 0
legacyEnabled := !ualertEnabled
migrator.Cfg.UnifiedAlerting.Enabled = &ualertEnabled
setting.AlertingEnabled = &legacyEnabled
migrator.Logger.Debug(fmt.Sprintf("Found %d legacy alerts in the database. Unified alerting enabled is %v", resp.Count, ualertEnabled))
return nil
}
// getAlertFolderNameFromDashboard generates a folder name for alerts that belong to a dashboard. Formats the string according to DASHBOARD_FOLDER format.
// If the resulting string exceeds the migrations.MaxTitleLength, the dashboard title is stripped to be at the maximum length
func getAlertFolderNameFromDashboard(dash *dashboard) string {
@@ -7,15 +7,10 @@ import (
"sort"
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/util"
"github.com/prometheus/alertmanager/pkg/labels"
"github.com/stretchr/testify/require"
"xorm.io/xorm"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
)
var MigTitle = migTitle
@@ -103,99 +98,6 @@ func Test_validateAlertmanagerConfig(t *testing.T) {
}
}
func TestCheckUnifiedAlertingEnabledByDefault(t *testing.T) {
testDB := sqlutil.SQLite3TestDB()
x, err := xorm.NewEngine(testDB.DriverName, testDB.ConnStr)
require.NoError(t, err)
_, err = x.Exec("CREATE TABLE alert ( id bigint )")
require.NoError(t, err)
t.Cleanup(func() {
_, err = x.Exec("DROP TABLE alert")
require.NoError(t, err)
})
tests := []struct {
title string
legacyAlertExists bool
legacyIsDefined bool
legacyValue bool
expectedUnifiedAlerting bool
}{
{
title: "enable unified alerting when there are no legacy alerts",
legacyIsDefined: false,
legacyAlertExists: false,
expectedUnifiedAlerting: true,
},
{
title: "enable unified alerting when there are no legacy alerts and legacy enabled",
legacyIsDefined: true,
legacyValue: true,
legacyAlertExists: false,
expectedUnifiedAlerting: true,
},
{
title: "enable unified alerting when there are no legacy alerts and legacy disabled",
legacyIsDefined: true,
legacyValue: false,
legacyAlertExists: false,
expectedUnifiedAlerting: true,
},
{
title: "enable unified alerting when there are legacy alerts but legacy disabled",
legacyIsDefined: true,
legacyValue: false,
legacyAlertExists: true,
expectedUnifiedAlerting: true,
},
{
title: "disable unified alerting when there are legacy alerts",
legacyIsDefined: false,
legacyAlertExists: true,
expectedUnifiedAlerting: false,
},
{
title: "disable unified alerting when there are legacy alerts and it is enabled",
legacyIsDefined: true,
legacyValue: true,
legacyAlertExists: true,
expectedUnifiedAlerting: false,
},
}
for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
setting.AlertingEnabled = nil
if test.legacyIsDefined {
value := test.legacyValue
setting.AlertingEnabled = &value
}
if test.legacyAlertExists {
_, err := x.Exec("INSERT INTO alert VALUES (1)")
require.NoError(t, err)
} else {
_, err := x.Exec("DELETE FROM alert")
require.NoError(t, err)
}
cfg := setting.Cfg{
UnifiedAlerting: setting.UnifiedAlertingSettings{
Enabled: nil,
},
}
mg := migrator.NewMigrator(x, &cfg)
err := CheckUnifiedAlertingEnabledByDefault(mg)
require.NoError(t, err)
require.NotNil(t, setting.AlertingEnabled)
require.NotNil(t, cfg.UnifiedAlerting.Enabled)
require.Equal(t, *cfg.UnifiedAlerting.Enabled, test.expectedUnifiedAlerting)
require.Equal(t, *setting.AlertingEnabled, !test.expectedUnifiedAlerting)
})
}
}
func configFromReceivers(t *testing.T, receivers []*PostableGrafanaReceiver) *PostableUserConfig {
t.Helper()