forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
195 lines (161 loc) · 5.42 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
package metrics
import (
"fmt"
"net/http"
"net/http/pprof"
"strings"
"time"
)
const (
defaultMetricsPath = "/metrics"
)
// Kind is the type a metrics expose backend can be.
type Kind int
const (
UnkownKind Kind = iota
CodaHaleKind
PrometheusKind
)
func (k Kind) String() string {
switch k {
case CodaHaleKind:
return "codahale"
case PrometheusKind:
return "prometheus"
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
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 {
MeasureSince(key string, start time.Time)
IncCounter(key string)
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)
}
// 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, detailed total response time metrics will be collected
// for each host, additionally grouped by status and method.
EnableServeHostMetrics 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
// 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
}
var (
Default Metrics
Void Metrics
)
func init() {
Void = NewVoid()
Default = Void
}
// NewDefaultHandler returns a default metrics handler.
func NewDefaultHandler(o Options) http.Handler {
var m Metrics
switch o.Format {
case PrometheusKind:
m = NewPrometheus(o)
default:
// CodaHale is the default backend always.
m = NewCodaHale(o)
}
return NewHandler(o, 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))
}
// 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
}