Refactor InstrumentHandlerCounter definition

main
Andrew Morgan 2019-12-18 15:10:53 +00:00
parent 2baf5baa75
commit 9b5d6c9745
1 changed files with 28 additions and 28 deletions

View File

@ -85,33 +85,33 @@ func makeDownloadAPI(
activeRemoteRequests *types.ActiveRemoteRequests, activeRemoteRequests *types.ActiveRemoteRequests,
activeThumbnailGeneration *types.ActiveThumbnailGeneration, activeThumbnailGeneration *types.ActiveThumbnailGeneration,
) http.HandlerFunc { ) http.HandlerFunc {
return promhttp.InstrumentHandlerCounter( counterVec := promauto.NewCounterVec(
promauto.NewCounterVec( prometheus.CounterOpts{
prometheus.CounterOpts{ Name: name,
Name: name, Help: "Total number of media_api requests for either thumbnails or full downloads",
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",
)
}, },
)) []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))
} }