Skip to content
Snippets Groups Projects
Commit 76825fef authored by Joshua Winters's avatar Joshua Winters Committed by Rui Yang
Browse files

Make logger and prometheus optional in server config


Signed-off-by: default avatarJosh Winters <jwinters@pivotal.io>
Co-authored-by: default avatarMark Huang <mhuang@pivotal.io>
parent 1cdb2b1d
No related branches found
No related tags found
No related merge requests found
...@@ -236,23 +236,31 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy) ...@@ -236,23 +236,31 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy)
} }
} }
requestCounter := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Count of all HTTP requests.",
}, []string{"handler", "code", "method"})
err = c.PrometheusRegistry.Register(requestCounter)
if err != nil {
return nil, fmt.Errorf("server: Failed to register Prometheus HTTP metrics: %v", err)
}
instrumentHandlerCounter := func(handlerName string, handler http.Handler) http.HandlerFunc { instrumentHandlerCounter := func(handlerName string, handler http.Handler) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
m := httpsnoop.CaptureMetrics(handler, w, r) handler.ServeHTTP(w, r)
requestCounter.With(prometheus.Labels{"handler": handlerName, "code": strconv.Itoa(m.Code), "method": r.Method}).Inc()
}) })
} }
if c.PrometheusRegistry != nil {
requestCounter := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Count of all HTTP requests.",
}, []string{"handler", "code", "method"})
err = c.PrometheusRegistry.Register(requestCounter)
if err != nil {
return nil, fmt.Errorf("server: Failed to register Prometheus HTTP metrics: %v", err)
}
instrumentHandlerCounter = func(handlerName string, handler http.Handler) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
m := httpsnoop.CaptureMetrics(handler, w, r)
requestCounter.With(prometheus.Labels{"handler": handlerName, "code": strconv.Itoa(m.Code), "method": r.Method}).Inc()
})
}
}
r := mux.NewRouter() r := mux.NewRouter()
handle := func(p string, h http.Handler) { handle := func(p string, h http.Handler) {
r.Handle(path.Join(issuerURL.Path, p), instrumentHandlerCounter(p, h)) r.Handle(path.Join(issuerURL.Path, p), instrumentHandlerCounter(p, h))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment