-
Notifications
You must be signed in to change notification settings - Fork 342
/
metrics.go
284 lines (238 loc) · 8.44 KB
/
metrics.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
package metrics
import (
"fmt"
"net/http"
"net/http/pprof"
"runtime"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
)
const (
defaultMetricsPath = "/metrics"
)
// Kind is the type a metrics expose backend can be.
type Kind int
const (
UnkownKind Kind = 0
CodaHaleKind Kind = 1 << iota
PrometheusKind
AllKind = CodaHaleKind | PrometheusKind
)
func (k Kind) String() string {
switch k {
case CodaHaleKind:
return "codahale"
case PrometheusKind:
return "prometheus"
case AllKind:
return "all"
default:
return "unknown"
}
}
// ParseMetricsKind parses an string and returns the correct Metrics kind.
func ParseMetricsKind(t string) Kind {
t = strings.ToLower(t)
switch t {
case "codahale":
return CodaHaleKind
case "prometheus":
return PrometheusKind
case "all":
return AllKind
default:
return UnkownKind
}
}
// Metrics is the generic interface that all the required backends
// should implement to be an skipper metrics compatible backend.
type Metrics interface {
// Implements the `filter.Metrics` interface.
MeasureSince(key string, start time.Time)
IncCounter(key string)
IncCounterBy(key string, value int64)
IncFloatCounterBy(key string, value float64)
// Additional methods
MeasureRouteLookup(start time.Time)
MeasureFilterRequest(filterName string, start time.Time)
MeasureAllFiltersRequest(routeId string, start time.Time)
MeasureBackend(routeId string, start time.Time)
MeasureBackendHost(routeBackendHost string, start time.Time)
MeasureFilterResponse(filterName string, start time.Time)
MeasureAllFiltersResponse(routeId string, start time.Time)
MeasureResponse(code int, method string, routeId string, start time.Time)
MeasureServe(routeId, host, method string, code int, start time.Time)
IncRoutingFailures()
IncErrorsBackend(routeId string)
MeasureBackend5xx(t time.Time)
IncErrorsStreaming(routeId string)
RegisterHandler(path string, handler *http.ServeMux)
UpdateGauge(key string, value float64)
}
// Options for initializing metrics collection.
type Options struct {
// the metrics exposing format.
Format Kind
// Common prefix for the keys of the different
// collected metrics.
Prefix string
// If set, garbage collector metrics are collected
// in addition to the http traffic metrics.
EnableDebugGcMetrics bool
// If set, Go runtime metrics are collected in
// addition to the http traffic metrics.
EnableRuntimeMetrics bool
// If set, detailed total response time metrics will be collected
// for each route, additionally grouped by status and method.
EnableServeRouteMetrics bool
// If set, a counter for each route is generated, additionally
// grouped by status and method. It differs from the automatically
// generated counter from `EnableServeRouteMetrics` because it will
// always contain the status and method labels, independently of the
// `EnableServeMethodMetric` and `EnableServeStatusCodeMetric`
// flags.
EnableServeRouteCounter bool
// If set, detailed total response time metrics will be collected
// for each host, additionally grouped by status and method.
EnableServeHostMetrics bool
// If set, a counter for each host is generated, additionally
// grouped by status and method. It differs from the automatically
// generated counter from `EnableServeHostMetrics` because it will
// always contain the status and method labels, independently of the
// `EnableServeMethodMetric` and `EnableServeStatusCodeMetric` flags.
EnableServeHostCounter bool
// If set, the detailed total response time metrics will contain the
// HTTP method as a domain of the metric. It affects both route and
// host splitted metrics.
EnableServeMethodMetric bool
// If set, the detailed total response time metrics will contain the
// HTTP Response status code as a domain of the metric. It affects
// both route and host splitted metrics.
EnableServeStatusCodeMetric bool
// If set, detailed response time metrics will be collected
// for each backend host
EnableBackendHostMetrics bool
// EnableAllFiltersMetrics enables collecting combined filter
// metrics per each route. Without the DisableCompatibilityDefaults,
// it is enabled by default.
EnableAllFiltersMetrics bool
// EnableCombinedResponseMetrics enables collecting response time
// metrics combined for every route.
EnableCombinedResponseMetrics bool
// EnableRouteResponseMetrics enables collecting response time
// metrics per each route. Without the DisableCompatibilityDefaults,
// it is enabled by default.
EnableRouteResponseMetrics bool
// EnableRouteBackendErrorsCounters enables counters for backend
// errors per each route. Without the DisableCompatibilityDefaults,
// it is enabled by default.
EnableRouteBackendErrorsCounters bool
// EnableRouteStreamingErrorsCounters enables counters for streaming
// errors per each route. Without the DisableCompatibilityDefaults,
// it is enabled by default.
EnableRouteStreamingErrorsCounters bool
// EnableRouteBackendMetrics enables backend response time metrics
// per each route. Without the DisableCompatibilityDefaults, it is
// enabled by default.
EnableRouteBackendMetrics bool
// UseExpDecaySample, when set, makes the histograms use an exponentially
// decaying sample instead of the default uniform one.
UseExpDecaySample bool
// HistogramBuckets defines buckets into which the observations are counted for
// histogram metrics.
HistogramBuckets []float64
// The following options, for backwards compatibility, are true
// by default: EnableAllFiltersMetrics, EnableRouteResponseMetrics,
// EnableRouteBackendErrorsCounters, EnableRouteStreamingErrorsCounters,
// EnableRouteBackendMetrics. With this compatibility flag, the default
// for these options can be set to false.
DisableCompatibilityDefaults bool
// EnableProfile exposes profiling information on /pprof of the
// metrics listener.
EnableProfile bool
// BlockProfileRate calls runtime.SetBlockProfileRate(BlockProfileRate) if != 0 (<0 will disable) and profiling is enabled
BlockProfileRate int
// MutexProfileFraction calls runtime.SetMutexProfileFraction(MutexProfileFraction) if != 0 (<0 will disable) and profiling is enabled
MutexProfileFraction int
// MemProfileRate calls runtime.SetMemProfileRate(MemProfileRate) if != 0 (<0 will disable) and profiling is enabled
MemProfileRate int
// An instance of a Prometheus registry. It allows registering and serving custom metrics when skipper is used as a
// library.
// A new registry is created if this option is nil.
PrometheusRegistry *prometheus.Registry
}
var (
Default Metrics
Void Metrics
)
func init() {
Void = NewVoid()
Default = Void
}
// NewDefaultHandler returns a default metrics handler.
func NewDefaultHandler(o Options) http.Handler {
m := NewMetrics(o)
return NewHandler(o, m)
}
// NewMetrics creates a metrics collector instance based on the Format option.
func NewMetrics(o Options) Metrics {
var m Metrics
switch o.Format {
case AllKind:
m = NewAll(o)
case PrometheusKind:
m = NewPrometheus(o)
default:
// CodaHale is the default metrics implementation.
m = NewCodaHale(o)
}
return m
}
// NewHandler returns a collection of metrics handlers.
func NewHandler(o Options, m Metrics) http.Handler {
mux := http.NewServeMux()
if o.EnableProfile {
mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
switch n := o.BlockProfileRate; {
case n > 0:
runtime.SetBlockProfileRate(o.BlockProfileRate)
case n < 0:
runtime.SetBlockProfileRate(0)
default:
// 0 keeps default
}
switch n := o.MutexProfileFraction; {
case n > 0:
runtime.SetMutexProfileFraction(o.MutexProfileFraction)
case n < 0:
runtime.SetMutexProfileFraction(0)
default:
// 0 keeps default
}
switch n := o.MemProfileRate; {
case n > 0:
runtime.MemProfileRate = o.MemProfileRate
case n < 0:
runtime.MemProfileRate = 0
default:
// 0 keeps default
}
}
// Root path should return 404.
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
Default = m
// Fix trailing slashes and register routes.
mPath := defaultMetricsPath
mPath = strings.TrimRight(mPath, "/")
m.RegisterHandler(mPath, mux)
mPath = fmt.Sprintf("%s/", mPath)
m.RegisterHandler(mPath, mux)
return mux
}