From 9b5d6c9745bf8cb1c5a2057ee6218640c82e5250 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Wed, 18 Dec 2019 15:10:53 +0000 Subject: [PATCH] Refactor InstrumentHandlerCounter definition --- mediaapi/routing/routing.go | 56 ++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/mediaapi/routing/routing.go b/mediaapi/routing/routing.go index bc919bac..3a8cb9ca 100644 --- a/mediaapi/routing/routing.go +++ b/mediaapi/routing/routing.go @@ -85,33 +85,33 @@ func makeDownloadAPI( activeRemoteRequests *types.ActiveRemoteRequests, activeThumbnailGeneration *types.ActiveThumbnailGeneration, ) http.HandlerFunc { - return promhttp.InstrumentHandlerCounter( - promauto.NewCounterVec( - prometheus.CounterOpts{ - Name: name, - Help: "Total number of media_api requests for either thumbnails or full downloads", - }, - []string{"code"}, - ), http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - req = util.RequestWithLogging(req) - - // Set common headers returned regardless of the outcome of the request - util.SetCORSHeaders(w) - // Content-Type will be overridden in case of returning file data, else we respond with JSON-formatted errors - w.Header().Set("Content-Type", "application/json") - vars := mux.Vars(req) - Download( - w, - req, - gomatrixserverlib.ServerName(vars["serverName"]), - types.MediaID(vars["mediaId"]), - cfg, - db, - client, - activeRemoteRequests, - activeThumbnailGeneration, - name == "thumbnail", - ) + counterVec := promauto.NewCounterVec( + prometheus.CounterOpts{ + Name: name, + Help: "Total number of media_api requests for either thumbnails or full downloads", }, - )) + []string{"code"}, + ) + httpHandler := func(w http.ResponseWriter, req *http.Request) { + req = util.RequestWithLogging(req) + + // Set common headers returned regardless of the outcome of the request + util.SetCORSHeaders(w) + // Content-Type will be overridden in case of returning file data, else we respond with JSON-formatted errors + w.Header().Set("Content-Type", "application/json") + vars := mux.Vars(req) + Download( + w, + req, + gomatrixserverlib.ServerName(vars["serverName"]), + types.MediaID(vars["mediaId"]), + cfg, + db, + client, + activeRemoteRequests, + activeThumbnailGeneration, + name == "thumbnail", + ) + } + return promhttp.InstrumentHandlerCounter(counterVec, http.HandlerFunc(httpHandler)) }