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 24, 2022
1 parent 8ed733e commit e3553d8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions internal/webserver/middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion internal/webserver/middleware/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,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 @@ -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)
}

Expand All @@ -76,6 +79,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 e3553d8

Please sign in to comment.