Merge pull request #1625 from concourse/pr/optional-prometheus-logger-sync
Optional Prometheus Registry
This commit is contained in:
commit
8894eed8d3
1 changed files with 20 additions and 12 deletions
|
@ -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 {
|
||||
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()
|
||||
handler.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
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()
|
||||
handle := func(p string, h http.Handler) {
|
||||
r.Handle(path.Join(issuerURL.Path, p), instrumentHandlerCounter(p, h))
|
||||
|
|
Reference in a new issue