@@ -236,23 +236,31 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy)
236236 }
237237 }
238238
239- requestCounter := prometheus .NewCounterVec (prometheus.CounterOpts {
240- Name : "http_requests_total" ,
241- Help : "Count of all HTTP requests." ,
242- }, []string {"handler" , "code" , "method" })
243-
244- err = c .PrometheusRegistry .Register (requestCounter )
245- if err != nil {
246- return nil , fmt .Errorf ("server: Failed to register Prometheus HTTP metrics: %v" , err )
247- }
248-
249239 instrumentHandlerCounter := func (handlerName string , handler http.Handler ) http.HandlerFunc {
250240 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
251- m := httpsnoop .CaptureMetrics (handler , w , r )
252- requestCounter .With (prometheus.Labels {"handler" : handlerName , "code" : strconv .Itoa (m .Code ), "method" : r .Method }).Inc ()
241+ handler .ServeHTTP (w , r )
253242 })
254243 }
255244
245+ if c .PrometheusRegistry != nil {
246+ requestCounter := prometheus .NewCounterVec (prometheus.CounterOpts {
247+ Name : "http_requests_total" ,
248+ Help : "Count of all HTTP requests." ,
249+ }, []string {"handler" , "code" , "method" })
250+
251+ err = c .PrometheusRegistry .Register (requestCounter )
252+ if err != nil {
253+ return nil , fmt .Errorf ("server: Failed to register Prometheus HTTP metrics: %v" , err )
254+ }
255+
256+ instrumentHandlerCounter = func (handlerName string , handler http.Handler ) http.HandlerFunc {
257+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
258+ m := httpsnoop .CaptureMetrics (handler , w , r )
259+ requestCounter .With (prometheus.Labels {"handler" : handlerName , "code" : strconv .Itoa (m .Code ), "method" : r .Method }).Inc ()
260+ })
261+ }
262+ }
263+
256264 r := mux .NewRouter ()
257265 handle := func (p string , h http.Handler ) {
258266 r .Handle (path .Join (issuerURL .Path , p ), instrumentHandlerCounter (p , h ))
0 commit comments