From 0d0c9d5440b0dd8eb04edb16a3b580fabdc82515 Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Fri, 31 Mar 2017 21:20:12 +0530 Subject: [PATCH] Move Registerer to Config Struct in Notifier --- cmd/prometheus/config.go | 4 ++++ cmd/prometheus/main.go | 2 +- notifier/notifier.go | 6 ++++-- notifier/notifier_test.go | 12 ++++++------ 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/cmd/prometheus/config.go b/cmd/prometheus/config.go index 4bc249880..5ed3e9061 100644 --- a/cmd/prometheus/config.go +++ b/cmd/prometheus/config.go @@ -26,6 +26,7 @@ import ( "unicode" "github.com/asaskevich/govalidator" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/log" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/config" @@ -56,6 +57,9 @@ var cfg = struct { prometheusURL string }{ alertmanagerURLs: stringset{}, + notifier: notifier.Options{ + Registerer: prometheus.DefaultRegisterer, + }, } // Value type for flags that are now unused, but which are kept around to diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index f51333863..c59295861 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -97,7 +97,7 @@ func Main() int { reloadables = append(reloadables, remoteStorage) var ( - notifier = notifier.New(&cfg.notifier, prometheus.DefaultRegisterer) + notifier = notifier.New(&cfg.notifier) targetManager = retrieval.NewTargetManager(sampleAppender) queryEngine = promql.NewEngine(localStorage, &cfg.queryEngine) ctx, cancelCtx = context.WithCancel(context.Background()) diff --git a/notifier/notifier.go b/notifier/notifier.go index abff02a90..54f5d02f3 100644 --- a/notifier/notifier.go +++ b/notifier/notifier.go @@ -74,6 +74,8 @@ type Options struct { RelabelConfigs []*config.RelabelConfig // Used for sending HTTP requests to the Alertmanager. Do func(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) + + Registerer prometheus.Registerer } type alertMetrics struct { @@ -147,7 +149,7 @@ func newAlertMetrics(r prometheus.Registerer, o *Options) *alertMetrics { } // New constructs a new Notifier. -func New(o *Options, r prometheus.Registerer) *Notifier { +func New(o *Options) *Notifier { ctx, cancel := context.WithCancel(context.Background()) if o.Do == nil { @@ -160,7 +162,7 @@ func New(o *Options, r prometheus.Registerer) *Notifier { cancel: cancel, more: make(chan struct{}, 1), opts: o, - metrics: newAlertMetrics(r, o), + metrics: newAlertMetrics(o.Registerer, o), } } diff --git a/notifier/notifier_test.go b/notifier/notifier_test.go index 0a8b59250..9f479a02d 100644 --- a/notifier/notifier_test.go +++ b/notifier/notifier_test.go @@ -63,7 +63,7 @@ func TestPostPath(t *testing.T) { } func TestHandlerNextBatch(t *testing.T) { - h := New(&Options{}, prometheus.DefaultRegisterer) + h := New(&Options{}) defer unregisterMetrics() for i := range make([]struct{}, 2*maxBatchSize+1) { @@ -159,7 +159,7 @@ func TestHandlerSendAll(t *testing.T) { defer server1.Close() defer server2.Close() - h := New(&Options{}, prometheus.DefaultRegisterer) + h := New(&Options{}) defer unregisterMetrics() h.alertmanagers = append(h.alertmanagers, &alertmanagerSet{ ams: []alertmanager{ @@ -227,7 +227,7 @@ func TestCustomDo(t *testing.T) { Body: ioutil.NopCloser(nil), }, nil }, - }, prometheus.DefaultRegisterer) + }) defer unregisterMetrics() h.sendOne(context.Background(), nil, testURL, []byte(testBody)) @@ -250,7 +250,7 @@ func TestExternalLabels(t *testing.T) { Replacement: "c", }, }, - }, prometheus.DefaultRegisterer) + }) defer unregisterMetrics() // This alert should get the external label attached. @@ -305,7 +305,7 @@ func TestHandlerRelabel(t *testing.T) { Replacement: "renamed", }, }, - }, prometheus.DefaultRegisterer) + }) defer unregisterMetrics() // This alert should be dropped due to the configuration @@ -360,7 +360,7 @@ func TestHandlerQueueing(t *testing.T) { h := New(&Options{ QueueCapacity: 3 * maxBatchSize, - }, prometheus.DefaultRegisterer) + }) defer unregisterMetrics() h.alertmanagers = append(h.alertmanagers, &alertmanagerSet{ ams: []alertmanager{