Only register limit metrics when they are used.

Limits are not used in standalone alertmanager.

Signed-off-by: Peter Štibraný <pstibrany@gmail.com>
Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
This commit is contained in:
Peter Štibraný 2021-05-20 08:49:16 +02:00 committed by Peter Štibraný
parent 390474ffbe
commit d5ed7bfb15
3 changed files with 9 additions and 6 deletions

View File

@ -383,7 +383,7 @@ func run() int {
tmpl *template.Template
)
dispMetrics := dispatch.NewDispatcherMetrics(prometheus.DefaultRegisterer)
dispMetrics := dispatch.NewDispatcherMetrics(false, prometheus.DefaultRegisterer)
pipelineBuilder := notify.NewPipelineBuilder(prometheus.DefaultRegisterer)
configLogger := log.With(logger, "component", "configuration")
configCoordinator := config.NewCoordinator(

View File

@ -39,7 +39,7 @@ type DispatcherMetrics struct {
}
// NewDispatcherMetrics returns a new registered DispatchMetrics.
func NewDispatcherMetrics(r prometheus.Registerer) *DispatcherMetrics {
func NewDispatcherMetrics(registerLimitMetrics bool, r prometheus.Registerer) *DispatcherMetrics {
m := DispatcherMetrics{
aggrGroups: prometheus.NewGauge(
prometheus.GaugeOpts{
@ -62,7 +62,10 @@ func NewDispatcherMetrics(r prometheus.Registerer) *DispatcherMetrics {
}
if r != nil {
r.MustRegister(m.aggrGroups, m.processingDuration, m.aggrGroupLimitReached)
r.MustRegister(m.aggrGroups, m.processingDuration)
if registerLimitMetrics {
r.MustRegister(m.aggrGroupLimitReached)
}
}
return &m

View File

@ -375,7 +375,7 @@ route:
timeout := func(d time.Duration) time.Duration { return time.Duration(0) }
recorder := &recordStage{alerts: make(map[string]map[model.Fingerprint]*types.Alert)}
lim := limits{groups: 6}
m := NewDispatcherMetrics(prometheus.NewRegistry())
m := NewDispatcherMetrics(true, prometheus.NewRegistry())
dispatcher := NewDispatcher(alerts, route, recorder, marker, timeout, lim, logger, m)
go dispatcher.Run()
defer dispatcher.Stop()
@ -564,7 +564,7 @@ func TestDispatcherRace(t *testing.T) {
defer alerts.Close()
timeout := func(d time.Duration) time.Duration { return time.Duration(0) }
dispatcher := NewDispatcher(alerts, nil, nil, marker, timeout, nil, logger, NewDispatcherMetrics(prometheus.NewRegistry()))
dispatcher := NewDispatcher(alerts, nil, nil, marker, timeout, nil, logger, NewDispatcherMetrics(false, prometheus.NewRegistry()))
go dispatcher.Run()
dispatcher.Stop()
}
@ -592,7 +592,7 @@ func TestDispatcherRaceOnFirstAlertNotDeliveredWhenGroupWaitIsZero(t *testing.T)
timeout := func(d time.Duration) time.Duration { return d }
recorder := &recordStage{alerts: make(map[string]map[model.Fingerprint]*types.Alert)}
dispatcher := NewDispatcher(alerts, route, recorder, marker, timeout, nil, logger, NewDispatcherMetrics(prometheus.NewRegistry()))
dispatcher := NewDispatcher(alerts, route, recorder, marker, timeout, nil, logger, NewDispatcherMetrics(false, prometheus.NewRegistry()))
go dispatcher.Run()
defer dispatcher.Stop()