-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
218 lines (165 loc) · 5.64 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
package httransform
import "bytes"
var (
// copy-pasted from fasthttp for the faster access
metricsStrGet = []byte("GET")
metricsStrHead = []byte("HEAD")
metricsStrPost = []byte("POST")
metricsStrPut = []byte("PUT")
metricsStrDelete = []byte("DELETE")
metricsStrConnect = []byte("CONNECT")
metricsStrOptions = []byte("OPTIONS")
metricsStrTrace = []byte("TRACE")
metricsStrPatch = []byte("PATCH")
)
// Metrics is an interface for notifying on multiple events which can
// happen within a framework. For example, open/close connections, some
// method counters etc.
type Metrics interface {
// NewConnection reports new established connection.
NewConnection()
// DropConnection reports connection closed.
DropConnection()
// NewGet reports about new HTTP GET request.
NewGet()
// NewHead reports about new HTTP HEAD request.
NewHead()
// NewPost reports about new HTTP POST request.
NewPost()
// NewPut reports about new HTTP PUT request.
NewPut()
// NewDelete reports about new HTTP DELETE request.
NewDelete()
// NewConnect reports about new HTTP CONNECT request.
NewConnect()
// NewOptions reports about new HTTP OPTIONS request.
NewOptions()
// NewTrace reports about new HTTP TRACE request.
NewTrace()
// NewPatch reports about new HTTP PATCH request.
NewPatch()
// NewOther reports about new HTTP request with rate verb.
NewOther()
// DropGet reports about closed HTTP GET request.
DropGet()
// DropHead reports about closed HTTP HEAD request.
DropHead()
// DropPost reports about closed HTTP POST request.
DropPost()
// DropPut reports about closed HTTP PUT request.
DropPut()
// DropDelete reports about closed HTTP DELETE request.
DropDelete()
// DropConnect reports about closed HTTP CONNECT request.
DropConnect()
// DropOptions reports about closed HTTP OPTIONS request.
DropOptions()
// DropTrace reports about closed HTTP TRACE request.
DropTrace()
// DropPatch reports about closed HTTP PATCH request.
DropPatch()
// DropOther reports about closed HTTP request with rate verb.
DropOther()
// NewCertificate reports about new generated TLS certificate.
NewCertificate()
// DropCertificate reports about pruned TLS certificate.
DropCertificate()
}
// NoopMetrics is a metrics structure which does nothing.
type NoopMetrics struct{}
// NewConnection reports new established connection.
func (n *NoopMetrics) NewConnection() {}
// DropConnection reports connection closed.
func (n *NoopMetrics) DropConnection() {}
// NewGet reports about new HTTP GET request.
func (n *NoopMetrics) NewGet() {}
// NewHead reports about new HTTP HEAD request.
func (n *NoopMetrics) NewHead() {}
// NewPost reports about new HTTP POST request.
func (n *NoopMetrics) NewPost() {}
// NewPut reports about new HTTP PUT request.
func (n *NoopMetrics) NewPut() {}
// NewDelete reports about new HTTP DELETE request.
func (n *NoopMetrics) NewDelete() {}
// NewConnect reports about new HTTP CONNECT request.
func (n *NoopMetrics) NewConnect() {}
// NewOptions reports about new HTTP OPTIONS request.
func (n *NoopMetrics) NewOptions() {}
// NewTrace reports about new HTTP TRACE request.
func (n *NoopMetrics) NewTrace() {}
// NewPatch reports about new HTTP PATCH request.
func (n *NoopMetrics) NewPatch() {}
// NewOther reports about new HTTP request with rate verb.
func (n *NoopMetrics) NewOther() {}
// DropGet reports about closed HTTP GET request.
func (n *NoopMetrics) DropGet() {}
// DropHead reports about closed HTTP HEAD request.
func (n *NoopMetrics) DropHead() {}
// DropPost reports about closed HTTP POST request.
func (n *NoopMetrics) DropPost() {}
// DropPut reports about closed HTTP PUT request.
func (n *NoopMetrics) DropPut() {}
// DropDelete reports about closed HTTP DELETE request.
func (n *NoopMetrics) DropDelete() {}
// DropConnect reports about closed HTTP CONNECT request.
func (n *NoopMetrics) DropConnect() {}
// DropOptions reports about closed HTTP OPTIONS request.
func (n *NoopMetrics) DropOptions() {}
// DropTrace reports about closed HTTP TRACE request.
func (n *NoopMetrics) DropTrace() {}
// DropPatch reports about closed HTTP PATCH request.
func (n *NoopMetrics) DropPatch() {}
// DropOther reports about closed HTTP request with rate verb.
func (n *NoopMetrics) DropOther() {}
// NewCertificate reports about new generated TLS certificate.
func (n *NoopMetrics) NewCertificate() {}
// DropCertificate reports about pruned TLS certificate.
func (n *NoopMetrics) DropCertificate() {}
func newMethodMetricsValue(m Metrics, value []byte) {
switch {
case bytes.Equal(value, metricsStrConnect):
m.NewConnect()
case bytes.Equal(value, metricsStrGet):
m.NewGet()
case bytes.Equal(value, metricsStrOptions):
m.NewOptions()
case bytes.Equal(value, metricsStrPost):
m.NewPost()
case bytes.Equal(value, metricsStrPut):
m.NewPut()
case bytes.Equal(value, metricsStrHead):
m.NewHead()
case bytes.Equal(value, metricsStrDelete):
m.NewDelete()
case bytes.Equal(value, metricsStrTrace):
m.NewTrace()
case bytes.Equal(value, metricsStrPatch):
m.NewPatch()
default:
m.NewOther()
}
}
func dropMethodMetricsValue(m Metrics, value []byte) {
switch {
case bytes.Equal(value, metricsStrConnect):
m.DropConnect()
case bytes.Equal(value, metricsStrGet):
m.DropGet()
case bytes.Equal(value, metricsStrOptions):
m.DropOptions()
case bytes.Equal(value, metricsStrPost):
m.DropPost()
case bytes.Equal(value, metricsStrPut):
m.DropPut()
case bytes.Equal(value, metricsStrHead):
m.DropHead()
case bytes.Equal(value, metricsStrDelete):
m.DropDelete()
case bytes.Equal(value, metricsStrTrace):
m.DropTrace()
case bytes.Equal(value, metricsStrPatch):
m.DropPatch()
default:
m.DropOther()
}
}