Repository: prometheus/alertmanager
File: provider/mem/mem.go (Go)

Code before:
func (a *Alerts) registerMetrics(r prometheus.Registerer) {
	newMemAlertByStatus := func(s types.AlertState) prometheus.GaugeFunc {
		return prometheus.NewGaugeFunc(
			prometheus.GaugeOpts{
				Name:        "alertmanager_alerts",
				Help:        "How many alerts by state.",
				ConstLabels: prometheus.Labels{"state": string(s)},
			},
			func() float64 {
				return float64(a.count(s))
			},
		)
	}

	r.MustRegister(newMemAlertByStatus(types.AlertStateActive))
	r.MustRegister(newMemAlertByStatus(types.AlertStateSuppressed))
	r.MustRegister(newMemAlertByStatus(types.AlertStateUnprocessed))

	a.subscriberChannelWrites = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "alertmanager_alerts_subscriber_channel_writes_total",
			Help: "Number of times alerts were written to subscriber channels",
		},
		[]string{"subscriber"},
	)
	r.MustRegister(a.subscriberChannelWrites)
}

// NewAlerts returns a new alert provider.
func NewAlerts(ctx context.Context, m types.AlertMarker, intervalGC time.Duration, alertCallback AlertStoreCallback, l *slog.Logger, r prometheus.Registerer) (*Alerts, error) {
	if alertCallback == nil {
		alertCallback = noopCallback{}
	}

	ctx, cancel := context.WithCancel(ctx)
	a := &Alerts{
		marker:    m,
		alerts:    store.NewAlerts(),
		cancel:    cancel,
		listeners: map[int]listeningAlerts{},
		next:      0,
		logger:    l.With("component", "provider"),
		callback:  alertCallback,
	}

	if r != nil {
		a.registerMetrics(r)
	}

	go a.gcLoop(ctx, intervalGC)


Review comment (suggestion):
Maybe we can start using `github.com/prometheus/client_golang/prometheus/promauto`?

```suggestion
	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"},
	)
```

Code after:

func (a *Alerts) registerMetrics(r prometheus.Registerer) {
	newMemAlertByStatus := func(s types.AlertState) prometheus.GaugeFunc {
		return promauto.With(r).NewGaugeFunc(
			prometheus.GaugeOpts{
				Name:        "alertmanager_alerts",
				Help:        "How many alerts by state.",
				ConstLabels: prometheus.Labels{"state": string(s)},
			},
			func() float64 {
				return float64(a.count(s))
			},
		)
	}

	newMemAlertByStatus(types.AlertStateActive)
	newMemAlertByStatus(types.AlertStateSuppressed)
	newMemAlertByStatus(types.AlertStateUnprocessed)

	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, m types.AlertMarker, intervalGC time.Duration, alertCallback AlertStoreCallback, l *slog.Logger, r prometheus.Registerer) (*Alerts, error) {
	if alertCallback == nil {
		alertCallback = noopCallback{}
	}

	ctx, cancel := context.WithCancel(ctx)
	a := &Alerts{
		marker:    m,
		alerts:    store.NewAlerts(),
		cancel:    cancel,
		listeners: map[int]listeningAlerts{},
		next:      0,
		logger:    l.With("component", "provider"),
		callback:  alertCallback,
	}

	if r != nil {
		a.registerMetrics(r)
	}

	go a.gcLoop(ctx, intervalGC)