From 15be9861d03dcd3ecd239e5a0223ffb6aa96d8fb Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 12 May 2025 14:25:43 +0200 Subject: [PATCH] Remote Alertmanager: Remove code for remote only mode (#105184) --- .../src/types/featureToggles.gen.ts | 4 - pkg/services/featuremgmt/registry.go | 6 -- pkg/services/featuremgmt/toggles_gen.csv | 1 - pkg/services/featuremgmt/toggles_gen.go | 4 - pkg/services/featuremgmt/toggles_gen.json | 3 +- .../ngalert/metrics/remote_alertmanager.go | 1 - pkg/services/ngalert/ngalert.go | 98 +++++-------------- 7 files changed, 24 insertions(+), 93 deletions(-) diff --git a/packages/grafana-data/src/types/featureToggles.gen.ts b/packages/grafana-data/src/types/featureToggles.gen.ts index 40455f2010c..3ddaf5c4e9e 100644 --- a/packages/grafana-data/src/types/featureToggles.gen.ts +++ b/packages/grafana-data/src/types/featureToggles.gen.ts @@ -371,10 +371,6 @@ export interface FeatureToggles { */ alertmanagerRemotePrimary?: boolean; /** - * Disable the internal Alertmanager and only use the external one defined. - */ - alertmanagerRemoteOnly?: boolean; - /** * Change the way annotation permissions work by scoping them to folders and dashboards. * @default true */ diff --git a/pkg/services/featuremgmt/registry.go b/pkg/services/featuremgmt/registry.go index 1781eb7ac95..9b96ddfbd3c 100644 --- a/pkg/services/featuremgmt/registry.go +++ b/pkg/services/featuremgmt/registry.go @@ -615,12 +615,6 @@ var ( Stage: FeatureStageExperimental, Owner: grafanaAlertingSquad, }, - { - Name: "alertmanagerRemoteOnly", - Description: "Disable the internal Alertmanager and only use the external one defined.", - Stage: FeatureStageExperimental, - Owner: grafanaAlertingSquad, - }, { Name: "annotationPermissionUpdate", Description: "Change the way annotation permissions work by scoping them to folders and dashboards.", diff --git a/pkg/services/featuremgmt/toggles_gen.csv b/pkg/services/featuremgmt/toggles_gen.csv index a80534f0e54..574752b3938 100644 --- a/pkg/services/featuremgmt/toggles_gen.csv +++ b/pkg/services/featuremgmt/toggles_gen.csv @@ -81,7 +81,6 @@ prometheusCodeModeMetricNamesSearch,experimental,@grafana/oss-big-tent,false,fal addFieldFromCalculationStatFunctions,GA,@grafana/dataviz-squad,false,false,true alertmanagerRemoteSecondary,experimental,@grafana/alerting-squad,false,false,false alertmanagerRemotePrimary,experimental,@grafana/alerting-squad,false,false,false -alertmanagerRemoteOnly,experimental,@grafana/alerting-squad,false,false,false annotationPermissionUpdate,GA,@grafana/identity-access-team,false,false,false extractFieldsNameDeduplication,experimental,@grafana/dataviz-squad,false,false,true dashboardSceneForViewers,GA,@grafana/dashboards-squad,false,false,true diff --git a/pkg/services/featuremgmt/toggles_gen.go b/pkg/services/featuremgmt/toggles_gen.go index f17f133edc5..a9e0149f5a2 100644 --- a/pkg/services/featuremgmt/toggles_gen.go +++ b/pkg/services/featuremgmt/toggles_gen.go @@ -335,10 +335,6 @@ const ( // Enable Grafana to have a remote Alertmanager instance as the primary Alertmanager. FlagAlertmanagerRemotePrimary = "alertmanagerRemotePrimary" - // FlagAlertmanagerRemoteOnly - // Disable the internal Alertmanager and only use the external one defined. - FlagAlertmanagerRemoteOnly = "alertmanagerRemoteOnly" - // FlagAnnotationPermissionUpdate // Change the way annotation permissions work by scoping them to folders and dashboards. FlagAnnotationPermissionUpdate = "annotationPermissionUpdate" diff --git a/pkg/services/featuremgmt/toggles_gen.json b/pkg/services/featuremgmt/toggles_gen.json index de5c8f7d6c3..5f46c6da05a 100644 --- a/pkg/services/featuremgmt/toggles_gen.json +++ b/pkg/services/featuremgmt/toggles_gen.json @@ -385,7 +385,8 @@ "metadata": { "name": "alertmanagerRemoteOnly", "resourceVersion": "1743693517832", - "creationTimestamp": "2023-10-30T16:27:08Z" + "creationTimestamp": "2023-10-30T16:27:08Z", + "deletionTimestamp": "2025-05-09T12:58:01Z" }, "spec": { "description": "Disable the internal Alertmanager and only use the external one defined.", diff --git a/pkg/services/ngalert/metrics/remote_alertmanager.go b/pkg/services/ngalert/metrics/remote_alertmanager.go index 5963f9facb6..b09f3ecb3fb 100644 --- a/pkg/services/ngalert/metrics/remote_alertmanager.go +++ b/pkg/services/ngalert/metrics/remote_alertmanager.go @@ -9,7 +9,6 @@ import ( const ( ModeRemoteSecondary = "remote_secondary" ModeRemotePrimary = "remote_primary" - ModeRemoteOnly = "remote_only" ) type RemoteAlertmanager struct { diff --git a/pkg/services/ngalert/ngalert.go b/pkg/services/ngalert/ngalert.go index a3ab48b821f..7e1bfe53a12 100644 --- a/pkg/services/ngalert/ngalert.go +++ b/pkg/services/ngalert/ngalert.go @@ -179,61 +179,34 @@ func (ng *AlertNG) init() error { // is removed from Alertmanager. compat.InitFromFlags(ng.Log, featurecontrol.NoopFlags{}) - // If enabled, configure the remote Alertmanager. - // - If several toggles are enabled, the order of precedence is RemoteOnly, RemotePrimary, RemoteSecondary - // - If no toggles are enabled, we default to using only the internal Alertmanager - // We currently do not support remote primary mode, so we fall back to remote secondary. + // Configure the remote Alertmanager. + // If toggles for both modes are enabled, remote primary takes precedence. var overrides []notifier.Option moaLogger := log.New("ngalert.multiorg.alertmanager") - remoteOnly := ng.FeatureToggles.IsEnabled(initCtx, featuremgmt.FlagAlertmanagerRemoteOnly) remotePrimary := ng.FeatureToggles.IsEnabled(initCtx, featuremgmt.FlagAlertmanagerRemotePrimary) remoteSecondary := ng.FeatureToggles.IsEnabled(initCtx, featuremgmt.FlagAlertmanagerRemoteSecondary) - if remoteOnly || remotePrimary || remoteSecondary { + if remotePrimary || remoteSecondary { + m := ng.Metrics.GetRemoteAlertmanagerMetrics() + cfg := remote.AlertmanagerConfig{ + BasicAuthPassword: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.Password, + DefaultConfig: ng.Cfg.UnifiedAlerting.DefaultConfiguration, + TenantID: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.TenantID, + URL: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.URL, + ExternalURL: ng.Cfg.AppURL, + SmtpFrom: ng.Cfg.Smtp.FromAddress, + StaticHeaders: ng.Cfg.Smtp.StaticHeaders, + } autogenFn := func(ctx context.Context, logger log.Logger, orgID int64, cfg *definitions.PostableApiAlertingConfig, skipInvalid bool) error { return notifier.AddAutogenConfig(ctx, logger, ng.store, orgID, cfg, skipInvalid) } - switch { - case remoteOnly: - ng.Log.Debug("Starting Grafana with remote only mode enabled") - m := ng.Metrics.GetRemoteAlertmanagerMetrics() - m.Info.WithLabelValues(metrics.ModeRemoteOnly).Set(1) - ng.Cfg.UnifiedAlerting.SkipClustering = true - - // This function will be used by the MOA to create new Alertmanagers. - override := notifier.WithAlertmanagerOverride(func(_ notifier.OrgAlertmanagerFactory) notifier.OrgAlertmanagerFactory { - return func(ctx context.Context, orgID int64) (notifier.Alertmanager, error) { - // Create remote Alertmanager. - cfg := remote.AlertmanagerConfig{ - BasicAuthPassword: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.Password, - DefaultConfig: ng.Cfg.UnifiedAlerting.DefaultConfiguration, - OrgID: orgID, - TenantID: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.TenantID, - URL: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.URL, - PromoteConfig: true, - SyncInterval: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.SyncInterval, - ExternalURL: ng.Cfg.AppURL, - SmtpFrom: ng.Cfg.Smtp.FromAddress, - StaticHeaders: ng.Cfg.Smtp.StaticHeaders, - } - remoteAM, err := createRemoteAlertmanager(ctx, cfg, ng.KVStore, ng.SecretsService.Decrypt, autogenFn, m, ng.tracer) - if err != nil { - moaLogger.Error("Failed to create remote Alertmanager", "err", err) - return nil, err - } - return remoteAM, nil - } - }) - - overrides = append(overrides, override) - - case remotePrimary: + var override notifier.Option + if remotePrimary { ng.Log.Debug("Starting Grafana with remote primary mode enabled") - m := ng.Metrics.GetRemoteAlertmanagerMetrics() m.Info.WithLabelValues(metrics.ModeRemotePrimary).Set(1) ng.Cfg.UnifiedAlerting.SkipClustering = true // This function will be used by the MOA to create new Alertmanagers. - override := notifier.WithAlertmanagerOverride(func(factoryFn notifier.OrgAlertmanagerFactory) notifier.OrgAlertmanagerFactory { + override = notifier.WithAlertmanagerOverride(func(factoryFn notifier.OrgAlertmanagerFactory) notifier.OrgAlertmanagerFactory { return func(ctx context.Context, orgID int64) (notifier.Alertmanager, error) { // Create internal Alertmanager. internalAM, err := factoryFn(ctx, orgID) @@ -242,17 +215,8 @@ func (ng *AlertNG) init() error { } // Create remote Alertmanager. - cfg := remote.AlertmanagerConfig{ - BasicAuthPassword: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.Password, - DefaultConfig: ng.Cfg.UnifiedAlerting.DefaultConfiguration, - OrgID: orgID, - PromoteConfig: true, - TenantID: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.TenantID, - URL: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.URL, - ExternalURL: ng.Cfg.AppURL, - SmtpFrom: ng.Cfg.Smtp.FromAddress, - StaticHeaders: ng.Cfg.Smtp.StaticHeaders, - } + cfg.OrgID = orgID + cfg.PromoteConfig = true remoteAM, err := createRemoteAlertmanager(ctx, cfg, ng.KVStore, ng.SecretsService.Decrypt, autogenFn, m, ng.tracer) if err != nil { moaLogger.Error("Failed to create remote Alertmanager, falling back to using only the internal one", "err", err) @@ -263,16 +227,12 @@ func (ng *AlertNG) init() error { return remote.NewRemotePrimaryForkedAlertmanager(log.New("ngalert.forked-alertmanager.remote-primary"), internalAM, remoteAM), nil } }) - - overrides = append(overrides, override) - - case remoteSecondary: + } else { ng.Log.Debug("Starting Grafana with remote secondary mode enabled") - m := ng.Metrics.GetRemoteAlertmanagerMetrics() m.Info.WithLabelValues(metrics.ModeRemoteSecondary).Set(1) // This function will be used by the MOA to create new Alertmanagers. - override := notifier.WithAlertmanagerOverride(func(factoryFn notifier.OrgAlertmanagerFactory) notifier.OrgAlertmanagerFactory { + override = notifier.WithAlertmanagerOverride(func(factoryFn notifier.OrgAlertmanagerFactory) notifier.OrgAlertmanagerFactory { return func(ctx context.Context, orgID int64) (notifier.Alertmanager, error) { // Create internal Alertmanager. internalAM, err := factoryFn(ctx, orgID) @@ -281,17 +241,7 @@ func (ng *AlertNG) init() error { } // Create remote Alertmanager. - cfg := remote.AlertmanagerConfig{ - BasicAuthPassword: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.Password, - DefaultConfig: ng.Cfg.UnifiedAlerting.DefaultConfiguration, - OrgID: orgID, - TenantID: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.TenantID, - URL: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.URL, - SyncInterval: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.SyncInterval, - ExternalURL: ng.Cfg.AppURL, - SmtpFrom: ng.Cfg.Smtp.FromAddress, - StaticHeaders: ng.Cfg.Smtp.StaticHeaders, - } + cfg.OrgID = orgID remoteAM, err := createRemoteAlertmanager(ctx, cfg, ng.KVStore, ng.SecretsService.Decrypt, autogenFn, m, ng.tracer) if err != nil { moaLogger.Error("Failed to create remote Alertmanager, falling back to using only the internal one", "err", err) @@ -308,12 +258,8 @@ func (ng *AlertNG) init() error { return remote.NewRemoteSecondaryForkedAlertmanager(rsCfg, internalAM, remoteAM) } }) - - overrides = append(overrides, override) - - default: - ng.Log.Error("A mode should be selected when enabling the remote Alertmanager, falling back to using only the internal Alertmanager") } + overrides = append(overrides, override) } decryptFn := ng.SecretsService.GetDecryptedValue