From 34ac5059c1e5d7a02acb9408dafc4491828a2b98 Mon Sep 17 00:00:00 2001 From: Steve Simpson Date: Wed, 3 Dec 2025 10:39:54 +0100 Subject: [PATCH] Alerting: Allow missing NGAlert in historian.alerting app (#114750) --- apps/alerting/historian/pkg/app/app.go | 26 ++++++++++++------- .../apps/alerting/historian/register.go | 22 +++++++++------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/apps/alerting/historian/pkg/app/app.go b/apps/alerting/historian/pkg/app/app.go index e34e03c9044..2996a21a231 100644 --- a/apps/alerting/historian/pkg/app/app.go +++ b/apps/alerting/historian/pkg/app/app.go @@ -16,6 +16,12 @@ import ( func New(cfg app.Config) (app.App, error) { runtimeConfig := cfg.SpecificConfig.(config.RuntimeConfig) + alertStateHandler := runtimeConfig.GetAlertStateHistoryHandler + if alertStateHandler == nil { + alertStateHandler = NewErrorHandler("no alert state handler") + } + notificationHandler := NewErrorHandler("unimplemented") + simpleConfig := simple.AppConfig{ Name: "alerting.historian", KubeConfig: cfg.KubeConfig, @@ -25,12 +31,12 @@ func New(cfg app.Config) (app.App, error) { Namespaced: true, Path: "/alertstate/history", Method: "GET", - }: runtimeConfig.GetAlertStateHistoryHandler, + }: alertStateHandler, { Namespaced: true, Path: "/notification/query", Method: "POST", - }: UnimplementedHandler, + }: notificationHandler, }, }, // TODO: Remove when SDK is fixed. @@ -54,12 +60,14 @@ func New(cfg app.Config) (app.App, error) { return a, nil } -func UnimplementedHandler(ctx context.Context, writer app.CustomRouteResponseWriter, request *app.CustomRouteRequest) error { - return &apierrors.StatusError{ - ErrStatus: metav1.Status{ - Status: metav1.StatusFailure, - Code: http.StatusUnprocessableEntity, - Message: "unimplemented", - }, +func NewErrorHandler(message string) simple.AppCustomRouteHandler { + return func(ctx context.Context, writer app.CustomRouteResponseWriter, request *app.CustomRouteRequest) error { + return &apierrors.StatusError{ + ErrStatus: metav1.Status{ + Status: metav1.StatusFailure, + Code: http.StatusUnprocessableEntity, + Message: message, + }, + } } } diff --git a/pkg/registry/apps/alerting/historian/register.go b/pkg/registry/apps/alerting/historian/register.go index fb0e90e7062..78312c326aa 100644 --- a/pkg/registry/apps/alerting/historian/register.go +++ b/pkg/registry/apps/alerting/historian/register.go @@ -26,19 +26,21 @@ func RegisterAppInstaller( cfg *setting.Cfg, ng *ngalert.AlertNG, ) (*AlertingHistorianAppInstaller, error) { - if ng.IsDisabled() { - log.New("app-registry").Info("Skipping Kubernetes Alerting Historian apiserver (historian.alerting.grafana.app): Unified Alerting is disabled") - return nil, nil - } - installer := &AlertingHistorianAppInstaller{} + appSpecificConfig := historianAppConfig.RuntimeConfig{} - handlers := &handlers{ - historian: ng.Api.Historian, - } + // If we're provided an AlertNG, then call back into that for things we need. + // This is a temporary whilst building out the app; we should not depend on it. + if ng != nil { + if ng.IsDisabled() { + log.New("app-registry").Info("Skipping Kubernetes Alerting Historian apiserver (historian.alerting.grafana.app): Unified Alerting is disabled") + return nil, nil + } - appSpecificConfig := historianAppConfig.RuntimeConfig{ - GetAlertStateHistoryHandler: handlers.GetAlertStateHistoryHandler, + handlers := &handlers{ + historian: ng.Api.Historian, + } + appSpecificConfig.GetAlertStateHistoryHandler = handlers.GetAlertStateHistoryHandler } provider := simple.NewAppProvider(apis.LocalManifest(), appSpecificConfig, historianApp.New)