This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
forked from TykTechnologies/tyk-pump
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
165 lines (133 loc) · 4.11 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
package main
import (
"flag"
"time"
"github.com/TykTechnologies/logrus"
prefixed "github.com/TykTechnologies/logrus-prefixed-formatter"
"github.com/TykTechnologies/tyk-pump/analytics"
"github.com/TykTechnologies/tyk-pump/pumps"
"github.com/TykTechnologies/tyk-pump/storage"
logger "github.com/TykTechnologies/tykcommon-logger"
"gopkg.in/vmihailenco/msgpack.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 string = "main"
func init() {
SystemConfig = TykPumpConfiguration{}
confFile := flag.String("c", "pump.conf", "Path to the config file")
flag.Parse()
log.Formatter = new(prefixed.TextFormatter)
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Info("## Tyk Analytics Pump, ", VERSION, " ##")
LoadConfig(confFile, &SystemConfig)
}
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 initialisePumps() {
Pumps = make([]pumps.Pump, len(SystemConfig.Pumps))
i := 0
for name, pmp := range SystemConfig.Pumps {
pmpType, err := pumps.GetPumpByName(name)
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())
Pumps[i] = thisPmp
}
}
i++
}
if !SystemConfig.DontPurgeUptimeData {
UptimePump = pumps.MongoPump{}
UptimePump.Init(SystemConfig.UptimePumpConfig)
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Info("Init Uptime Pump: ", UptimePump.GetName())
}
}
func StartPurgeLoop(nextCount int) {
job := instrument.NewJob("PumpRecordsPurge")
time.Sleep(time.Duration(nextCount) * time.Second)
AnalyticsValues := AnalyticsStore.GetAndDeleteSet(storage.ANALYTICS_KEYNAME)
if len(AnalyticsValues) > 0 {
startTime := time.Now()
// Convert to something clean
keys := make([]interface{}, len(AnalyticsValues), len(AnalyticsValues))
for i, v := range AnalyticsValues {
decoded := analytics.AnalyticsRecord{}
err := msgpack.Unmarshal(v.([]byte), &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 {
keys[i] = interface{}(decoded)
job.Event("record")
}
}
// Send to pumps
if Pumps != nil {
for _, pmp := range Pumps {
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Debug("Writing to: ", pmp.GetName())
pmp.WriteData(keys)
job.Timing("purge_time_"+pmp.GetName(), time.Since(startTime).Nanoseconds())
}
} else {
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Warning("No pumps defined!")
}
job.Timing("purge_time_all", time.Since(startTime).Nanoseconds())
}
if !SystemConfig.DontPurgeUptimeData {
UptimeValues := UptimeStorage.GetAndDeleteSet(storage.UptimeAnalytics_KEYNAME)
UptimePump.WriteUptimeData(UptimeValues)
}
StartPurgeLoop(nextCount)
}
func main() {
// Create the store
setupAnalyticsStore()
// prime the pumps
initialisePumps()
// start the worker loop
log.WithFields(logrus.Fields{
"prefix": mainPrefix,
}).Info("Starting purge loop @", SystemConfig.PurgeDelay, "(s)")
SetupInstrumentation()
StartPurgeLoop(SystemConfig.PurgeDelay)
}