From 91e40c5a08c40394ca418a107d9e6d9c7734b171 Mon Sep 17 00:00:00 2001 From: Joshua Winters Date: Wed, 21 Feb 2018 11:23:50 -0500 Subject: [PATCH] Make logger and prometheus optional in server config --- server/server.go | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/server/server.go b/server/server.go index 3b586d8e44..883261d750 100644 --- a/server/server.go +++ b/server/server.go @@ -150,6 +150,10 @@ func NewServer(ctx context.Context, c Config) (*Server, error) { } func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy) (*Server, error) { + if c.Logger == nil { + c.Logger = logrus.New() + } + issuerURL, err := url.Parse(c.Issuer) if err != nil { return nil, fmt.Errorf("server: can't parse issuer URL") @@ -219,27 +223,34 @@ 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"}) + r := mux.NewRouter() - err = c.PrometheusRegistry.Register(requestCounter) - if err != nil { - return nil, fmt.Errorf("server: Failed to register Prometheus HTTP metrics: %v", err) + handleFunc := func(p string, h http.HandlerFunc) { + // r.HandleFunc(path.Join(issuerURL.Path, p), instrumentHandlerCounter(p, h)) + r.HandleFunc(path.Join(issuerURL.Path, p), h) } - 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() - }) - } + if c.PrometheusRegistry != nil { + requestCounter := prometheus.NewCounterVec(prometheus.CounterOpts{ + Name: "http_requests_total", + Help: "Count of all HTTP requests.", + }, []string{"handler", "code", "method"}) - r := mux.NewRouter() - handleFunc := func(p string, h http.HandlerFunc) { - r.HandleFunc(path.Join(issuerURL.Path, p), instrumentHandlerCounter(p, h)) + 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() + }) + } + handleFunc = func(p string, h http.HandlerFunc) { + r.HandleFunc(path.Join(issuerURL.Path, p), instrumentHandlerCounter(p, h)) + } } + handlePrefix := func(p string, h http.Handler) { prefix := path.Join(issuerURL.Path, p) r.PathPrefix(prefix).Handler(http.StripPrefix(prefix, h))