-
Notifications
You must be signed in to change notification settings - Fork 346
/
filter.go
242 lines (211 loc) · 7.36 KB
/
filter.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
package apiusagemonitoring
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/jwt"
)
const (
metricCountAll = "http_count"
metricCountUnknownClass = "httpxxx_count"
metricCount100s = "http1xx_count"
metricCount200s = "http2xx_count"
metricCount300s = "http3xx_count"
metricCount400s = "http4xx_count"
metricCount500s = "http5xx_count"
metricLatency = "latency"
metricLatencySum = "latency_sum"
)
const (
stateBagKey = "filter." + filters.ApiUsageMonitoringName
)
const (
authorizationHeaderName = "Authorization"
authorizationHeaderPrefix = "Bearer "
)
// apiUsageMonitoringFilter implements filters.Filter interface and is the structure
// created for every route invocation of the `apiUsageMonitoring` filter.
type apiUsageMonitoringFilter struct {
clientKeys []string
realmKeys []string
Paths []*pathInfo
UnknownPath *pathInfo
}
type apiUsageMonitoringStateBag struct {
url *url.URL
begin time.Time
}
// HandleErrorResponse is to opt-in for filters to get called
// Response(ctx) in case of errors via proxy. It has to return true to
// opt-in.
func (f *apiUsageMonitoringFilter) HandleErrorResponse() bool { return true }
func (f *apiUsageMonitoringFilter) Request(c filters.FilterContext) {
u := *c.Request().URL
c.StateBag()[stateBagKey] = apiUsageMonitoringStateBag{
url: &u,
begin: time.Now(),
}
}
func (f *apiUsageMonitoringFilter) Response(c filters.FilterContext) {
request, response, metrics := c.Request(), c.Response(), c.Metrics()
stateBag, stateBagPresent := c.StateBag()[stateBagKey].(apiUsageMonitoringStateBag)
path := f.UnknownPath
if stateBagPresent && stateBag.url != nil {
path = f.resolveMatchedPath(stateBag.url)
}
if path == f.UnknownPath {
path = f.resolveMatchedPath(request.URL)
}
classMetricsIndex := response.StatusCode / 100
if classMetricsIndex < 1 || classMetricsIndex > 5 {
log.Errorf(
"Response HTTP Status Code %d is invalid. Response status code metric will be %q.",
response.StatusCode, metricCountUnknownClass)
classMetricsIndex = 0 // unknown classes are tracked, not ignored
}
// Endpoint metrics
endpointMetricsNames := getEndpointMetricsNames(request, path)
metrics.IncCounter(endpointMetricsNames.countAll)
metrics.IncCounter(endpointMetricsNames.countPerStatusCodeRange[classMetricsIndex])
if stateBagPresent {
metrics.MeasureSince(endpointMetricsNames.latency, stateBag.begin)
}
log.Debugf("Pushed endpoint metrics with prefix `%s`", endpointMetricsNames.endpointPrefix)
// Client metrics
if path.ClientTracking != nil {
realmClientKey := f.getRealmClientKey(request, path)
clientMetricsNames := getClientMetricsNames(realmClientKey, path)
metrics.IncCounter(clientMetricsNames.countAll)
metrics.IncCounter(clientMetricsNames.countPerStatusCodeRange[classMetricsIndex])
if stateBagPresent {
latency := time.Since(stateBag.begin).Seconds()
metrics.IncFloatCounterBy(clientMetricsNames.latencySum, latency)
}
log.Debugf("Pushed client metrics with prefix `%s%s.`", path.ClientPrefix, realmClientKey)
}
}
func getClientMetricsNames(realmClientKey string, path *pathInfo) *clientMetricNames {
if value, ok := path.metricPrefixedPerClient.Load(realmClientKey); ok {
if prefixes, ok := value.(clientMetricNames); ok {
return &prefixes
}
}
clientPrefixForThisClient := path.ClientPrefix + realmClientKey + "."
prefixes := &clientMetricNames{
countAll: clientPrefixForThisClient + metricCountAll,
countPerStatusCodeRange: [6]string{
clientPrefixForThisClient + metricCountUnknownClass,
clientPrefixForThisClient + metricCount100s,
clientPrefixForThisClient + metricCount200s,
clientPrefixForThisClient + metricCount300s,
clientPrefixForThisClient + metricCount400s,
clientPrefixForThisClient + metricCount500s,
},
latencySum: clientPrefixForThisClient + metricLatencySum,
}
path.metricPrefixedPerClient.Store(realmClientKey, prefixes)
return prefixes
}
const unknownUnknown = unknownPlaceholder + "." + unknownPlaceholder
// getRealmClientKey generates the proper <realm>.<client> part of the client metrics name.
func (f *apiUsageMonitoringFilter) getRealmClientKey(r *http.Request, path *pathInfo) string {
// no JWT ==> {unknown}.{unknown}
jwt := parseJwtBody(r)
if jwt == nil {
return unknownUnknown
}
// no realm in JWT ==> {unknown}.{unknown}
realm, ok := jwt.getOneOfString(f.realmKeys)
if !ok {
return unknownUnknown
}
// realm is not one of the realmsTrackingPattern to be tracked ==> realm.{all}
if !path.ClientTracking.RealmsTrackingMatcher.MatchString(realm) {
return realm + ".{all}"
}
// no client in JWT ==> realm.{unknown}
client, ok := jwt.getOneOfString(f.clientKeys)
if !ok {
return realm + "." + unknownPlaceholder
}
// if client does not match ==> realm.{no-match}
matcher := path.ClientTracking.ClientTrackingMatcher
if matcher == nil || !matcher.MatchString(client) {
return realm + "." + noMatchPlaceholder
}
// all matched ==> realm.client
return realm + "." + client
}
// resolveMatchedPath tries to match the request's path with one of the configured path template.
func (f *apiUsageMonitoringFilter) resolveMatchedPath(u *url.URL) *pathInfo {
if u != nil {
for _, p := range f.Paths {
if p.Matcher.MatchString(u.Path) {
return p
}
}
}
return f.UnknownPath
}
// getEndpointMetricsNames returns the structure with names of the metrics for this specific context.
// It tries first from the path's cache. If it is not already cached, it is generated and
// caches it to speed up next calls.
func getEndpointMetricsNames(req *http.Request, path *pathInfo) *endpointMetricNames {
method := req.Method
methodIndex, ok := methodToIndex[method]
if !ok {
methodIndex = methodIndexUnknown
method = unknownPlaceholder
}
if p := path.metricPrefixesPerMethod[methodIndex]; p != nil {
return p
}
return createAndCacheMetricsNames(path, method, methodIndex)
}
// createAndCacheMetricsNames generates metrics names and cache them.
func createAndCacheMetricsNames(path *pathInfo, method string, methodIndex int) *endpointMetricNames {
endpointPrefix := path.CommonPrefix + method + "." + path.PathTemplate + ".*.*."
prefixes := &endpointMetricNames{
endpointPrefix: endpointPrefix,
countAll: endpointPrefix + metricCountAll,
countPerStatusCodeRange: [6]string{
endpointPrefix + metricCountUnknownClass,
endpointPrefix + metricCount100s,
endpointPrefix + metricCount200s,
endpointPrefix + metricCount300s,
endpointPrefix + metricCount400s,
endpointPrefix + metricCount500s,
},
latency: endpointPrefix + metricLatency,
}
path.metricPrefixesPerMethod[methodIndex] = prefixes
return prefixes
}
// parseJwtBody parses the JWT token from a HTTP request.
// It returns `nil` if it was not possible to parse the JWT body.
func parseJwtBody(req *http.Request) jwtTokenPayload {
ahead := req.Header.Get(authorizationHeaderName)
tv := strings.TrimPrefix(ahead, authorizationHeaderPrefix)
if tv == ahead {
return nil
}
token, err := jwt.Parse(tv)
if err != nil {
return nil
}
return token.Claims
}
type jwtTokenPayload map[string]interface{}
func (j jwtTokenPayload) getOneOfString(properties []string) (value string, ok bool) {
var rawValue interface{}
for _, p := range properties {
if rawValue, ok = j[p]; ok {
value = fmt.Sprint(rawValue)
return
}
}
return
}