diff --git a/internal/webserver/middleware/metrics.go b/internal/webserver/middleware/metrics.go index 496459b4..df5a5fb5 100644 --- a/internal/webserver/middleware/metrics.go +++ b/internal/webserver/middleware/metrics.go @@ -10,11 +10,11 @@ import ( "github.com/gorilla/mux" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" + "sigs.k8s.io/controller-runtime/pkg/metrics" ) func init() { - _ = prometheus.Register(totalRequests) - _ = prometheus.Register(httpDuration) + metrics.Registry.MustRegister(totalRequests, httpDuration) } type httpResponseWriter struct { diff --git a/internal/webserver/middleware/metrics_test.go b/internal/webserver/middleware/metrics_test.go index f656ccc5..173011d9 100644 --- a/internal/webserver/middleware/metrics_test.go +++ b/internal/webserver/middleware/metrics_test.go @@ -20,10 +20,13 @@ func dummyHandler(w http.ResponseWriter, r *http.Request) { func newRequest(method, url string) (*http.Request, error) { req, err := http.NewRequest(method, url, nil) + return req, err } func Test_MetricsMiddleware_RequestCount(t *testing.T) { + t.Parallel() + testCases := []struct { name string requestCount int @@ -31,7 +34,6 @@ func Test_MetricsMiddleware_RequestCount(t *testing.T) { output float64 }{ { - name: "single request count", requestCount: 1, path: "/test", @@ -51,6 +53,7 @@ func Test_MetricsMiddleware_RequestCount(t *testing.T) { if err != nil { t.Errorf("failed to create HTTP request object") } + router.ServeHTTP(rw, req) } @@ -76,6 +79,7 @@ func labels2Map(labels []*model.LabelPair) map[string]string { for _, l := range labels { res[l.GetName()] = l.GetValue() } + return res } diff --git a/internal/webserver/webserver.go b/internal/webserver/webserver.go index 39f50a10..2bba07ec 100644 --- a/internal/webserver/webserver.go +++ b/internal/webserver/webserver.go @@ -19,7 +19,6 @@ import ( "github.com/gorilla/handlers" "github.com/gorilla/mux" "github.com/pkg/errors" - "github.com/prometheus/client_golang/prometheus/promhttp" "golang.org/x/net/http/httpguts" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/util/sets" @@ -265,7 +264,6 @@ func (n kubeFilter) Start(ctx context.Context) error { writer.WriteHeader(200) _, _ = writer.Write([]byte("ok")) }) - r.Path("/_metrics").Subrouter().Handle("", promhttp.Handler()) root := r.PathPrefix("").Subrouter() n.registerModules(ctx, root)