forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics.go
219 lines (184 loc) · 5.67 KB
/
analytics.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
package main
import (
"fmt"
"net"
"regexp"
"time"
"github.com/jeffail/tunny"
"github.com/oschwald/maxminddb-golang"
"gopkg.in/vmihailenco/msgpack.v2"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/storage"
)
// AnalyticsRecord encodes the details of a request
type AnalyticsRecord struct {
Method string
Path string // HTTP path, can be overriden by "track path" plugin
RawPath string // Original HTTP path
ContentLength int64
UserAgent string
Day int
Month time.Month
Year int
Hour int
ResponseCode int
APIKey string
TimeStamp time.Time
APIVersion string
APIName string
APIID string
OrgID string
OauthID string
RequestTime int64
RawRequest string // Base64 encoded request data (if detailed recording turned on)
RawResponse string // ^ same but for response
IPAddress string
Geo GeoData
Tags []string
Alias string
TrackPath bool
ExpireAt time.Time `bson:"expireAt" json:"expireAt"`
}
type GeoData struct {
Country struct {
ISOCode string `maxminddb:"iso_code"`
} `maxminddb:"country"`
City struct {
Names map[string]string `maxminddb:"names"`
} `maxminddb:"city"`
Location struct {
Latitude float64 `maxminddb:"latitude"`
Longitude float64 `maxminddb:"longitude"`
TimeZone string `maxminddb:"time_zone"`
} `maxminddb:"location"`
}
const analyticsKeyName = "tyk-system-analytics"
func (a *AnalyticsRecord) GetGeo(ipStr string) {
if !config.Global.AnalyticsConfig.EnableGeoIP {
return
}
// Not great, tightly coupled
if analytics.GeoIPDB == nil {
return
}
record, err := geoIPLookup(ipStr)
if err != nil {
log.Error("GeoIP Failure (not recorded): ", err)
return
}
if record == nil {
return
}
log.Debug("ISO Code: ", record.Country.ISOCode)
log.Debug("City: ", record.City.Names["en"])
log.Debug("Lat: ", record.Location.Latitude)
log.Debug("Lon: ", record.Location.Longitude)
log.Debug("TZ: ", record.Location.TimeZone)
a.Geo = *record
}
func geoIPLookup(ipStr string) (*GeoData, error) {
if ipStr == "" {
return nil, nil
}
ip := net.ParseIP(ipStr)
if ip == nil {
return nil, fmt.Errorf("invalid IP address %q", ipStr)
}
record := new(GeoData)
if err := analytics.GeoIPDB.Lookup(ip, record); err != nil {
return nil, fmt.Errorf("geoIPDB lookup of %q failed: %v", ipStr, err)
}
return record, nil
}
func initNormalisationPatterns() (pats config.NormaliseURLPatterns) {
pats.UUIDs = regexp.MustCompile(`[0-9a-fA-F]{8}(-)?[0-9a-fA-F]{4}(-)?[0-9a-fA-F]{4}(-)?[0-9a-fA-F]{4}(-)?[0-9a-fA-F]{12}`)
pats.IDs = regexp.MustCompile(`\/(\d+)`)
for _, pattern := range config.Global.AnalyticsConfig.NormaliseUrls.Custom {
if patRe, err := regexp.Compile(pattern); err != nil {
log.Error("failed to compile custom pattern: ", err)
} else {
pats.Custom = append(pats.Custom, patRe)
}
}
return
}
func (a *AnalyticsRecord) NormalisePath() {
if config.Global.AnalyticsConfig.NormaliseUrls.NormaliseUUIDs {
a.Path = config.Global.AnalyticsConfig.NormaliseUrls.CompiledPatternSet.UUIDs.ReplaceAllString(a.Path, "{uuid}")
}
if config.Global.AnalyticsConfig.NormaliseUrls.NormaliseNumbers {
a.Path = config.Global.AnalyticsConfig.NormaliseUrls.CompiledPatternSet.IDs.ReplaceAllString(a.Path, "/{id}")
}
for _, r := range config.Global.AnalyticsConfig.NormaliseUrls.CompiledPatternSet.Custom {
a.Path = r.ReplaceAllString(a.Path, "{var}")
}
}
func (a *AnalyticsRecord) SetExpiry(expiresInSeconds int64) {
expiry := time.Duration(expiresInSeconds) * time.Second
if expiresInSeconds == 0 {
// Expiry is set to 100 years
expiry = (24 * time.Hour) * (365 * 100)
}
t := time.Now()
t2 := t.Add(expiry)
a.ExpireAt = t2
}
// RedisAnalyticsHandler will record analytics data to a redis back end
// as defined in the Config object
type RedisAnalyticsHandler struct {
Store storage.Handler
Clean Purger
GeoIPDB *maxminddb.Reader
AnalyticsPool *tunny.WorkPool
}
func (r *RedisAnalyticsHandler) Init() {
var err error
if config.Global.AnalyticsConfig.EnableGeoIP {
db, err := maxminddb.Open(config.Global.AnalyticsConfig.GeoIPDBLocation)
if err != nil {
log.Error("Failed to init GeoIP Database: ", err)
} else {
r.GeoIPDB = db
}
}
analytics.Store.Connect()
ps := config.Global.AnalyticsConfig.PoolSize
if ps == 0 {
ps = 50
}
r.AnalyticsPool, err = tunny.CreatePoolGeneric(ps).Open()
if err != nil {
log.Error("Failed to init analytics pool")
}
}
// RecordHit will store an AnalyticsRecord in Redis
func (r *RedisAnalyticsHandler) RecordHit(record AnalyticsRecord) error {
r.AnalyticsPool.SendWork(func() {
// If we are obfuscating API Keys, store the hashed representation (config check handled in hashing function)
record.APIKey = storage.HashKey(record.APIKey)
configMu.Lock()
if config.Global.SlaveOptions.UseRPC {
// Extend tag list to include this data so wecan segment by node if necessary
record.Tags = append(record.Tags, "tyk-hybrid-rpc")
}
if config.Global.DBAppConfOptions.NodeIsSegmented {
// Extend tag list to include this data so wecan segment by node if necessary
record.Tags = append(record.Tags, config.Global.DBAppConfOptions.Tags...)
}
configMu.Unlock()
// Lets add some metadata
if record.APIKey != "" {
record.Tags = append(record.Tags, "key-"+record.APIKey)
}
if record.OrgID != "" {
record.Tags = append(record.Tags, "org-"+record.OrgID)
}
record.Tags = append(record.Tags, "api-"+record.APIID)
encoded, err := msgpack.Marshal(record)
if err != nil {
log.Error("Error encoding analytics data: ", err)
}
r.Store.AppendToSet(analyticsKeyName, string(encoded))
})
return nil
}