forked from TykTechnologies/tyk-pump
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
342 lines (290 loc) · 9.65 KB
/
main.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package main
import (
"context"
"strings"
"sync"
"time"
"os"
"github.com/gocraft/health"
msgpack "gopkg.in/vmihailenco/msgpack.v2"
"github.com/TykTechnologies/logrus"
prefixed "github.com/TykTechnologies/logrus-prefixed-formatter"
"github.com/TykTechnologies/tyk-pump/analytics"
"github.com/TykTechnologies/tyk-pump/analytics/demo"
logger "github.com/TykTechnologies/tyk-pump/logger"
"github.com/TykTechnologies/tyk-pump/pumps"
"github.com/TykTechnologies/tyk-pump/server"
"github.com/TykTechnologies/tyk-pump/storage"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var SystemConfig TykPumpConfiguration
var AnalyticsStore storage.AnalyticsStorage
var UptimeStorage storage.AnalyticsStorage
var Pumps []pumps.Pump
var UptimePump pumps.MongoPump
var log = logger.GetLogger()
var mainPrefix = "main"
var (
help = kingpin.CommandLine.HelpFlag.Short('h')
conf = kingpin.Flag("conf", "path to the config file").Short('c').Default("pump.conf").String()
demoMode = kingpin.Flag("demo", "pass orgID string to generate demo data").Default("").String()
demoApiMode = kingpin.Flag("demo-api", "pass apiID string to generate demo data").Default("").String()
demoApiVersionMode = kingpin.Flag("demo-api-version", "pass apiID string to generate demo data").Default("").String()
debugMode = kingpin.Flag("debug", "enable debug mode").Bool()
version = kingpin.Version(VERSION)
)
func Init() {
SystemConfig = TykPumpConfiguration{}
kingpin.Parse()
log.Formatter = new(prefixed.TextFormatter)
envDemo := os.Getenv("TYK_PMP_BUILDDEMODATA")
if envDemo != "" {
log.Warning("Demo mode active via environemnt variable")
demoMode = &envDemo
}
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Info("## Tyk Analytics Pump, ", VERSION, " ##")
LoadConfig(conf, &SystemConfig)
// If no environment variable is set, check the configuration file:
if os.Getenv("TYK_LOGLEVEL") == "" {
level := strings.ToLower(SystemConfig.LogLevel)
switch level {
case "", "info":
// default, do nothing
case "error":
log.Level = logrus.ErrorLevel
case "warn":
log.Level = logrus.WarnLevel
case "debug":
log.Level = logrus.DebugLevel
default:
log.WithFields(logrus.Fields{
"prefix": "main",
}).Fatalf("Invalid log level %q specified in config, must be error, warn, debug or info. ", level)
}
}
// If debug mode flag is set, override previous log level parameter:
if *debugMode {
log.Level = logrus.DebugLevel
}
}
func setupAnalyticsStore() {
switch SystemConfig.AnalyticsStorageType {
case "redis":
AnalyticsStore = &storage.RedisClusterStorageManager{}
UptimeStorage = &storage.RedisClusterStorageManager{}
default:
AnalyticsStore = &storage.RedisClusterStorageManager{}
UptimeStorage = &storage.RedisClusterStorageManager{}
}
AnalyticsStore.Init(SystemConfig.AnalyticsStorageConfig)
// Copy across the redis configuration
uptimeConf := SystemConfig.AnalyticsStorageConfig
// Swap key prefixes for uptime purger
uptimeConf.RedisKeyPrefix = "host-checker:"
UptimeStorage.Init(uptimeConf)
}
func storeVersion() {
var versionStore = &storage.RedisClusterStorageManager{}
versionConf := SystemConfig.AnalyticsStorageConfig
versionStore.KeyPrefix = "version-check-"
versionStore.Config = versionConf
versionStore.Connect()
versionStore.SetKey("pump", VERSION, 0)
}
func initialisePumps() {
Pumps = make([]pumps.Pump, len(SystemConfig.Pumps))
i := 0
for key, pmp := range SystemConfig.Pumps {
pumpTypeName := pmp.Type
if pumpTypeName == "" {
pumpTypeName = key
}
pmpType, err := pumps.GetPumpByName(pumpTypeName)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Error("Pump load error (skipping): ", err)
} else {
thisPmp := pmpType.New()
initErr := thisPmp.Init(pmp.Meta)
if initErr != nil {
log.Error("Pump init error (skipping): ", initErr)
} else {
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Info("Init Pump: ", thisPmp.GetName())
thisPmp.SetFilters(pmp.Filters)
thisPmp.SetTimeout(pmp.Timeout)
thisPmp.SetOmitDetailedRecording(pmp.OmitDetailedRecording)
Pumps[i] = thisPmp
}
}
i++
}
if !SystemConfig.DontPurgeUptimeData {
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Info("'dont_purge_uptime_data' set to false, attempting to start Uptime pump! ", UptimePump.GetName())
UptimePump = pumps.MongoPump{}
UptimePump.Init(SystemConfig.UptimePumpConfig)
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Info("Init Uptime Pump: ", UptimePump.GetName())
}
}
func StartPurgeLoop(secInterval int, omitDetails bool) {
for range time.Tick(time.Duration(secInterval) * time.Second) {
job := instrument.NewJob("PumpRecordsPurge")
AnalyticsValues := AnalyticsStore.GetAndDeleteSet(storage.ANALYTICS_KEYNAME)
if len(AnalyticsValues) > 0 {
startTime := time.Now()
// Convert to something clean
keys := make([]interface{}, len(AnalyticsValues))
for i, v := range AnalyticsValues {
decoded := analytics.AnalyticsRecord{}
err := msgpack.Unmarshal([]byte(v.(string)), &decoded)
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Debug("Decoded Record: ", decoded)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Error("Couldn't unmarshal analytics data:", err)
} else {
if omitDetails {
decoded.RawRequest = ""
decoded.RawResponse = ""
}
keys[i] = interface{}(decoded)
job.Event("record")
}
}
// Send to pumps
writeToPumps(keys, job, startTime, secInterval)
job.Timing("purge_time_all", time.Since(startTime).Nanoseconds())
}
if !SystemConfig.DontPurgeUptimeData {
UptimeValues := UptimeStorage.GetAndDeleteSet(storage.UptimeAnalytics_KEYNAME)
UptimePump.WriteUptimeData(UptimeValues)
}
}
}
func writeToPumps(keys []interface{}, job *health.Job, startTime time.Time, purgeDelay int) {
// Send to pumps
if Pumps != nil {
var wg sync.WaitGroup
wg.Add(len(Pumps))
for _, pmp := range Pumps {
go execPumpWriting(&wg, pmp, &keys, purgeDelay, startTime, job)
}
wg.Wait()
} else {
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Warning("No pumps defined!")
}
}
func filterData(pump pumps.Pump, keys []interface{}) []interface{} {
filters := pump.GetFilters()
if !filters.HasFilter() && !pump.GetOmitDetailedRecording() {
return keys
}
filteredKeys := keys[:]
newLenght := 0
for _, key := range filteredKeys {
decoded := key.(analytics.AnalyticsRecord)
if pump.GetOmitDetailedRecording() {
decoded.RawRequest = ""
decoded.RawResponse = ""
}
if filters.ShouldFilter(decoded) {
continue
}
filteredKeys[newLenght] = decoded
newLenght++
}
filteredKeys = filteredKeys[:newLenght]
return filteredKeys
}
func execPumpWriting(wg *sync.WaitGroup, pmp pumps.Pump, keys *[]interface{}, purgeDelay int, startTime time.Time, job *health.Job) {
timer := time.AfterFunc(time.Duration(purgeDelay)*time.Second, func() {
if pmp.GetTimeout() == 0 {
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Warning("Pump ", pmp.GetName(), " is taking more time than the value configured of purge_delay. You should try to set a timeout for this pump.")
} else if pmp.GetTimeout() > purgeDelay {
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Warning("Pump ", pmp.GetName(), " is taking more time than the value configured of purge_delay. You should try lowering the timeout configured for this pump.")
}
})
defer timer.Stop()
defer wg.Done()
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Debug("Writing to: ", pmp.GetName())
ch := make(chan error, 1)
//Load pump timeout
timeout := pmp.GetTimeout()
var ctx context.Context
var cancel context.CancelFunc
//Initialize context depending if the pump has a configured timeout
if timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
} else {
ctx, cancel = context.WithCancel(context.Background())
}
defer cancel()
go func(ch chan error, ctx context.Context, pmp pumps.Pump, keys *[]interface{}) {
filteredKeys := filterData(pmp, *keys)
ch <- pmp.WriteData(ctx, filteredKeys)
}(ch, ctx, pmp, keys)
select {
case err := <-ch:
if err != nil {
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Warning("Error Writing to: ", pmp.GetName(), " - Error:", err)
}
case <-ctx.Done():
switch ctx.Err() {
case context.Canceled:
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Warning("The writing to ", pmp.GetName(), " have got canceled.")
case context.DeadlineExceeded:
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Warning("Timeout Writing to: ", pmp.GetName())
}
}
if job != nil {
job.Timing("purge_time_"+pmp.GetName(), time.Since(startTime).Nanoseconds())
}
}
func main() {
Init()
SetupInstrumentation()
go server.ServeHealthCheck(SystemConfig.HealthCheckEndpointName, SystemConfig.HealthCheckEndpointPort)
// Store version which will be read by dashboard and sent to
// vclu(version check and licecnse utilisation) service
storeVersion()
// Create the store
setupAnalyticsStore()
// prime the pumps
initialisePumps()
if *demoMode != "" {
log.Warning("BUILDING DEMO DATA AND EXITING...")
log.Warning("Starting from date: ", time.Now().AddDate(0, 0, -30))
demo.DemoInit(*demoMode, *demoApiMode, *demoApiVersionMode)
demo.GenerateDemoData(time.Now().AddDate(0, 0, -30), 30, *demoMode, writeToPumps)
return
}
// start the worker loop
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Info("Starting purge loop @", SystemConfig.PurgeDelay, "(s)")
StartPurgeLoop(SystemConfig.PurgeDelay, SystemConfig.OmitDetailedRecording)
}