/
queryhttp.go
273 lines (240 loc) · 7.05 KB
/
queryhttp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package queryhttp
import (
"fmt"
"net/http"
"strconv"
"sync"
"github.com/m3db/m3/src/query/util/logging"
"github.com/m3db/m3/src/x/instrument"
"github.com/uber-go/tally"
"github.com/gorilla/mux"
)
var (
histogramTimerOptions = instrument.NewHistogramTimerOptions(
instrument.HistogramTimerOptions{
// Use sparse histogram timer buckets to not overload with latency metrics.
HistogramBuckets: instrument.SparseHistogramTimerHistogramBuckets(),
})
)
// NewEndpointRegistry returns a new endpoint registry.
func NewEndpointRegistry(
router *mux.Router,
instrumentOpts instrument.Options,
) *EndpointRegistry {
return &EndpointRegistry{
router: router,
instrumentOpts: instrumentOpts.SetMetricsScope(
instrumentOpts.MetricsScope().SubScope("http_handler")),
registered: make(map[routeKey]*mux.Route),
}
}
// EndpointRegistry is an endpoint registry that can register routes
// and instrument them.
type EndpointRegistry struct {
router *mux.Router
instrumentOpts instrument.Options
registered map[routeKey]*mux.Route
}
type routeKey struct {
path string
pathPrefix string
method string
}
// RegisterOptions are options for registering a handler.
type RegisterOptions struct {
Path string
PathPrefix string
Handler http.Handler
Methods []string
}
// Register registers an endpoint.
func (r *EndpointRegistry) Register(
opts RegisterOptions,
middlewareOpts ...logging.MiddlewareOption,
) error {
// Wrap requests with response time logging as well as panic recovery.
var (
route *mux.Route
metrics = newRouteMetrics(r.instrumentOpts)
middlewareOptions []logging.MiddlewareOption
)
postRequestOption := logging.WithPostRequestMiddleware(
logging.PostRequestMiddleware(func(
r *http.Request,
meta logging.RequestMiddlewareMetadata,
) {
if !meta.WroteHeader {
return
}
p, err := route.GetPathTemplate()
if err != nil {
p = "unknown"
}
counter, timer := metrics.metric(p, meta.StatusCode)
counter.Inc(1)
timer.Record(meta.Duration)
}))
middlewareOptions = append(middlewareOptions, postRequestOption)
middlewareOptions = append(middlewareOptions, middlewareOpts...)
wrapped := func(n http.Handler) http.Handler {
return logging.WithResponseTimeAndPanicErrorLogging(n, r.instrumentOpts,
middlewareOptions...)
}
handler := wrapped(opts.Handler)
if p := opts.Path; p != "" && len(opts.Methods) > 0 {
for _, method := range opts.Methods {
key := routeKey{
path: p,
method: method,
}
if _, ok := r.registered[key]; ok {
return fmt.Errorf("route already exists: path=%s, method=%s",
p, method)
}
route = r.router.HandleFunc(p, handler.ServeHTTP).Methods(method)
r.registered[key] = route
}
} else if p := opts.PathPrefix; p != "" {
key := routeKey{
pathPrefix: p,
}
if _, ok := r.registered[key]; ok {
return fmt.Errorf("route already exists: pathPrefix=%s", p)
}
route = r.router.PathPrefix(p).Handler(handler)
r.registered[key] = route
} else {
return fmt.Errorf("no path and methods or path prefix set: +%v", opts)
}
return nil
}
// RegisterPathsOptions is options for registering multiple paths
// with the same handler.
type RegisterPathsOptions struct {
Handler http.Handler
Methods []string
}
// RegisterPaths registers multiple paths for the same handler.
func (r *EndpointRegistry) RegisterPaths(
paths []string,
opts RegisterPathsOptions,
middlewareOpts ...logging.MiddlewareOption,
) error {
for _, p := range paths {
if err := r.Register(RegisterOptions{
Path: p,
Handler: opts.Handler,
Methods: opts.Methods,
}); err != nil {
return err
}
}
return nil
}
// PathRoute resolves a registered route that was registered by path and method,
// not by path prefix.
func (r *EndpointRegistry) PathRoute(path, method string) (*mux.Route, bool) {
key := routeKey{
path: path,
method: method,
}
h, ok := r.registered[key]
return h, ok
}
// PathPrefixRoute resolves a registered route that was registered by path
// prefix, not by path and method.
func (r *EndpointRegistry) PathPrefixRoute(pathPrefix string) (*mux.Route, bool) {
key := routeKey{
pathPrefix: pathPrefix,
}
h, ok := r.registered[key]
return h, ok
}
// Walk walks the router and all its sub-routers, calling walkFn for each route
// in the tree. The routes are walked in the order they were added. Sub-routers
// are explored depth-first.
func (r *EndpointRegistry) Walk(walkFn mux.WalkFunc) error {
return r.router.Walk(walkFn)
}
func routeName(p string, method string) string {
if method == "" {
return p
}
return fmt.Sprintf("%s %s", p, method)
}
type routeMetrics struct {
sync.RWMutex
instrumentOpts instrument.Options
metrics map[routeMetricKey]routeMetric
timers map[string]tally.Timer
}
type routeMetricKey struct {
path string
status int
}
type routeMetric struct {
status tally.Counter
}
func newRouteMetrics(instrumentOpts instrument.Options) *routeMetrics {
return &routeMetrics{
instrumentOpts: instrumentOpts,
metrics: make(map[routeMetricKey]routeMetric),
timers: make(map[string]tally.Timer),
}
}
func (m *routeMetrics) metric(path string, status int) (tally.Counter, tally.Timer) {
key := routeMetricKey{
path: path,
status: status,
}
m.RLock()
metric, ok1 := m.metrics[key]
timer, ok2 := m.timers[path]
m.RUnlock()
if ok1 && ok2 {
return metric.status, timer
}
m.Lock()
defer m.Unlock()
metric, ok1 = m.metrics[key]
timer, ok2 = m.timers[path]
if ok1 && ok2 {
return metric.status, timer
}
scopePath := m.instrumentOpts.MetricsScope().Tagged(map[string]string{
"path": path,
})
scopePathAndStatus := scopePath.Tagged(map[string]string{
"status": strconv.Itoa(status),
})
if !ok1 {
metric = routeMetric{
status: scopePathAndStatus.Counter("request"),
}
m.metrics[key] = metric
}
if !ok2 {
timer = instrument.NewTimer(scopePath, "latency", histogramTimerOptions)
m.timers[path] = timer
}
return metric.status, timer
}