-
Notifications
You must be signed in to change notification settings - Fork 343
/
codahale.go
386 lines (325 loc) · 10.6 KB
/
codahale.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package metrics
import (
"encoding/json"
"fmt"
"net/http"
"path"
"strings"
"time"
"github.com/rcrowley/go-metrics"
)
const (
KeyRouteLookup = "routelookup"
KeyRouteFailure = "routefailure"
KeyFilterRequest = "filter.%s.request"
KeyFiltersRequest = "allfilters.request.%s"
KeyAllFiltersRequestCombined = "allfilters.combined.request"
KeyProxyBackend = "backend.%s"
KeyProxyBackendCombined = "all.backend"
KeyProxyBackendHost = "backendhost.%s"
KeyFilterResponse = "filter.%s.response"
KeyFiltersResponse = "allfilters.response.%s"
KeyAllFiltersResponseCombined = "allfilters.combined.response"
KeyResponse = "response.%d.%s.skipper.%s"
KeyResponseCombined = "all.response.%d.%s.skipper"
Key5xxsBackend = "all.backend.5xx"
KeyErrorsBackend = "errors.backend.%s"
KeyErrorsStreaming = "errors.streaming.%s"
statsRefreshDuration = time.Duration(5 * time.Second)
defaultUniformReservoirSize = 1024
defaultExpDecayReservoirSize = 1028
defaultExpDecayAlpha = 0.015
)
// CodaHale is the CodaHale format backend, implements Metrics interface in DropWizard's CodaHale metrics format.
type CodaHale struct {
reg metrics.Registry
createTimer func() metrics.Timer
createCounter func() metrics.Counter
createGauge func() metrics.GaugeFloat64
options Options
handler http.Handler
quit chan struct{}
}
// NewCodaHale returns a new CodaHale backend of metrics.
func NewCodaHale(o Options) *CodaHale {
o = applyCompatibilityDefaults(o)
c := &CodaHale{}
c.quit = make(chan struct{})
c.reg = metrics.NewRegistry()
var createSample func() metrics.Sample
if o.UseExpDecaySample {
createSample = newExpDecaySample
} else {
createSample = newUniformSample
}
c.createTimer = func() metrics.Timer { return createTimer(createSample()) }
c.createCounter = metrics.NewCounter
c.createGauge = metrics.NewGaugeFloat64
c.options = o
if o.EnableDebugGcMetrics {
metrics.RegisterDebugGCStats(c.reg)
go c.collectStats(metrics.CaptureDebugGCStatsOnce)
}
if o.EnableRuntimeMetrics {
metrics.RegisterRuntimeMemStats(c.reg)
go c.collectStats(metrics.CaptureRuntimeMemStatsOnce)
}
return c
}
func NewVoid() *CodaHale {
c := &CodaHale{}
c.reg = metrics.NewRegistry()
c.createTimer = func() metrics.Timer { return metrics.NilTimer{} }
c.createCounter = func() metrics.Counter { return metrics.NilCounter{} }
c.createGauge = func() metrics.GaugeFloat64 { return metrics.NilGaugeFloat64{} }
return c
}
func (c *CodaHale) getTimer(key string) metrics.Timer {
return c.reg.GetOrRegister(key, c.createTimer).(metrics.Timer)
}
func (c *CodaHale) updateTimer(key string, d time.Duration) {
c.getTimer(key).Update(d)
}
func (c *CodaHale) MeasureSince(key string, start time.Time) {
c.measureSince(key, start)
}
func (c *CodaHale) getGauge(key string) metrics.GaugeFloat64 {
return c.reg.GetOrRegister(key, c.createGauge).(metrics.GaugeFloat64)
}
func (c *CodaHale) UpdateGauge(key string, v float64) {
c.getGauge(key).Update(v)
}
func (c *CodaHale) IncCounter(key string) {
c.incCounter(key, 1)
}
func (c *CodaHale) IncCounterBy(key string, value int64) {
c.incCounter(key, value)
}
func (c *CodaHale) IncFloatCounterBy(key string, value float64) {
// Dropped. CodaHale does not support float counter.
}
func (c *CodaHale) measureSince(key string, start time.Time) {
c.updateTimer(key, time.Since(start))
}
func (c *CodaHale) MeasureRouteLookup(start time.Time) {
c.measureSince(KeyRouteLookup, start)
}
func (c *CodaHale) MeasureFilterRequest(filterName string, start time.Time) {
c.measureSince(fmt.Sprintf(KeyFilterRequest, filterName), start)
}
func (c *CodaHale) MeasureAllFiltersRequest(routeId string, start time.Time) {
c.measureSince(KeyAllFiltersRequestCombined, start)
if c.options.EnableAllFiltersMetrics {
c.measureSince(fmt.Sprintf(KeyFiltersRequest, routeId), start)
}
}
func (c *CodaHale) MeasureBackend(routeId string, start time.Time) {
c.measureSince(KeyProxyBackendCombined, start)
if c.options.EnableRouteBackendMetrics {
c.measureSince(fmt.Sprintf(KeyProxyBackend, routeId), start)
}
}
func (c *CodaHale) MeasureBackendHost(routeBackendHost string, start time.Time) {
if c.options.EnableBackendHostMetrics {
c.measureSince(fmt.Sprintf(KeyProxyBackendHost, hostForKey(routeBackendHost)), start)
}
}
func (c *CodaHale) MeasureFilterResponse(filterName string, start time.Time) {
c.measureSince(fmt.Sprintf(KeyFilterResponse, filterName), start)
}
func (c *CodaHale) MeasureAllFiltersResponse(routeId string, start time.Time) {
c.measureSince(KeyAllFiltersResponseCombined, start)
if c.options.EnableAllFiltersMetrics {
c.measureSince(fmt.Sprintf(KeyFiltersResponse, routeId), start)
}
}
func (c *CodaHale) MeasureResponse(code int, method string, routeId string, start time.Time) {
method = measuredMethod(method)
if c.options.EnableCombinedResponseMetrics {
c.measureSince(fmt.Sprintf(KeyResponseCombined, code, method), start)
}
if c.options.EnableRouteResponseMetrics {
c.measureSince(fmt.Sprintf(KeyResponse, code, method, routeId), start)
}
}
func (c *CodaHale) MeasureServe(routeId, host, method string, code int, start time.Time) {
if !(c.options.EnableServeRouteMetrics || c.options.EnableServeHostMetrics) {
return
}
var keyServeRoute, keyServeHost string
method = measuredMethod(method)
hfk := hostForKey(host)
switch {
case c.options.EnableServeMethodMetric && c.options.EnableServeStatusCodeMetric:
keyServeHost = fmt.Sprintf("servehost.%s.%s.%d", hfk, method, code)
keyServeRoute = fmt.Sprintf("serveroute.%s.%s.%d", routeId, method, code)
case c.options.EnableServeMethodMetric:
keyServeHost = fmt.Sprintf("servehost.%s.%s", hfk, method)
keyServeRoute = fmt.Sprintf("serveroute.%s.%s", routeId, method)
case c.options.EnableServeStatusCodeMetric:
keyServeHost = fmt.Sprintf("servehost.%s.%d", hfk, code)
keyServeRoute = fmt.Sprintf("serveroute.%s.%d", routeId, code)
default:
keyServeHost = fmt.Sprintf("servehost.%s", hfk)
keyServeRoute = fmt.Sprintf("serveroute.%s", routeId)
}
if c.options.EnableServeRouteMetrics {
c.measureSince(keyServeRoute, start)
}
if c.options.EnableServeHostMetrics {
c.measureSince(keyServeHost, start)
}
}
func (c *CodaHale) getCounter(key string) metrics.Counter {
return c.reg.GetOrRegister(key, c.createCounter).(metrics.Counter)
}
func (c *CodaHale) incCounter(key string, value int64) {
c.getCounter(key).Inc(value)
}
func (c *CodaHale) IncRoutingFailures() {
c.incCounter(KeyRouteFailure, 1)
}
func (c *CodaHale) IncErrorsBackend(routeId string) {
if c.options.EnableRouteBackendErrorsCounters {
c.incCounter(fmt.Sprintf(KeyErrorsBackend, routeId), 1)
}
}
func (c *CodaHale) MeasureBackend5xx(t time.Time) {
c.measureSince(Key5xxsBackend, t)
}
func (c *CodaHale) IncErrorsStreaming(routeId string) {
if c.options.EnableRouteStreamingErrorsCounters {
c.incCounter(fmt.Sprintf(KeyErrorsStreaming, routeId), 1)
}
}
func (c *CodaHale) Close() {
close(c.quit)
}
func (c *CodaHale) collectStats(capture func(r metrics.Registry)) {
ticker := time.NewTicker(statsRefreshDuration)
defer ticker.Stop()
for {
select {
case <-ticker.C:
capture(c.reg)
case <-c.quit:
return
}
}
}
func (c *CodaHale) RegisterHandler(path string, handler *http.ServeMux) {
h := c.getHandler(path)
handler.Handle(path, h)
}
func (c *CodaHale) CreateHandler(path string) http.Handler {
return &codaHaleMetricsHandler{path: path, registry: c.reg, options: c.options}
}
func (c *CodaHale) getHandler(path string) http.Handler {
if c.handler != nil {
return c.handler
}
c.handler = c.CreateHandler(path)
return c.handler
}
type codaHaleMetricsHandler struct {
path string
registry metrics.Registry
options Options
}
func (c *codaHaleMetricsHandler) sendMetrics(w http.ResponseWriter, p string) {
_, k := path.Split(p)
metrics := filterMetrics(c.registry, c.options.Prefix, k)
if len(metrics) > 0 {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(metrics)
} else {
http.NotFound(w, nil)
}
}
// This listener is only used to expose the metrics
func (c *codaHaleMetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
p := r.URL.Path
c.sendMetrics(w, strings.TrimPrefix(p, c.path))
}
func filterMetrics(reg metrics.Registry, prefix, key string) skipperMetrics {
metrics := make(skipperMetrics)
canonicalKey := strings.TrimPrefix(key, prefix)
m := reg.Get(canonicalKey)
if m != nil {
metrics[key] = m
} else {
reg.Each(func(name string, i interface{}) {
if key == "" || (strings.HasPrefix(name, canonicalKey)) {
metrics[prefix+name] = i
}
})
}
return metrics
}
type skipperMetrics map[string]interface{}
// This listener is used to expose the collected metrics.
func (sm skipperMetrics) MarshalJSON() ([]byte, error) {
data := make(map[string]map[string]interface{})
for name, metric := range sm {
values := make(map[string]interface{})
var metricsFamily string
switch m := metric.(type) {
case metrics.Gauge:
metricsFamily = "gauges"
values["value"] = m.Value()
case metrics.GaugeFloat64:
t := m.Snapshot()
metricsFamily = "gauges"
values["value"] = t.Value()
case metrics.Histogram:
metricsFamily = "histograms"
h := m.Snapshot()
ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
values["count"] = h.Count()
values["min"] = h.Min()
values["max"] = h.Max()
values["mean"] = h.Mean()
values["stddev"] = h.StdDev()
values["median"] = ps[0]
values["75%"] = ps[1]
values["95%"] = ps[2]
values["99%"] = ps[3]
values["99.9%"] = ps[4]
case metrics.Timer:
metricsFamily = "timers"
t := m.Snapshot()
ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
values["count"] = t.Count()
values["min"] = t.Min()
values["max"] = t.Max()
values["mean"] = t.Mean()
values["stddev"] = t.StdDev()
values["median"] = ps[0]
values["75%"] = ps[1]
values["95%"] = ps[2]
values["99%"] = ps[3]
values["99.9%"] = ps[4]
values["1m.rate"] = t.Rate1()
values["5m.rate"] = t.Rate5()
values["15m.rate"] = t.Rate15()
values["mean.rate"] = t.RateMean()
case metrics.Counter:
metricsFamily = "counters"
t := m.Snapshot()
values["count"] = t.Count()
default:
metricsFamily = "unknown"
values["error"] = fmt.Sprintf("unknown metrics type %T", m)
}
if data[metricsFamily] == nil {
data[metricsFamily] = make(map[string]interface{})
}
data[metricsFamily][name] = values
}
return json.Marshal(data)
}