Skip to content

Commit

Permalink
improve redability in computeConfig()
Browse files Browse the repository at this point in the history
  • Loading branch information
santihernandezc committed Feb 28, 2025
1 parent 51524d0 commit 85a54d5
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions pkg/alertmanager/multitenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,16 +752,17 @@ func (am *MultitenantAlertmanager) computeConfig(cfgs alertspb.AlertConfigDescs)
AlertConfigDesc: cfgs.Mimir,
tmplExternalURL: am.cfg.ExternalURL.URL,
}
strictInit := am.cfg.GrafanaAlertmanagerTenantSuffix != "" && strings.HasSuffix(cfgs.Mimir.User, am.cfg.GrafanaAlertmanagerTenantSuffix)

// A Grafana configuration is considered usable if it's promoted, non-default, and not empty.
if !cfgs.Grafana.Promoted || cfgs.Grafana.Default || cfgs.Grafana.RawConfig == "" {
if !strictInit {
// Check if this tenant can be skipped (Grafana suffix matching).
skippable := am.canSkipTenant(cfgs.Mimir.User)

// If Grafana config is not usable, skip if possible.
if !isGrafanaConfigUsable(cfgs.Grafana) {
if !skippable {
return cfg, true, nil
}

// If the tenant ID matches the configured Grafana suffix, only run the Alertmanager if it's receiving requests.
// If there's no value, store a zero-value timestamp to indicate that we've skipped the tenant.
// For skippable tenants, only run if receiving requests recently.
createdAt, loaded := am.lastRequestTime.LoadOrStore(cfgs.Mimir.User, time.Time{}.Unix())
if !loaded {
return cfg, false, nil
Expand All @@ -775,14 +776,13 @@ func (am *MultitenantAlertmanager) computeConfig(cfgs alertspb.AlertConfigDescs)
return cfg, true, nil
}

// If the Alertmanager was previously skipped but now has a usable configuration, remove it from the skipped list.
if strictInit {
// Clear any previous skipped status since we now have a usable config.
if skippable {
if _, ok := am.lastRequestTime.LoadAndDelete(cfgs.Mimir.User); ok {
level.Debug(am.logger).Log("msg", "user now has a usable config, removing it from skipped list", "user", cfgs.Mimir.User)
}
}

// If the Mimir configuration is either default or empty, use the Grafana configuration.
if cfgs.Mimir.RawConfig == am.fallbackConfig || cfgs.Mimir.RawConfig == "" {
level.Debug(am.logger).Log("msg", "using grafana config with the default globals", "user", cfgs.Mimir.User)
cfg, err := am.createUsableGrafanaConfig(cfgs.Grafana, am.fallbackConfig)
Expand All @@ -793,6 +793,16 @@ func (am *MultitenantAlertmanager) computeConfig(cfgs alertspb.AlertConfigDescs)
return cfg, true, nil
}

// isGrafanaConfigUsable returns true if the Grafana configuration is promoted, non-default, and not empty.
func isGrafanaConfigUsable(cfg alertspb.GrafanaAlertConfigDesc) bool {
return cfg.Promoted && cfg.Default && cfg.RawConfig == ""
}

// canSkipTenant returns true if the tenant can be skipped (Grafana suffix matching).
func (am *MultitenantAlertmanager) canSkipTenant(userID string) bool {
return am.cfg.GrafanaAlertmanagerTenantSuffix != "" && strings.HasSuffix(userID, am.cfg.GrafanaAlertmanagerTenantSuffix)
}

// syncStates promotes/unpromotes the Grafana state and updates the 'promoted' flag if needed.
func (am *MultitenantAlertmanager) syncStates(ctx context.Context, cfg amConfig) error {
// fetching grafana state first so we can register its size independently of it being promoted or not
Expand Down

0 comments on commit 85a54d5

Please sign in to comment.