From 46b5925e6cbb728b5eb65859a74781ae7c318c2d Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Fri, 24 Jun 2022 11:48:49 +0530 Subject: [PATCH] feat: register the metrics with manager --- internal/webserver/middleware/metrics.go | 7 +++++-- internal/webserver/middleware/metrics_test.go | 9 ++++++++- internal/webserver/webserver.go | 2 -- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/internal/webserver/middleware/metrics.go b/internal/webserver/middleware/metrics.go index 496459b4..fb5f01e1 100644 --- a/internal/webserver/middleware/metrics.go +++ b/internal/webserver/middleware/metrics.go @@ -10,11 +10,12 @@ 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" ) +// nolint:gochecknoinits func init() { - _ = prometheus.Register(totalRequests) - _ = prometheus.Register(httpDuration) + metrics.Registry.MustRegister(totalRequests, httpDuration) } type httpResponseWriter struct { @@ -34,6 +35,7 @@ func (h *httpResponseWriter) WriteHeader(statusCode int) { h.ResponseWriter.WriteHeader(statusCode) } +// nolint:gochecknoglobals var totalRequests = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: "capsule_proxy_requests_total", @@ -42,6 +44,7 @@ var totalRequests = prometheus.NewCounterVec( []string{"path", "status"}, ) +// nolint:gochecknoglobals var httpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{ Name: "capsule_proxy_response_time_seconds", Help: "Duration of capsule proxy requests.", diff --git a/internal/webserver/middleware/metrics_test.go b/internal/webserver/middleware/metrics_test.go index f656ccc5..16e5aa59 100644 --- a/internal/webserver/middleware/metrics_test.go +++ b/internal/webserver/middleware/metrics_test.go @@ -1,6 +1,7 @@ // Copyright 2022 Clastix Labs // SPDX-License-Identifier: Apache-2.0 +// nolint:testpackage package middleware import ( @@ -20,10 +21,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 +35,6 @@ func Test_MetricsMiddleware_RequestCount(t *testing.T) { output float64 }{ { - name: "single request count", requestCount: 1, path: "/test", @@ -40,6 +43,8 @@ func Test_MetricsMiddleware_RequestCount(t *testing.T) { } for _, test := range testCases { + t.Parallel() + router := mux.NewRouter() router.HandleFunc(test.path, dummyHandler).Methods("GET") router.Use(MetricsMiddleware) @@ -51,6 +56,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 +82,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)