Skip to content

Commit

Permalink
whitelist tenants receiving alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
santihernandezc committed Jan 24, 2025
1 parent a0e52e5 commit 91a8ea0
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/alertmanager/multitenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ type MultitenantAlertmanager struct {
tenantsDiscovered prometheus.Gauge
syncTotal *prometheus.CounterVec
syncFailures *prometheus.CounterVec

lolMtx sync.RWMutex
receivingAlerts map[string]struct{}
}

// NewMultitenantAlertmanager creates a new MultitenantAlertmanager.
Expand Down Expand Up @@ -399,6 +402,7 @@ func createMultitenantAlertmanager(cfg *MultitenantAlertmanagerConfig, fallbackC
registry: registerer,
limits: limits,
features: features,
receivingAlerts: map[string]struct{}{},
ringCheckErrors: promauto.With(registerer).NewCounter(prometheus.CounterOpts{
Name: "cortex_alertmanager_ring_check_errors_total",
Help: "Number of errors that have occurred when checking the ring for ownership.",
Expand Down Expand Up @@ -729,7 +733,13 @@ func (am *MultitenantAlertmanager) computeConfig(cfgs alertspb.AlertConfigDescs)
isMimirCfgUsable := cfgs.Mimir.RawConfig != "" && cfgs.Mimir.RawConfig != am.fallbackConfig
if am.cfg.StrictInitializationMode && !isGrafanaCfgUsable && !isMimirCfgUsable {
// Skip starting the Alertmanager if we have no usable configurations.
return amConfig{}, false, nil
am.lolMtx.RLock()
_, ok := am.receivingAlerts[cfgs.Mimir.User]
am.lolMtx.RUnlock()
if !ok {
return amConfig{}, false, nil
}
fmt.Printf("%s is receiving alerts!", cfgs.Mimir.User)
}

cfg := amConfig{
Expand Down Expand Up @@ -1014,6 +1024,12 @@ func (am *MultitenantAlertmanager) serveRequest(w http.ResponseWriter, req *http
return
}

if req.URL.Path == "/alertmanager/api/v2/alerts" && req.Method == http.MethodPost {
am.lolMtx.Lock()
am.receivingAlerts[userID] = struct{}{}
am.lolMtx.Unlock()
}

if am.fallbackConfig != "" {
userAM, err = am.alertmanagerFromFallbackConfig(req.Context(), userID)
if errors.Is(err, errNotUploadingFallback) {
Expand Down

0 comments on commit 91a8ea0

Please sign in to comment.