forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrpc_analytics_purger.go
135 lines (117 loc) · 3.26 KB
/
rpc_analytics_purger.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
package rpc
import (
"encoding/json"
"time"
msgpack "gopkg.in/vmihailenco/msgpack.v2"
"github.com/TykTechnologies/tyk/storage"
)
type AnalyticsRecord struct {
Method string
Path string
RawPath string
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
RawResponse string
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 {
GeoNameID uint `maxminddb:"geoname_id"`
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"
// RPCPurger will purge analytics data into a Mongo database, requires that the Mongo DB string is specified
// in the Config object
type Purger struct {
Store storage.Handler
}
// Connect Connects to RPC
func (r *Purger) Connect() {
if !clientIsConnected {
Log.Error("RPC client is not connected, use Connect method 1st")
}
// setup RPC func if needed
if !addedFuncs["Ping"] {
dispatcher.AddFunc("Ping", func() bool {
return false
})
addedFuncs["Ping"] = true
}
if !addedFuncs["PurgeAnalyticsData"] {
dispatcher.AddFunc("PurgeAnalyticsData", func(data string) error {
return nil
})
addedFuncs["PurgeAnalyticsData"] = true
}
Log.Info("RPC Analytics client using singleton")
}
// PurgeLoop starts the loop that will pull data out of the in-memory
// store and into RPC.
func (r Purger) PurgeLoop(ticker <-chan time.Time) {
for {
<-ticker
r.PurgeCache()
}
}
// PurgeCache will pull the data from the in-memory store and drop it into the specified MongoDB collection
func (r *Purger) PurgeCache() {
if !clientIsConnected {
Log.Error("RPC client is not connected, use Connect method 1st")
}
if _, err := FuncClientSingleton("Ping", nil); err != nil {
Log.WithError(err).Error("Can't purge cache, failed to ping RPC")
return
}
analyticsValues := r.Store.GetAndDeleteSet(analyticsKeyName)
if len(analyticsValues) == 0 {
return
}
keys := make([]interface{}, len(analyticsValues))
for i, v := range analyticsValues {
decoded := AnalyticsRecord{}
if err := msgpack.Unmarshal(v.([]byte), &decoded); err != nil {
Log.WithError(err).Error("Couldn't unmarshal analytics data")
} else {
Log.WithField("decoded", decoded).Debug("Decoded Record")
keys[i] = decoded
}
}
data, err := json.Marshal(keys)
if err != nil {
Log.WithError(err).Error("Failed to marshal analytics data")
return
}
// Send keys to RPC
if _, err := FuncClientSingleton("PurgeAnalyticsData", string(data)); err != nil {
EmitErrorEvent(FuncClientSingletonCall, "PurgeAnalyticsData", err)
Log.Warn("Failed to call purge, retrying: ", err)
}
}