From f0e81464c140c688694f9c61680aab58dc611804 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Wed, 19 Jun 2019 11:46:39 +0100 Subject: [PATCH] Proper instrumenthandler fix --- mediaapi/routing/routing.go | 57 +++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/mediaapi/routing/routing.go b/mediaapi/routing/routing.go index 8a719ee7f..a54238c03 100644 --- a/mediaapi/routing/routing.go +++ b/mediaapi/routing/routing.go @@ -29,6 +29,7 @@ import ( "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" ) @@ -80,32 +81,34 @@ func makeDownloadAPI( activeRemoteRequests *types.ActiveRemoteRequests, activeThumbnailGeneration *types.ActiveThumbnailGeneration, ) http.HandlerFunc { - handlerInfo := prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: name, + 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", + ) }, - []string{name}, - ) - return promhttp.InstrumentHandlerCounter(handlerInfo, 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", - ) - })) + )) }