From d5ed7bfb150ed94d4d825e8110bef5b86c0f8ebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20S=CC=8Ctibrany=CC=81?= Date: Thu, 20 May 2021 08:49:16 +0200 Subject: [PATCH] Only register limit metrics when they are used. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Limits are not used in standalone alertmanager. Signed-off-by: Peter Štibraný Signed-off-by: Peter Štibraný --- cmd/alertmanager/main.go | 2 +- dispatch/dispatch.go | 7 +++++-- dispatch/dispatch_test.go | 6 +++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cmd/alertmanager/main.go b/cmd/alertmanager/main.go index 8b2757de..1fc321fc 100644 --- a/cmd/alertmanager/main.go +++ b/cmd/alertmanager/main.go @@ -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( diff --git a/dispatch/dispatch.go b/dispatch/dispatch.go index 65e4dda8..a31b869b 100644 --- a/dispatch/dispatch.go +++ b/dispatch/dispatch.go @@ -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 diff --git a/dispatch/dispatch_test.go b/dispatch/dispatch_test.go index b2dd5bb5..39575b36 100644 --- a/dispatch/dispatch_test.go +++ b/dispatch/dispatch_test.go @@ -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()