ApplyConfig applies a new configuration to an Alertmanager.
(userID string, conf *config.Config, rawCfg string)
| 353 | |
| 354 | // ApplyConfig applies a new configuration to an Alertmanager. |
| 355 | func (am *Alertmanager) ApplyConfig(userID string, conf *config.Config, rawCfg string) error { |
| 356 | templateFiles := make([]string, len(conf.Templates)) |
| 357 | for i, t := range conf.Templates { |
| 358 | templateFilepath, err := safeTemplateFilepath(filepath.Join(am.cfg.TenantDataDir, templatesDir), t) |
| 359 | if err != nil { |
| 360 | return err |
| 361 | } |
| 362 | |
| 363 | templateFiles[i] = templateFilepath |
| 364 | } |
| 365 | |
| 366 | tmpl, err := template.FromGlobs(templateFiles) |
| 367 | if err != nil { |
| 368 | return err |
| 369 | } |
| 370 | tmpl.ExternalURL = am.cfg.ExternalURL |
| 371 | |
| 372 | am.api.Update(conf, func(_ context.Context, _ model.LabelSet) {}) |
| 373 | |
| 374 | // Ensure inhibitor is set before being called |
| 375 | if am.inhibitor != nil { |
| 376 | am.inhibitor.Stop() |
| 377 | } |
| 378 | |
| 379 | // Ensure dispatcher is set before being called |
| 380 | if am.dispatcher != nil { |
| 381 | am.dispatcher.Stop() |
| 382 | } |
| 383 | |
| 384 | am.inhibitor = inhibit.NewInhibitor(am.alerts, conf.InhibitRules, am.alertMarker, util_log.GoKitLogToSlog(log.With(am.logger, "component", "inhibitor"))) |
| 385 | |
| 386 | waitFunc := clusterWait(am.state.Position, am.cfg.PeerTimeout) |
| 387 | |
| 388 | timeoutFunc := func(d time.Duration) time.Duration { |
| 389 | if d < notify.MinTimeout { |
| 390 | d = notify.MinTimeout |
| 391 | } |
| 392 | return d + waitFunc() |
| 393 | } |
| 394 | |
| 395 | // Create a firewall binded to the per-tenant config. |
| 396 | firewallDialer := util_net.NewFirewallDialer(newFirewallDialerConfigProvider(userID, am.cfg.Limits)) |
| 397 | |
| 398 | integrationsMap, err := buildIntegrationsMap(conf.Receivers, tmpl, firewallDialer, am.logger, func(integrationName string, notifier notify.Notifier) notify.Notifier { |
| 399 | if am.cfg.Limits != nil { |
| 400 | rl := &tenantRateLimits{ |
| 401 | tenant: userID, |
| 402 | limits: am.cfg.Limits, |
| 403 | integration: integrationName, |
| 404 | } |
| 405 | |
| 406 | return newRateLimitedNotifier(notifier, rl, 10*time.Second, am.rateLimitedNotifications.WithLabelValues(integrationName)) |
| 407 | } |
| 408 | return notifier |
| 409 | }) |
| 410 | if err != nil { |
| 411 | return err |
| 412 | } |