forked from launchdarkly/node-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_processor.js
237 lines (214 loc) · 6.63 KB
/
event_processor.js
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
var LRUCache = require('lrucache');
var request = require('request');
var EventSummarizer = require('./event_summarizer');
var UserFilter = require('./user_filter');
var errors = require('./errors');
var wrapPromiseCallback = require('./utils/wrapPromiseCallback');
function EventProcessor(sdkKey, config, errorReporter) {
var ep = {};
var userFilter = UserFilter(config),
summarizer = EventSummarizer(config),
userKeysCache = LRUCache(config.userKeysCapacity),
queue = [],
lastKnownPastTime = 0,
exceededCapacity = false,
shutdown = false,
flushTimer,
flushUsersTimer;
function enqueue(event) {
if (queue.length < config.capacity) {
queue.push(event);
exceededCapacity = false;
} else {
if (!exceededCapacity) {
exceededCapacity = true;
config.logger.warn("Exceeded event queue capacity. Increase capacity to avoid dropping events.");
}
}
}
function shouldDebugEvent(event) {
if (event.debugEventsUntilDate) {
if (event.debugEventsUntilDate > lastKnownPastTime &&
event.debugEventsUntilDate > new Date().getTime()) {
return true;
}
}
return false;
}
function makeOutputEvent(event) {
switch (event.kind) {
case 'feature':
debug = !!event.debug;
var out = {
kind: debug ? 'debug' : 'feature',
creationDate: event.creationDate,
key: event.key,
value: event.value,
default: event.default,
prereqOf: event.prereqOf
};
if (event.variation !== undefined && event.variation !== null) {
out.variation = event.variation;
}
if (event.version) {
out.version = event.version;
}
if (config.inlineUsersInEvents || debug) {
out.user = userFilter.filterUser(event.user);
} else {
out.userKey = event.user && event.user.key;
}
return out;
case 'identify':
return {
kind: 'identify',
creationDate: event.creationDate,
key: event.user && event.user.key,
user: userFilter.filterUser(event.user)
};
case 'custom':
var out = {
kind: 'custom',
creationDate: event.creationDate,
key: event.key,
data: event.data
};
if (config.inlineUsersInEvents) {
out.user = userFilter.filterUser(event.user);
} else {
out.userKey = event.user && event.user.key;
}
return out;
default:
return event;
}
}
ep.sendEvent = function(event) {
var addIndexEvent = false,
addFullEvent = false,
addDebugEvent = false;
if (shutdown) {
return;
}
// Always record the event in the summarizer.
summarizer.summarizeEvent(event);
// Decide whether to add the event to the payload. Feature events may be added twice, once for
// the event (if tracked) and once for debugging.
if (event.kind === 'feature') {
addFullEvent = event.trackEvents;
addDebugEvent = shouldDebugEvent(event);
} else {
addFullEvent = true;
}
// For each user we haven't seen before, we add an index event - unless this is already
// an identify event for that user.
if (!addFullEvent || !config.inlineUsersInEvents) {
if (event.user && !userKeysCache.get(event.user.key)) {
userKeysCache.set(event.user.key, true);
if (event.kind != 'identify') {
addIndexEvent = true;
}
}
}
if (addIndexEvent) {
enqueue({
kind: 'index',
creationDate: event.creationDate,
user: userFilter.filterUser(event.user)
});
}
if (addFullEvent) {
enqueue(makeOutputEvent(event));
}
if (addDebugEvent) {
var debugEvent = Object.assign({}, event, { debug: true });
enqueue(makeOutputEvent(debugEvent));
}
}
ep.flush = function(callback) {
return wrapPromiseCallback(new Promise(function(resolve, reject) {
var worklist;
var summary;
if (shutdown) {
var err = new errors.LDInvalidSDKKeyError("Events cannot be posted because SDK key is invalid");
reject(err);
return;
}
worklist = queue;
queue = [];
summary = summarizer.getSummary();
summarizer.clearSummary();
if (Object.keys(summary.features).length) {
summary.kind = 'summary';
worklist.push(summary);
}
if (!worklist.length) {
resolve();
return;
}
config.logger.debug("Flushing %d events", worklist.length);
tryPostingEvents(worklist, resolve, reject, true);
}.bind(this)), callback);
}
function tryPostingEvents(events, resolve, reject, canRetry) {
var retryOrReject = function(err) {
if (canRetry) {
config.logger && config.logger.warn("Will retry posting events after 1 second");
setTimeout(function() {
tryPostingEvents(events, resolve, reject, false);
}, 1000);
} else {
reject(err);
}
}
request({
method: "POST",
url: config.eventsUri + '/bulk',
headers: {
'Authorization': sdkKey,
'User-Agent': config.userAgent,
'X-LaunchDarkly-Event-Schema': '3'
},
json: true,
body: events,
timeout: config.timeout * 1000,
agent: config.proxyAgent
}).on('response', function(resp, body) {
if (resp.headers['date']) {
var date = Date.parse(resp.headers['date']);
if (date) {
lastKnownPastTime = date;
}
}
if (resp.statusCode > 204) {
var err = new errors.LDUnexpectedResponseError("Unexpected status code " + resp.statusCode + "; events may not have been processed",
resp.statusCode);
errorReporter && errorReporter(err);
if (resp.statusCode === 401) {
reject(err);
var err1 = new errors.LDInvalidSDKKeyError("Received 401 error, no further events will be posted since SDK key is invalid");
errorReporter && errorReporter(err1);
shutdown = true;
} else if (resp.statusCode >= 500) {
retryOrReject(err);
}
} else {
resolve(resp, body);
}
}).on('error', function(err) {
retryOrReject(err);
});
}
ep.close = function() {
clearInterval(flushTimer);
clearInterval(flushUsersTimer);
}
flushTimer = setInterval(function() {
ep.flush();
}, config.flushInterval * 1000);
flushUsersTimer = setInterval(function() {
userKeysCache.removeAll();
}, config.userKeysFlushInterval * 1000);
return ep;
}
module.exports = EventProcessor;