@@ -9,22 +9,21 @@ package server
9
9
import (
10
10
"net/http"
11
11
12
- "github.com/gorilla/mux"
13
12
"github.com/prometheus/client_golang/prometheus"
14
13
"github.com/prometheus/client_golang/prometheus/promauto"
15
14
"github.com/prometheus/client_golang/prometheus/promhttp"
16
15
)
17
16
18
17
// serveMux is an interface of an HTTP request multiplexer.
19
18
type serveMux interface {
20
- Handle (pattern string , handler http.Handler ) * mux. Route
21
- HandleFunc (pattern string , handler func (http.ResponseWriter , * http.Request )) * mux. Route
19
+ Handle (pattern string , handler http.Handler )
20
+ HandleFunc (pattern string , handler func (http.ResponseWriter , * http.Request ))
22
21
ServeHTTP (w http.ResponseWriter , r * http.Request )
23
22
}
24
23
25
24
// httpMetrics is a struct of metrics for Prometheus to collect for each endpoint.
26
25
type httpMetrics struct {
27
- reqest * prometheus.CounterVec
26
+ request * prometheus.CounterVec
28
27
duration * prometheus.HistogramVec
29
28
requestSize * prometheus.HistogramVec
30
29
responseSize * prometheus.HistogramVec
@@ -35,7 +34,7 @@ type httpMetrics struct {
35
34
// and registres them using the given factory.
36
35
func newHTTPMetrics (factory * promauto.Factory , namespace string , subsystem string , constLabels map [string ]string ) * httpMetrics {
37
36
return & httpMetrics {
38
- reqest : factory .NewCounterVec (
37
+ request : factory .NewCounterVec (
39
38
prometheus.CounterOpts {
40
39
Namespace : namespace ,
41
40
Subsystem : subsystem ,
@@ -93,7 +92,7 @@ func newHTTPMetrics(factory *promauto.Factory, namespace string, subsystem strin
93
92
// promServeMux is a wrapper around mux.Router with additional instrumentation to
94
93
// gather Prometheus metrics.
95
94
type promServeMux struct {
96
- router * mux. Router
95
+ router * http. ServeMux
97
96
promFactory * promauto.Factory
98
97
metrics map [string ]* httpMetrics
99
98
namespace string
@@ -104,7 +103,7 @@ type promServeMux struct {
104
103
// namespace and subsystem are used to name the exposed metrics.
105
104
func newPromServeMux (factory * promauto.Factory , namespace string , subsystem string ) * promServeMux {
106
105
return & promServeMux {
107
- router : mux . NewRouter (),
106
+ router : http . NewServeMux (),
108
107
promFactory : factory ,
109
108
metrics : make (map [string ]* httpMetrics ),
110
109
namespace : namespace ,
@@ -114,22 +113,22 @@ func newPromServeMux(factory *promauto.Factory, namespace string, subsystem stri
114
113
115
114
// Handle is a wrapper around (*mux.Router) Handle form the http package
116
115
// A chain of prometheus instrumentation collects metrics for the given handler.
117
- func (p * promServeMux ) Handle (pattern string , handler http.Handler ) * mux. Route {
116
+ func (p * promServeMux ) Handle (pattern string , handler http.Handler ) {
118
117
if p .metrics [pattern ] == nil {
119
118
constLabels := map [string ]string {
120
119
"path" : pattern ,
121
120
}
122
121
p .metrics [pattern ] = newHTTPMetrics (p .promFactory , p .namespace , p .subsystem , constLabels )
123
122
}
124
- return p .router .Handle (pattern , p .metricsMiddleware (pattern , handler ))
123
+ p .router .Handle (pattern , p .metricsMiddleware (pattern , handler ))
125
124
}
126
125
127
126
// HandleFunc registers the handler function for the given pattern.
128
- func (p * promServeMux ) HandleFunc (pattern string , handler func (http.ResponseWriter , * http.Request )) * mux. Route {
127
+ func (p * promServeMux ) HandleFunc (pattern string , handler func (http.ResponseWriter , * http.Request )) {
129
128
if handler == nil {
130
129
panic ("promServerMux: http: nil handler" )
131
130
}
132
- return p .Handle (pattern , http .HandlerFunc (handler ))
131
+ p .Handle (pattern , http .HandlerFunc (handler ))
133
132
}
134
133
135
134
// ServeHTTP is a wrapper around (*mux.Router) ServeHttp form the http package.
@@ -140,7 +139,7 @@ func (p *promServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
140
139
// metricsMiddelware returns the handed next handler wrapped in a bunch of prometheus metric handlers.
141
140
func (p * promServeMux ) metricsMiddleware (pattern string , next http.Handler ) http.Handler {
142
141
return promhttp .InstrumentHandlerDuration (p .metrics [pattern ].duration ,
143
- promhttp .InstrumentHandlerCounter (p .metrics [pattern ].reqest ,
142
+ promhttp .InstrumentHandlerCounter (p .metrics [pattern ].request ,
144
143
promhttp .InstrumentHandlerRequestSize (p .metrics [pattern ].requestSize ,
145
144
promhttp .InstrumentHandlerResponseSize (p .metrics [pattern ].responseSize ,
146
145
promhttp .InstrumentHandlerInFlight (p .metrics [pattern ].inflight , next ),
@@ -152,9 +151,11 @@ func (p *promServeMux) metricsMiddleware(pattern string, next http.Handler) http
152
151
153
152
// setMethodNOtAllowedHandler sets f as instrumented handler for the mux.Router.
154
153
func (p * promServeMux ) setMethodNotAllowedHandler (f func (http.ResponseWriter , * http.Request )) {
155
- p .router .MethodNotAllowedHandler = http .HandlerFunc (
154
+ p .router .HandleFunc (
155
+ "/" ,
156
156
func (w http.ResponseWriter , r * http.Request ) {
157
157
handler := p .metricsMiddleware (r .URL .Path , http .HandlerFunc (f ))
158
158
handler .ServeHTTP (w , r )
159
- })
159
+ },
160
+ )
160
161
}
0 commit comments