From de7af8f75544cfd7d17da98f5b42d9b693fef200 Mon Sep 17 00:00:00 2001 From: Vadim Stepanov Date: Wed, 16 Jul 2025 16:17:16 +0100 Subject: [PATCH] add feature toggle --- .../src/types/featureToggles.gen.ts | 5 ++ pkg/services/featuremgmt/registry.go | 9 ++++ pkg/services/featuremgmt/toggles_gen.csv | 1 + pkg/services/featuremgmt/toggles_gen.go | 4 ++ pkg/services/featuremgmt/toggles_gen.json | 15 ++++++ pkg/services/ngalert/ngalert.go | 6 +++ pkg/services/ngalert/ngalert_test.go | 46 +++++++++++++------ 7 files changed, 72 insertions(+), 14 deletions(-) diff --git a/packages/grafana-data/src/types/featureToggles.gen.ts b/packages/grafana-data/src/types/featureToggles.gen.ts index 1832b95666f..8fd56e5a98a 100644 --- a/packages/grafana-data/src/types/featureToggles.gen.ts +++ b/packages/grafana-data/src/types/featureToggles.gen.ts @@ -1026,4 +1026,9 @@ export interface FeatureToggles { * @default false */ enablePluginImporter?: boolean; + /** + * Enables the notification history feature + * @default false + */ + alertingNotificationHistory?: boolean; } diff --git a/pkg/services/featuremgmt/registry.go b/pkg/services/featuremgmt/registry.go index a82c3a77dbd..1a0be3432b9 100644 --- a/pkg/services/featuremgmt/registry.go +++ b/pkg/services/featuremgmt/registry.go @@ -1765,6 +1765,15 @@ var ( FrontendOnly: true, Expression: "false", }, + { + Name: "alertingNotificationHistory", + Description: "Enables the notification history feature", + Stage: FeatureStageExperimental, + Owner: grafanaAlertingSquad, + HideFromAdminPage: true, + HideFromDocs: true, + Expression: "false", + }, } ) diff --git a/pkg/services/featuremgmt/toggles_gen.csv b/pkg/services/featuremgmt/toggles_gen.csv index 81d6bf376f7..32ce5cb9cc0 100644 --- a/pkg/services/featuremgmt/toggles_gen.csv +++ b/pkg/services/featuremgmt/toggles_gen.csv @@ -229,3 +229,4 @@ newInfluxDSConfigPageDesign,privatePreview,@grafana/partner-datasources,false,fa enableAppChromeExtensions,experimental,@grafana/plugins-platform-backend,false,false,true foldersAppPlatformAPI,experimental,@grafana/grafana-search-navigate-organise,false,false,true enablePluginImporter,experimental,@grafana/plugins-platform-backend,false,false,true +alertingNotificationHistory,experimental,@grafana/alerting-squad,false,false,false diff --git a/pkg/services/featuremgmt/toggles_gen.go b/pkg/services/featuremgmt/toggles_gen.go index 6069be22df0..3cfa802b65e 100644 --- a/pkg/services/featuremgmt/toggles_gen.go +++ b/pkg/services/featuremgmt/toggles_gen.go @@ -926,4 +926,8 @@ const ( // FlagEnablePluginImporter // Set this to true to use the new PluginImporter functionality FlagEnablePluginImporter = "enablePluginImporter" + + // FlagAlertingNotificationHistory + // Enables the notification history feature + FlagAlertingNotificationHistory = "alertingNotificationHistory" ) diff --git a/pkg/services/featuremgmt/toggles_gen.json b/pkg/services/featuremgmt/toggles_gen.json index 793dd264e1c..fe5b270d9ac 100644 --- a/pkg/services/featuremgmt/toggles_gen.json +++ b/pkg/services/featuremgmt/toggles_gen.json @@ -222,6 +222,21 @@ "expression": "true" } }, + { + "metadata": { + "name": "alertingNotificationHistory", + "resourceVersion": "1752677257304", + "creationTimestamp": "2025-07-16T14:47:37Z" + }, + "spec": { + "description": "Enables the notification history feature", + "stage": "experimental", + "codeowner": "@grafana/alerting-squad", + "hideFromAdminPage": true, + "hideFromDocs": true, + "expression": "false" + } + }, { "metadata": { "name": "alertingNotificationsStepMode", diff --git a/pkg/services/ngalert/ngalert.go b/pkg/services/ngalert/ngalert.go index 9c386d3254a..81c8892e902 100644 --- a/pkg/services/ngalert/ngalert.go +++ b/pkg/services/ngalert/ngalert.go @@ -287,6 +287,7 @@ func (ng *AlertNG) init() error { notificationHistorian, err := configureNotificationHistorian( initCtx, + ng.FeatureToggles, ng.Cfg.UnifiedAlerting.NotificationHistory, ng.Metrics.GetNotificationHistorianMetrics(), ng.Log, @@ -732,11 +733,16 @@ func configureHistorianBackend( func configureNotificationHistorian( ctx context.Context, + featureToggles featuremgmt.FeatureToggles, cfg setting.UnifiedAlertingNotificationHistorySettings, met *metrics.NotificationHistorian, l log.Logger, tracer tracing.Tracer, ) (*notifier.NotificationHistorian, error) { + if !featureToggles.IsEnabled(ctx, featuremgmt.FlagAlertingNotificationHistory) { + return nil, nil + } + if !cfg.Enabled { met.Info.Set(0) return nil, nil diff --git a/pkg/services/ngalert/ngalert_test.go b/pkg/services/ngalert/ngalert_test.go index 64d11d9204f..640a81821c9 100644 --- a/pkg/services/ngalert/ngalert_test.go +++ b/pkg/services/ngalert/ngalert_test.go @@ -240,6 +240,7 @@ func TestConfigureNotificationHistorian(t *testing.T) { met := metrics.NewNotificationHistorianMetrics(reg) logger := log.NewNopLogger() tracer := tracing.InitializeTracerForTest() + ft := featuremgmt.WithFeatures(featuremgmt.FlagAlertingNotificationHistory) cfg := setting.UnifiedAlertingNotificationHistorySettings{ Enabled: true, LokiSettings: setting.UnifiedAlertingLokiSettings{ @@ -248,7 +249,7 @@ func TestConfigureNotificationHistorian(t *testing.T) { }, } - h, err := configureNotificationHistorian(context.Background(), cfg, met, logger, tracer) + h, err := configureNotificationHistorian(context.Background(), ft, cfg, met, logger, tracer) require.NotNil(t, h) require.NoError(t, err) @@ -263,25 +264,42 @@ grafana_alerting_notification_history_info 1 }) t.Run("emit special zero metric if notification history disabled", func(t *testing.T) { - reg := prometheus.NewRegistry() - met := metrics.NewNotificationHistorianMetrics(reg) - logger := log.NewNopLogger() - tracer := tracing.InitializeTracerForTest() - cfg := setting.UnifiedAlertingNotificationHistorySettings{ - Enabled: false, + testCases := []struct { + name string + ft featuremgmt.FeatureToggles + cfg setting.UnifiedAlertingNotificationHistorySettings + }{ + { + "disabled via config", + featuremgmt.WithFeatures(featuremgmt.FlagAlertingNotificationHistory), + setting.UnifiedAlertingNotificationHistorySettings{Enabled: false}, + }, + { + "disabled via feature toggle", + featuremgmt.WithFeatures(), + setting.UnifiedAlertingNotificationHistorySettings{Enabled: true}, + }, } - h, err := configureNotificationHistorian(context.Background(), cfg, met, logger, tracer) - require.Nil(t, h) - require.NoError(t, err) - - exp := bytes.NewBufferString(` + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + reg := prometheus.NewRegistry() + met := metrics.NewNotificationHistorianMetrics(reg) + logger := log.NewNopLogger() + tracer := tracing.InitializeTracerForTest() + h, err := configureNotificationHistorian(context.Background(), tc.ft, tc.cfg, met, logger, tracer) + require.Nil(t, h) + require.NoError(t, err) + + exp := bytes.NewBufferString(` # HELP grafana_alerting_notification_history_info Information about the notification history store. # TYPE grafana_alerting_notification_history_info gauge grafana_alerting_notification_history_info 0 `) - err = testutil.GatherAndCompare(reg, exp, "grafana_alerting_notification_history_info") - require.NoError(t, err) + err = testutil.GatherAndCompare(reg, exp, "grafana_alerting_notification_history_info") + require.NoError(t, err) + }) + } }) }