// Copyright The Prometheus Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package mem import ( "context" "errors" "log/slog" "sync" "time" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/common/model" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" "github.com/prometheus/alertmanager/alert" "github.com/prometheus/alertmanager/eventrecorder" "github.com/prometheus/alertmanager/featurecontrol" "github.com/prometheus/alertmanager/provider" "github.com/prometheus/alertmanager/store" "github.com/prometheus/alertmanager/tracing" ) const alertChannelLength = 200 var tracer = tracing.NewTracer("github.com/prometheus/alertmanager/provider/mem") // Alerts gives access to a set of alerts. All methods are goroutine-safe. type Alerts struct { cancel context.CancelFunc mtx sync.Mutex alerts *store.Alerts listeners map[int]listeningAlerts next int callback AlertStoreCallback logger *slog.Logger propagator propagation.TextMapPropagator recorder eventrecorder.Recorder flagger featurecontrol.Flagger alertsLimit prometheus.Gauge alertsLimitedTotal *prometheus.CounterVec subscriberChannelWrites *prometheus.CounterVec } type AlertStoreCallback interface { // PreStore is called before alert is stored into the store. If this method returns error, // alert is not stored. // Existing flag indicates whether alert has existed before (and is only updated) or not. // If alert has existed before, then alert passed to PreStore is result of merging existing alert with new alert. PreStore(alrt *alert.Alert, existing bool) error // PostStore is called after alert has been put into store. PostStore(alrt *alert.Alert, existing bool) // PostDelete is called after alert have been removed from the store due to alert garbage collection. PostDelete(alrt *alert.Alert) // PostGC is called after alerts have been removed from the store due to alert garbage collection. PostGC(fingerprints model.Fingerprints) } type listeningAlerts struct { name string alerts []chan *provider.Alert // One per shard. done chan struct{} } // shard returns the channel delivering the updates to fp. FNV-1a leaves the // low bits of a fingerprint poorly mixed, so fold the high half in first. func (l listeningAlerts) shard(fp model.Fingerprint) chan *provider.Alert { if len(l.alerts) == 1 { return l.alerts[0] } h := uint64(fp) h ^= h >> 32 return l.alerts[h%uint64(len(l.alerts))] } func (a *Alerts) registerMetrics(r prometheus.Registerer) { a.alertsLimit = promauto.With(r).NewGauge(prometheus.GaugeOpts{ Name: "alertmanager_alerts_per_alert_limit", Help: "Current limit on number of alerts per alert name", }) labels := []string{} if a.flagger.EnableAlertNamesInMetrics() { labels = append(labels, "alertname") } a.alertsLimitedTotal = promauto.With(r).NewCounterVec( prometheus.CounterOpts{ Name: "alertmanager_alerts_limited_total", Help: "Total number of alerts that were dropped due to per alert name limit", }, labels, ) a.subscriberChannelWrites = promauto.With(r).NewCounterVec( prometheus.CounterOpts{ Name: "alertmanager_alerts_subscriber_channel_writes_total", Help: "Number of times alerts were written to subscriber channels", }, []string{"subscriber"}, ) } // NewAlerts returns a new alert provider. func NewAlerts( ctx context.Context, intervalGC time.Duration, perAlertNameLimit int, alertCallback AlertStoreCallback, l *slog.Logger, recorder eventrecorder.Recorder, r prometheus.Registerer, flagger featurecontrol.Flagger, ) (*Alerts, error) { if alertCallback == nil { alertCallback = noopCallback{} } if perAlertNameLimit > 0 { l.Info("per alert name limit enabled", "limit", perAlertNameLimit) } if flagger == nil { flagger = featurecontrol.NoopFlags{} } ctx, cancel := context.WithCancel(ctx) a := &Alerts{ alerts: store.NewAlerts().WithPerAlertLimit(perAlertNameLimit), cancel: cancel, listeners: map[int]listeningAlerts{}, next: 0, logger: l.With("component", "provider"), propagator: otel.GetTextMapPropagator(), recorder: recorder, callback: alertCallback, flagger: flagger, } if r != nil { a.registerMetrics(r) a.alertsLimit.Set(float64(perAlertNameLimit)) } go a.gcLoop(ctx, intervalGC) return a, nil } func (a *Alerts) gcLoop(ctx context.Context, interval time.Duration) { t := time.NewTicker(interval) defer t.Stop() for { select { case <-ctx.Done(): return case <-t.C: a.gc() } } } func (a *Alerts) gc() { a.gcListeners() // As we don't persist alerts, we no longer consider them after // they are resolved. Alerts waiting for resolved notifications are // held in memory in aggregation groups redundantly. deleted := a.gcAlerts() // If there are no deleted alerts, there is nothing to do. if len(deleted) == 0 { return } ff := make(model.Fingerprints, len(deleted)) for i, alrt := range deleted { ff[i] = alrt.Fingerprint() a.callback.PostDelete(alrt) } a.callback.PostGC(ff) } func (a *Alerts) gcAlerts() []*alert.Alert { a.mtx.Lock() defer a.mtx.Unlock() return a.alerts.GC() } func (a *Alerts) gcListeners() { a.mtx.Lock() defer a.mtx.Unlock() for i, l := range a.listeners { select { case <-l.done: delete(a.listeners, i) for _, ch := range l.alerts { close(ch) } default: // listener is not closed yet, hence proceed. } } } // Close the alert provider. func (a *Alerts) Close() { if a.cancel != nil { a.cancel() } } // Subscribe returns an iterator over active alerts that have not been // resolved and successfully notified about. // They are not guaranteed to be in chronological order. func (a *Alerts) Subscribe(name string) provider.AlertIterator { a.mtx.Lock() defer a.mtx.Unlock() var ( done = make(chan struct{}) alerts = a.alerts.List() ch = make(chan *provider.Alert, max(len(alerts), alertChannelLength)) ) for _, a := range alerts { ch <- &provider.Alert{Data: a} } a.listeners[a.next] = listeningAlerts{name: name, alerts: []chan *provider.Alert{ch}, done: done} a.next++ return provider.NewAlertIterator(ch, done, nil) } // SlurpAndSubscribe implements provider.Alerts. Put blocks while an alert's // shard has alertChannelLength updates waiting. func (a *Alerts) SlurpAndSubscribe(name string, shards int) ([]*alert.Alert, []provider.AlertIterator) { a.mtx.Lock() defer a.mtx.Unlock() var ( done = make(chan struct{}) alerts = a.alerts.List() chs = make([]chan *provider.Alert, max(shards, 1)) ) for i := range chs { chs[i] = make(chan *provider.Alert, alertChannelLength) } a.listeners[a.next] = listeningAlerts{name: name, alerts: chs, done: done} a.next++ return alerts, provider.NewAlertIterators(chs, done) } // GetPending returns an iterator over all the alerts that have // pending notifications. func (a *Alerts) GetPending() provider.AlertIterator { var ( ch = make(chan *provider.Alert, alertChannelLength) done = make(chan struct{}) ) a.mtx.Lock() defer a.mtx.Unlock() alerts := a.alerts.List() go func() { defer close(ch) for _, a := range alerts { select { case ch <- &provider.Alert{Data: a}: case <-done: return } } }() return provider.NewAlertIterator(ch, done, nil) } // Get returns the alert for a given fingerprint. func (a *Alerts) Get(fp model.Fingerprint) (*alert.Alert, error) { a.mtx.Lock() defer a.mtx.Unlock() return a.alerts.Get(fp) } // Put adds the given alert to the set. func (a *Alerts) Put(ctx context.Context, alerts ...*alert.Alert) error { a.mtx.Lock() defer a.mtx.Unlock() ctx, span := tracer.StartAlertsPut(ctx, len(alerts)) defer span.End() for _, alrt := range alerts { fp := alrt.Fingerprint() existing := false // Check that there's an alert existing within the store before // trying to merge. if old, err := a.alerts.Get(fp); err == nil { existing = true // Merge alerts if there is an overlap in activity range. if (alrt.EndsAt.After(old.StartsAt) && alrt.EndsAt.Before(old.EndsAt)) || (alrt.StartsAt.After(old.StartsAt) && alrt.StartsAt.Before(old.EndsAt)) { alrt = old.Merge(alrt) } } if err := a.callback.PreStore(alrt, existing); err != nil { a.logger.Error("pre-store callback returned error on set alert", "err", err) continue } if err := a.alerts.Set(alrt); err != nil { a.logger.Warn("error on set alert", "alertname", alrt.Name(), "err", err) if errors.Is(err, store.ErrLimited) { labels := []string{} if a.flagger.EnableAlertNamesInMetrics() { labels = append(labels, alrt.Name()) } a.alertsLimitedTotal.WithLabelValues(labels...).Inc() } continue } a.callback.PostStore(alrt, existing) if !existing { a.recorder.RecordEvent(ctx, func() eventrecorder.EventData { return eventrecorder.NewAlertCreatedEvent(alrt) }) } msg := &provider.Alert{Data: alrt} // Only carry propagation headers when tracing is on: allocating and // filling the map for every alert is wasted work otherwise, and // consumers treat a nil Header as "nothing to extract". if tracing.Enabled() { metadata := map[string]string{} a.propagator.Inject(ctx, propagation.MapCarrier(metadata)) msg.Header = metadata } for _, l := range a.listeners { select { case l.shard(fp) <- msg: a.subscriberChannelWrites.WithLabelValues(l.name).Inc() case <-l.done: } } } return nil } type noopCallback struct{} func (n noopCallback) PreStore(_ *alert.Alert, _ bool) error { return nil } func (n noopCallback) PostStore(_ *alert.Alert, _ bool) {} func (n noopCallback) PostDelete(_ *alert.Alert) {} func (n noopCallback) PostGC(_ model.Fingerprints) {}