Skip to content

Commit

Permalink
feat: register the metrics with manager
Browse files Browse the repository at this point in the history
  • Loading branch information
viveksyngh committed Jun 28, 2022
1 parent 8ed733e commit 46b5925
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
7 changes: 5 additions & 2 deletions internal/webserver/middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand All @@ -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.",
Expand Down
9 changes: 8 additions & 1 deletion internal/webserver/middleware/metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2022 Clastix Labs
// SPDX-License-Identifier: Apache-2.0

// nolint:testpackage
package middleware

import (
Expand All @@ -20,18 +21,20 @@ 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
path string
output float64
}{
{

name: "single request count",
requestCount: 1,
path: "/test",
Expand All @@ -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)
Expand All @@ -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)
}

Expand All @@ -76,6 +82,7 @@ func labels2Map(labels []*model.LabelPair) map[string]string {
for _, l := range labels {
res[l.GetName()] = l.GetValue()
}

return res
}

Expand Down
2 changes: 0 additions & 2 deletions internal/webserver/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 46b5925

Please sign in to comment.