Skip to content

Commit

Permalink
Make logger and prometheus optional in server config
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Winters authored and vito committed Aug 14, 2018
1 parent 036e5d0 commit 91e40c5
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 91e40c5

Please sign in to comment.