mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Move the unified alerting settings to its own struct (#39350)
This commit is contained in:
parent
f3002931f4
commit
2ad82b9354
@ -122,7 +122,7 @@ func (ng *AlertNG) init() error {
|
||||
OrgStore: store,
|
||||
MultiOrgNotifier: ng.MultiOrgAlertmanager,
|
||||
Metrics: ng.Metrics.GetSchedulerMetrics(),
|
||||
AdminConfigPollInterval: ng.Cfg.AdminConfigPollInterval,
|
||||
AdminConfigPollInterval: ng.Cfg.UnifiedAlerting.AdminConfigPollInterval,
|
||||
}
|
||||
stateManager := state.NewManager(ng.Log, ng.Metrics.GetStateMetrics(), store, store)
|
||||
schedule := schedule.NewScheduler(schedCfg, ng.DataService, ng.Cfg.AppURL, stateManager)
|
||||
|
@ -133,7 +133,7 @@ func newAlertmanager(orgID int64, cfg *setting.Cfg, store store.AlertingStore, k
|
||||
dispatcherMetrics: dispatch.NewDispatcherMetrics(false, m.Registerer),
|
||||
Store: store,
|
||||
peer: peer,
|
||||
peerTimeout: cfg.HAPeerTimeout,
|
||||
peerTimeout: cfg.UnifiedAlerting.HAPeerTimeout,
|
||||
Metrics: m,
|
||||
orgID: orgID,
|
||||
}
|
||||
|
@ -55,16 +55,16 @@ func NewMultiOrgAlertmanager(cfg *setting.Cfg, configStore store.AlertingStore,
|
||||
|
||||
clusterLogger := gokit_log.With(gokit_log.NewLogfmtLogger(logging.NewWrapper(l)), "component", "cluster")
|
||||
moa.peer = &NilPeer{}
|
||||
if len(cfg.HAPeers) > 0 {
|
||||
if len(cfg.UnifiedAlerting.HAPeers) > 0 {
|
||||
peer, err := cluster.Create(
|
||||
clusterLogger,
|
||||
m.Registerer,
|
||||
cfg.HAListenAddr,
|
||||
cfg.HAAdvertiseAddr,
|
||||
cfg.HAPeers, // peers
|
||||
cfg.UnifiedAlerting.HAListenAddr,
|
||||
cfg.UnifiedAlerting.HAAdvertiseAddr,
|
||||
cfg.UnifiedAlerting.HAPeers, // peers
|
||||
true,
|
||||
cfg.HAPushPullInterval,
|
||||
cfg.HAGossipInterval,
|
||||
cfg.UnifiedAlerting.HAPushPullInterval,
|
||||
cfg.UnifiedAlerting.HAGossipInterval,
|
||||
cluster.DefaultTcpTimeout,
|
||||
cluster.DefaultProbeTimeout,
|
||||
cluster.DefaultProbeInterval,
|
||||
@ -98,7 +98,7 @@ func (moa *MultiOrgAlertmanager) Run(ctx context.Context) error {
|
||||
case <-ctx.Done():
|
||||
moa.StopAndWait()
|
||||
return nil
|
||||
case <-time.After(moa.settings.AlertmanagerConfigPollInterval):
|
||||
case <-time.After(moa.settings.UnifiedAlerting.AlertmanagerConfigPollInterval):
|
||||
if err := moa.LoadAndSyncAlertmanagersForOrgs(ctx); err != nil {
|
||||
moa.logger.Error("error while synchronizing Alertmanager orgs", "err", err)
|
||||
}
|
||||
|
@ -32,8 +32,8 @@ func TestMultiOrgAlertmanager_SyncAlertmanagersForOrgs(t *testing.T) {
|
||||
reg := prometheus.NewPedanticRegistry()
|
||||
m := metrics.NewNGAlert(reg)
|
||||
cfg := &setting.Cfg{
|
||||
DataPath: tmpDir,
|
||||
AlertmanagerConfigPollInterval: 3 * time.Minute, // do not poll in tests
|
||||
DataPath: tmpDir,
|
||||
UnifiedAlerting: setting.UnifiedAlertingSettings{AlertmanagerConfigPollInterval: 3 * time.Minute}, // do not poll in tests.
|
||||
}
|
||||
mam, err := NewMultiOrgAlertmanager(cfg, configStore, orgStore, kvStore, m.GetMultiOrgAlertmanagerMetrics(), log.New("testlogger"))
|
||||
require.NoError(t, err)
|
||||
@ -94,8 +94,8 @@ func TestMultiOrgAlertmanager_AlertmanagerFor(t *testing.T) {
|
||||
tmpDir, err := ioutil.TempDir("", "test")
|
||||
require.NoError(t, err)
|
||||
cfg := &setting.Cfg{
|
||||
DataPath: tmpDir,
|
||||
AlertmanagerConfigPollInterval: 3 * time.Minute, // do not poll in tests
|
||||
DataPath: tmpDir,
|
||||
UnifiedAlerting: setting.UnifiedAlertingSettings{AlertmanagerConfigPollInterval: 3 * time.Minute}, // do not poll in tests.
|
||||
}
|
||||
kvStore := newFakeKVStore(t)
|
||||
reg := prometheus.NewPedanticRegistry()
|
||||
|
@ -419,14 +419,7 @@ type Cfg struct {
|
||||
GeomapEnableCustomBaseLayers bool
|
||||
|
||||
// Unified Alerting
|
||||
AdminConfigPollInterval time.Duration
|
||||
AlertmanagerConfigPollInterval time.Duration
|
||||
HAListenAddr string
|
||||
HAAdvertiseAddr string
|
||||
HAPeers []string
|
||||
HAPeerTimeout time.Duration
|
||||
HAGossipInterval time.Duration
|
||||
HAPushPullInterval time.Duration
|
||||
UnifiedAlerting UnifiedAlertingSettings
|
||||
}
|
||||
|
||||
// IsLiveConfigEnabled returns true if live should be able to save configs to SQL tables
|
||||
|
@ -19,39 +19,51 @@ const (
|
||||
AlertmanagerDefaultConfigPollInterval = 60 * time.Second
|
||||
)
|
||||
|
||||
type UnifiedAlertingSettings struct {
|
||||
AdminConfigPollInterval time.Duration
|
||||
AlertmanagerConfigPollInterval time.Duration
|
||||
HAListenAddr string
|
||||
HAAdvertiseAddr string
|
||||
HAPeers []string
|
||||
HAPeerTimeout time.Duration
|
||||
HAGossipInterval time.Duration
|
||||
HAPushPullInterval time.Duration
|
||||
}
|
||||
|
||||
func (cfg *Cfg) ReadUnifiedAlertingSettings(iniFile *ini.File) error {
|
||||
uaCfg := UnifiedAlertingSettings{}
|
||||
ua := iniFile.Section("unified_alerting")
|
||||
var err error
|
||||
cfg.AdminConfigPollInterval, err = gtime.ParseDuration(valueAsString(ua, "admin_config_poll_interval", (SchedulerDefaultAdminConfigPollInterval).String()))
|
||||
uaCfg.AdminConfigPollInterval, err = gtime.ParseDuration(valueAsString(ua, "admin_config_poll_interval", (SchedulerDefaultAdminConfigPollInterval).String()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfg.AlertmanagerConfigPollInterval, err = gtime.ParseDuration(valueAsString(ua, "alertmanager_config_poll_interval", (AlertmanagerDefaultConfigPollInterval).String()))
|
||||
uaCfg.AlertmanagerConfigPollInterval, err = gtime.ParseDuration(valueAsString(ua, "alertmanager_config_poll_interval", (AlertmanagerDefaultConfigPollInterval).String()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfg.HAPeerTimeout, err = gtime.ParseDuration(valueAsString(ua, "ha_peer_timeout", (AlertmanagerDefaultPeerTimeout).String()))
|
||||
uaCfg.HAPeerTimeout, err = gtime.ParseDuration(valueAsString(ua, "ha_peer_timeout", (AlertmanagerDefaultPeerTimeout).String()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfg.HAGossipInterval, err = gtime.ParseDuration(valueAsString(ua, "ha_gossip_interval", (AlertmanagerDefaultGossipInterval).String()))
|
||||
uaCfg.HAGossipInterval, err = gtime.ParseDuration(valueAsString(ua, "ha_gossip_interval", (AlertmanagerDefaultGossipInterval).String()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfg.HAPushPullInterval, err = gtime.ParseDuration(valueAsString(ua, "ha_push_pull_interval", (AlertmanagerDefaultPushPullInterval).String()))
|
||||
uaCfg.HAPushPullInterval, err = gtime.ParseDuration(valueAsString(ua, "ha_push_pull_interval", (AlertmanagerDefaultPushPullInterval).String()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfg.HAListenAddr = ua.Key("ha_listen_address").MustString(AlertmanagerDefaultClusterAddr)
|
||||
cfg.HAAdvertiseAddr = ua.Key("ha_advertise_address").MustString("")
|
||||
uaCfg.HAListenAddr = ua.Key("ha_listen_address").MustString(AlertmanagerDefaultClusterAddr)
|
||||
uaCfg.HAAdvertiseAddr = ua.Key("ha_advertise_address").MustString("")
|
||||
peers := ua.Key("ha_peers").MustString("")
|
||||
cfg.HAPeers = make([]string, 0)
|
||||
uaCfg.HAPeers = make([]string, 0)
|
||||
if peers != "" {
|
||||
for _, peer := range strings.Split(peers, ",") {
|
||||
peer = strings.TrimSpace(peer)
|
||||
cfg.HAPeers = append(cfg.HAPeers, peer)
|
||||
uaCfg.HAPeers = append(uaCfg.HAPeers, peer)
|
||||
}
|
||||
}
|
||||
|
||||
cfg.UnifiedAlerting = uaCfg
|
||||
return nil
|
||||
}
|
||||
|
@ -14,26 +14,26 @@ func TestCfg_ReadUnifiedAlertingSettings(t *testing.T) {
|
||||
|
||||
// It sets the correct defaults.
|
||||
{
|
||||
require.Equal(t, 60*time.Second, cfg.AdminConfigPollInterval)
|
||||
require.Equal(t, 60*time.Second, cfg.AlertmanagerConfigPollInterval)
|
||||
require.Equal(t, 15*time.Second, cfg.HAPeerTimeout)
|
||||
require.Equal(t, "0.0.0.0:9094", cfg.HAListenAddr)
|
||||
require.Equal(t, "", cfg.HAAdvertiseAddr)
|
||||
require.Len(t, cfg.HAPeers, 0)
|
||||
require.Equal(t, 200*time.Millisecond, cfg.HAGossipInterval)
|
||||
require.Equal(t, 60*time.Second, cfg.HAPushPullInterval)
|
||||
require.Equal(t, 60*time.Second, cfg.UnifiedAlerting.AdminConfigPollInterval)
|
||||
require.Equal(t, 60*time.Second, cfg.UnifiedAlerting.AlertmanagerConfigPollInterval)
|
||||
require.Equal(t, 15*time.Second, cfg.UnifiedAlerting.HAPeerTimeout)
|
||||
require.Equal(t, "0.0.0.0:9094", cfg.UnifiedAlerting.HAListenAddr)
|
||||
require.Equal(t, "", cfg.UnifiedAlerting.HAAdvertiseAddr)
|
||||
require.Len(t, cfg.UnifiedAlerting.HAPeers, 0)
|
||||
require.Equal(t, 200*time.Millisecond, cfg.UnifiedAlerting.HAGossipInterval)
|
||||
require.Equal(t, 60*time.Second, cfg.UnifiedAlerting.HAPushPullInterval)
|
||||
}
|
||||
|
||||
// With peers set, it correctly parses them.
|
||||
{
|
||||
require.Len(t, cfg.HAPeers, 0)
|
||||
require.Len(t, cfg.UnifiedAlerting.HAPeers, 0)
|
||||
s, err := cfg.Raw.NewSection("unified_alerting")
|
||||
require.NoError(t, err)
|
||||
_, err = s.NewKey("ha_peers", "hostname1:9090,hostname2:9090,hostname3:9090")
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, cfg.ReadUnifiedAlertingSettings(cfg.Raw))
|
||||
require.Len(t, cfg.HAPeers, 3)
|
||||
require.ElementsMatch(t, []string{"hostname1:9090", "hostname2:9090", "hostname3:9090"}, cfg.HAPeers)
|
||||
require.Len(t, cfg.UnifiedAlerting.HAPeers, 3)
|
||||
require.ElementsMatch(t, []string{"hostname1:9090", "hostname2:9090", "hostname3:9090"}, cfg.UnifiedAlerting.HAPeers)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user