Remote Alertmanager: Use the correct OrgID when creating the store (#111634)

* Remote Alertmanager: Use the correct OrgID when creating the store

* fix test
This commit is contained in:
Santiago 2025-09-25 18:53:07 +02:00 committed by GitHub
parent 6629021f36
commit dab39c873f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 9 additions and 8 deletions

View File

@ -222,7 +222,6 @@ func (ng *AlertNG) init() error {
autogenFn := func(ctx context.Context, logger log.Logger, orgID int64, cfg *definitions.PostableApiAlertingConfig, skipInvalid bool) error { 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) return notifier.AddAutogenConfig(ctx, logger, ng.store, orgID, cfg, skipInvalid)
} }
store := notifier.NewFileStore(cfg.OrgID, ng.KVStore)
// This function will be used by the MOA to create new Alertmanagers. // This function will be used by the MOA to create new Alertmanagers.
var override func(notifier.OrgAlertmanagerFactory) notifier.OrgAlertmanagerFactory var override func(notifier.OrgAlertmanagerFactory) notifier.OrgAlertmanagerFactory
@ -230,12 +229,12 @@ func (ng *AlertNG) init() error {
if remotePrimary { if remotePrimary {
ng.Log.Debug("Starting Grafana with remote primary mode enabled") ng.Log.Debug("Starting Grafana with remote primary mode enabled")
m.Info.WithLabelValues(metrics.ModeRemotePrimary).Set(1) m.Info.WithLabelValues(metrics.ModeRemotePrimary).Set(1)
override = remote.NewRemotePrimaryFactory(cfg, store, crypto, autogenFn, m, ng.tracer) override = remote.NewRemotePrimaryFactory(cfg, ng.KVStore, crypto, autogenFn, m, ng.tracer)
} else { } else {
ng.Log.Debug("Starting Grafana with remote secondary mode enabled") ng.Log.Debug("Starting Grafana with remote secondary mode enabled")
m.Info.WithLabelValues(metrics.ModeRemoteSecondary).Set(1) m.Info.WithLabelValues(metrics.ModeRemoteSecondary).Set(1)
override = remote.NewRemoteSecondaryFactory(cfg, override = remote.NewRemoteSecondaryFactory(cfg,
store, ng.KVStore,
ng.store, ng.store,
ng.Cfg.UnifiedAlerting.RemoteAlertmanager.SyncInterval, ng.Cfg.UnifiedAlerting.RemoteAlertmanager.SyncInterval,
crypto, crypto,

View File

@ -60,7 +60,7 @@ func TestMultiorgAlertmanager_RemoteSecondaryMode(t *testing.T) {
} }
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore()) secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())
override := remote.NewRemoteSecondaryFactory(remoteAMCfg, override := remote.NewRemoteSecondaryFactory(remoteAMCfg,
notifier.NewFileStore(remoteAMCfg.OrgID, kvStore), kvStore,
configStore, configStore,
10*time.Second, 10*time.Second,
notifier.NewCrypto(secretsService, configStore, log.NewNopLogger()), notifier.NewCrypto(secretsService, configStore, log.NewNopLogger()),

View File

@ -7,6 +7,7 @@ import (
alertingNotify "github.com/grafana/alerting/notify" alertingNotify "github.com/grafana/alerting/notify"
"github.com/grafana/grafana/pkg/infra/kvstore"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing" "github.com/grafana/grafana/pkg/infra/tracing"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
@ -25,7 +26,7 @@ type RemotePrimaryForkedAlertmanager struct {
// NewRemotePrimaryFactory returns a function to override the default AM factory in the multi-org Alertmanager. // NewRemotePrimaryFactory returns a function to override the default AM factory in the multi-org Alertmanager.
func NewRemotePrimaryFactory( func NewRemotePrimaryFactory(
cfg AlertmanagerConfig, cfg AlertmanagerConfig,
store stateStore, store kvstore.KVStore,
crypto Crypto, crypto Crypto,
autogenFn AutogenFn, autogenFn AutogenFn,
m *metrics.RemoteAlertmanager, m *metrics.RemoteAlertmanager,
@ -43,7 +44,7 @@ func NewRemotePrimaryFactory(
cfg.OrgID = orgID cfg.OrgID = orgID
cfg.PromoteConfig = true cfg.PromoteConfig = true
l := log.New("ngalert.forked-alertmanager.remote-primary") l := log.New("ngalert.forked-alertmanager.remote-primary")
remoteAM, err := NewAlertmanager(ctx, cfg, store, crypto, autogenFn, m, t) remoteAM, err := NewAlertmanager(ctx, cfg, notifier.NewFileStore(cfg.OrgID, store), crypto, autogenFn, m, t)
if err != nil { if err != nil {
l.Error("Failed to create remote Alertmanager, falling back to using only the internal one", "err", err) l.Error("Failed to create remote Alertmanager, falling back to using only the internal one", "err", err)
return internalAM, nil return internalAM, nil

View File

@ -8,6 +8,7 @@ import (
alertingNotify "github.com/grafana/alerting/notify" alertingNotify "github.com/grafana/alerting/notify"
"github.com/grafana/grafana/pkg/infra/kvstore"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing" "github.com/grafana/grafana/pkg/infra/tracing"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
@ -65,7 +66,7 @@ func (c *RemoteSecondaryConfig) Validate() error {
// NewRemoteSecondaryFactory returns a function to override the default AM factory in the multi-org Alertmanager. // NewRemoteSecondaryFactory returns a function to override the default AM factory in the multi-org Alertmanager.
func NewRemoteSecondaryFactory( func NewRemoteSecondaryFactory(
cfg AlertmanagerConfig, cfg AlertmanagerConfig,
stateStore stateStore, store kvstore.KVStore,
cfgStore configStore, cfgStore configStore,
syncInterval time.Duration, syncInterval time.Duration,
crypto Crypto, crypto Crypto,
@ -79,7 +80,7 @@ func NewRemoteSecondaryFactory(
// Create the remote Alertmanager first so we don't need to unregister internal AM metrics if this fails. // Create the remote Alertmanager first so we don't need to unregister internal AM metrics if this fails.
cfg.OrgID = orgID cfg.OrgID = orgID
l := log.New("ngalert.forked-alertmanager.remote-secondary") l := log.New("ngalert.forked-alertmanager.remote-secondary")
remoteAM, err := NewAlertmanager(ctx, cfg, stateStore, crypto, autogenFn, m, t) remoteAM, err := NewAlertmanager(ctx, cfg, notifier.NewFileStore(cfg.OrgID, store), crypto, autogenFn, m, t)
if err != nil && withRemoteState { if err != nil && withRemoteState {
// We can't start the internal Alertmanager without the remote state. // We can't start the internal Alertmanager without the remote state.
return nil, fmt.Errorf("failed to create remote Alertmanager, can't start the internal Alertmanager without the remote state: %w", err) return nil, fmt.Errorf("failed to create remote Alertmanager, can't start the internal Alertmanager without the remote state: %w", err)