diff --git a/cmd/prometheus/config.go b/cmd/prometheus/config.go index 4bc249880c..5ed3e9061c 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 f513338632..c59295861e 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 abff02a907..54f5d02f3d 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 0a8b592507..9f479a02de 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{