-
Notifications
You must be signed in to change notification settings - Fork 345
/
pathinfo.go
103 lines (89 loc) · 2.88 KB
/
pathinfo.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
package apiusagemonitoring
import (
"net/http"
"regexp"
"strings"
"sync"
)
// pathInfo contains the tracking information for a specific path.
type pathInfo struct {
ApplicationId string
Tag string
ApiId string
PathTemplate string
Matcher *regexp.Regexp
ClientTracking *clientTrackingInfo
CommonPrefix string
ClientPrefix string
metricPrefixesPerMethod [methodIndexLength]*endpointMetricNames
metricPrefixedPerClient sync.Map
}
func newPathInfo(applicationId, tag, apiId, pathTemplate string, clientTracking *clientTrackingInfo) *pathInfo {
id, t, found := strings.Cut(applicationId, ":")
if found {
applicationId = id
tag = t
}
if tag == "" {
tag = noTagPlaceholder
}
commonPrefix := applicationId + "." + tag + "." + apiId + "."
return &pathInfo{
ApplicationId: applicationId,
Tag: tag,
ApiId: apiId,
PathTemplate: pathTemplate,
metricPrefixedPerClient: sync.Map{},
ClientTracking: clientTracking,
CommonPrefix: commonPrefix,
ClientPrefix: commonPrefix + "*.*.",
}
}
// pathInfoByRegExRev allows sort.Sort to reorder a slice of `pathInfo` in
// reverse alphabetical order of their matcher (Regular Expression). That way,
// the more complex RegEx will end up first in the slice.
type pathInfoByRegExRev []*pathInfo
func (s pathInfoByRegExRev) Len() int { return len(s) }
func (s pathInfoByRegExRev) Less(i, j int) bool { return s[i].Matcher.String() > s[j].Matcher.String() }
func (s pathInfoByRegExRev) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type endpointMetricNames struct {
endpointPrefix string
countAll string
countPerStatusCodeRange [6]string
latency string
}
type clientMetricNames struct {
countAll string
countPerStatusCodeRange [6]string
latencySum string
}
const (
methodIndexGet = iota // GET
methodIndexHead // HEAD
methodIndexPost // POST
methodIndexPut // PUT
methodIndexPatch // PATCH
methodIndexDelete // DELETE
methodIndexConnect // CONNECT
methodIndexOptions // OPTIONS
methodIndexTrace // TRACE
methodIndexUnknown // Value when the HTTP Method is not in the known list
methodIndexLength // Gives the constant size of the `metricPrefixesPerMethod` array.
)
var (
methodToIndex = map[string]int{
http.MethodGet: methodIndexGet,
http.MethodHead: methodIndexHead,
http.MethodPost: methodIndexPost,
http.MethodPut: methodIndexPut,
http.MethodPatch: methodIndexPatch,
http.MethodDelete: methodIndexDelete,
http.MethodConnect: methodIndexConnect,
http.MethodOptions: methodIndexOptions,
http.MethodTrace: methodIndexTrace,
}
)
type clientTrackingInfo struct {
ClientTrackingMatcher *regexp.Regexp
RealmsTrackingMatcher *regexp.Regexp
}