-
Notifications
You must be signed in to change notification settings - Fork 0
/
track_http.go
264 lines (222 loc) · 5.98 KB
/
track_http.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
package kero
import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/mileusna/useragent"
)
// 1x1px transparent GIF. Source must be StackOverflow
var Pixel, _ = base64.StdEncoding.DecodeString("R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==")
var PixelSize = int64(len(Pixel))
type TrackedHttpReq struct {
Method string
Path string
Headers http.Header
Query url.Values
Route string
ClientIp string
RemoteAddr string
}
func TrackedRequestFromHttp(httpReq *http.Request) TrackedHttpReq {
return TrackedHttpReq{
Method: httpReq.Method,
Path: httpReq.URL.Path,
Headers: httpReq.Header,
Query: httpReq.URL.Query(),
RemoteAddr: httpReq.RemoteAddr,
}
}
func (k *Kero) ShouldTrackHttpRequest(path string) bool {
if strings.HasPrefix(path, k.DashboardPath) {
return false
}
if len(k.PixelPath) > 1 && k.PixelPath == path {
return false
}
if k.IgnoreCommonPaths {
if path == "/favicon.ico" {
return false
}
for _, prefix := range k.IgnoredPrefixes {
if strings.HasPrefix(path, prefix) {
return false
}
}
for _, suffix := range k.IgnoredSuffixes {
if strings.HasSuffix(path, suffix) {
return false
}
}
}
return true
}
func (k *Kero) TrackWithRequest(metric string, labels MetricLabels, value float64, req TrackedHttpReq) error {
if !k.IgnoreDNT && req.Headers.Get("DNT") == "1" {
return nil
}
clientIp := req.ClientIp
if len(clientIp) == 0 {
clientIp = getClientIp(req.Headers, req.RemoteAddr)
}
allLabels := mergeMaps(
labels,
MetricLabels{
HttpMethodLabel: req.Method,
HttpPathLabel: req.Path,
HttpRouteLabel: req.Route,
},
k.visitorId(clientIp, req.Headers),
k.locationLabels(clientIp),
k.userAgentLabels(req.Headers),
k.referrerLabels(req.Headers),
k.utmLabels(req.Query),
)
if k.IgnoreBots && allLabels[BrowserFormFactorLabel] == FormFactorBot {
return nil
}
return k.TrackOne(metric, allLabels)
}
func (k *Kero) TrackOneWithRequest(metric string, labels MetricLabels, req TrackedHttpReq) error {
return k.TrackWithRequest(metric, labels, 1, req)
}
func (k *Kero) TrackHttpRequest(req TrackedHttpReq) error {
return k.TrackOneWithRequest(HttpReqMetricName, nil, req)
}
func (k *Kero) MeasureHttpRequest(req TrackedHttpReq, handler func()) {
start := time.Now()
defer func() {
duration := time.Since(start)
// duration.Milliseconds() performs integer rounding
ds := float64(duration.Nanoseconds()) / float64(1e6)
labels := MetricLabels{
HttpMethodLabel: req.Method,
HttpPathLabel: req.Path,
HttpRouteLabel: req.Route,
// "$status_code": strconv.Itoa(req.HttpRequest.Response.StatusCode),
}
// fmt.Println("tracked", labels[HttpRouteLabel], labels["$status_code"], )
k.Track(HttpReqDurationMetricName, labels, ds)
}()
handler()
}
func (k *Kero) visitorId(ip string, headers http.Header) MetricLabels {
id := strings.Join([]string{
ip,
headers.Get("user-agent"),
headers.Get("accept-encoding"),
headers.Get("accept-language"),
}, "|")
hash := md5.Sum([]byte(id))
hashString := hex.EncodeToString(hash[:])
return MetricLabels{
VisitorIdLabel: hashString,
}
}
// Building labels for requests
func (k *Kero) locationLabels(clientIp string) MetricLabels {
if !k.reverseLookupIP {
return MetricLabels{}
}
var country, region, city string
if ip := net.ParseIP(clientIp); ip != nil {
if loc, err := k.geoDB.City(ip); err == nil {
country = loc.Country.IsoCode
city = loc.City.Names["en"]
if subdivs := loc.Subdivisions; len(subdivs) > 0 {
region = subdivs[0].Names["en"]
}
}
}
return MetricLabels{
CountryLabel: country,
RegionLabel: region,
CityLabel: city,
}
}
const FormFactorDesktop = "desktop"
const FormFactorMobile = "mobile"
const FormFactorTablet = "tablet"
const FormFactorBot = "bot"
func (k *Kero) userAgentLabels(headers http.Header) MetricLabels {
uaString := headers.Get("user-agent")
ua := useragent.Parse(uaString)
var formFactor string
if ua.Desktop {
formFactor = FormFactorDesktop
}
if ua.Mobile {
formFactor = FormFactorMobile
}
if ua.Tablet {
formFactor = FormFactorTablet
}
if ua.Bot || k.isHttpClientLibrary(uaString) {
formFactor = FormFactorBot
}
return MetricLabels{
BrowserNameLabel: ua.Name,
BrowserVersionLabel: ua.Version,
BrowserDeviceLabel: ua.Device,
BrowserOSLabel: ua.OS,
BrowserOSVersionLabel: ua.OSVersion,
BrowserFormFactorLabel: formFactor,
}
}
func (k *Kero) referrerLabels(headers http.Header) MetricLabels {
var referrerHost string
referrer := headers.Get("referer")
if parsedUrl, err := url.Parse(referrer); err == nil {
referrerHost = parsedUrl.Hostname()
}
return MetricLabels{
ReferrerLabel: referrer,
ReferrerDomainLabel: referrerHost,
}
}
func (k *Kero) utmLabels(queryParams url.Values) MetricLabels {
return MetricLabels{
UTMContentLabel: queryParams.Get("utm_content"),
UTMMediumLabel: queryParams.Get("utm_medium"),
UTMSourceLabel: queryParams.Get("utm_source"),
UTMCampaignLabel: queryParams.Get("utm_campaign"),
UTMTermLabel: queryParams.Get("utm_term"),
ClickIdGoogleLabel: queryParams.Get("gclid"),
ClickIdFbLabel: queryParams.Get("fbclid"),
ClickIdMsLabel: queryParams.Get("msclkid"),
ClickIdTwLabel: queryParams.Get("twclid"),
}
}
func (k *Kero) isHttpClientLibrary(ua string) bool {
if len(ua) == 0 {
return true
}
ua = strings.ToLower(ua)
for _, clientName := range k.IgnoredAgents {
if strings.HasPrefix(ua, clientName) {
return true
}
}
return false
}
func getClientIp(headers http.Header, remoteAddr string) string {
if ip := headers.Get("CF-Connecting-IP"); len(ip) > 1 {
return ip
}
if ff := headers.Get("X-Forwarded-For"); len(ff) > 1 {
if ips := strings.Split(ff, ", "); len(ips) > 0 {
return ips[0]
}
}
if ip := headers.Get("X-Real-IP"); len(ip) > 1 {
return ip
}
if host, _, err := net.SplitHostPort(remoteAddr); err == nil {
return host
}
return ""
}