forked from aws-observability/aws-rum-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventCache.ts
More file actions
392 lines (355 loc) · 13 KB
/
EventCache.ts
File metadata and controls
392 lines (355 loc) · 13 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import { Session, SessionManager } from '../sessions/SessionManager';
import { v4 } from 'uuid';
import { MetaData } from '../events/meta-data';
import { Config } from '../orchestration/Orchestration';
import { PageAttributes, PageManager } from '../sessions/PageManager';
import {
AppMonitorDetails,
UserDetails,
RumEvent,
ParsedRumEvent
} from '../dispatch/dataplane';
import EventBus, { Topic } from '../event-bus/EventBus';
import { RecordEvent } from '../plugins/types';
import { SESSION_START_EVENT_TYPE } from '../plugins/utils/constant';
import { InternalLogger } from '../utils/InternalLogger';
const webClientVersion = '1.26.0';
/**
* A cache which stores events generated by telemetry plugins.
*
* The event cache stores meta data and events until they are dispatched to the
* data plane. The event cache removes the oldest event once the cache is full
* and a new event is added.
*/
export class EventCache {
private appMonitorDetails: AppMonitorDetails;
private config: Config;
private events: RumEvent[] = [];
private candidates = new Map<string, RumEvent>();
private sessionManager: SessionManager;
private pageManager: PageManager;
private enabled: boolean;
private installationMethod: string;
// Enable config.debug mode
private droppedEvent = 0; // tracks dropped event count due to insufficient `eventCache`
private sessionLimitExceeded = 0; // tracks dropped event count due to insufficieent `sessionEventLimit`
/**
* @param applicationDetails Application identity and version.
* @param batchLimit The maximum number of events that will be returned in a batch.
* @param eventCacheSize The maximum number of events the cache can contain before dropping events.
* @param sessionManager The sessionManager returns user id, session id and handles session timeout.
* @param pageManager The pageManager returns page id.
*/
constructor(
applicationDetails: AppMonitorDetails,
config: Config,
private eventBus = new EventBus<Topic>()
) {
this.appMonitorDetails = applicationDetails;
this.config = config;
this.enabled = true;
this.pageManager = new PageManager(config, this.recordEvent);
this.sessionManager = new SessionManager(
applicationDetails,
config,
this.recordEvent,
this.pageManager
);
this.installationMethod = config.client;
}
/**
* The event cache will record new events or new meta data.
*/
public enable(): void {
this.enabled = true;
}
/**
* The event cache will not record new events or new meta data. Events and
* meta data which are already in the cache will still be accessible.
*/
public disable(): void {
this.enabled = false;
}
/**
* Update the current page interaction for the session.
*/
public recordPageView = (payload: string | PageAttributes) => {
if (this.isCurrentUrlAllowed()) {
this.pageManager.recordPageView(payload);
}
};
/**
* Returns true if the session is sampled, false otherwise.
*/
public isSessionSampled(): boolean {
return this.sessionManager.isSampled();
}
/**
* Add an event to the cache and reset the session timer.
*
* If the session is being recorded, the event will be recorded.
* If the session is not being recorded, the event will not be recorded.
*
* @param type The event schema.
* @param eventData The event data.
*/
public recordEvent: RecordEvent = (type: string, eventData: object) => {
if (!this.enabled) {
return;
}
if (this.isCurrentUrlAllowed()) {
if (type !== SESSION_START_EVENT_TYPE) {
// Only refresh session if not session start event
// to avoid recursive loop.
this.sessionManager.getSession();
}
if (this.sessionManager.canRecord()) {
this.sessionManager.incrementSessionEventCount();
this.addRecordToCache(type, eventData);
} else if (this.config.debug) {
this.sessionLimitExceeded++;
}
}
};
/**
* Adds a candidate to the cache and reset session timer
*
* @param eventType The event schema.
* @param eventData The event data.
*/
public recordCandidate: RecordEvent = (
eventType: string,
eventData: object
) => {
const session = this.sessionManager.getSession();
if (!this.enabled || !this.isCurrentUrlAllowed() || !session.record) {
return;
}
const [event] = this.createEvent(eventType, eventData);
// Update candidate if exists
if (this.candidates.has(eventType)) {
this.candidates.set(eventType, event);
return;
}
// Record new candidate only if limits have not been reached
if (
this.candidates.size < this.config.candidatesCacheSize &&
!this.sessionManager.isLimitExceeded()
) {
this.candidates.set(eventType, event);
this.sessionManager.incrementSessionEventCount();
}
};
/**
* Returns the current session (1) if a session exists and (2) if the
* current URL is allowed. Returns undefined otherwise.
*/
public getSession = (): Session | undefined => {
if (this.isCurrentUrlAllowed()) {
return this.sessionManager.getSession();
}
return undefined;
};
/**
* Returns true if there are one or more events in the cache.
*/
public hasEvents(): boolean {
// For debug mode
if (this.config.debug && this.sessionLimitExceeded > 0) {
const total = this.events.length + this.sessionLimitExceeded;
if (this.sessionManager.isSampled()) {
InternalLogger.warn(
`Dropped ${
this.sessionLimitExceeded
} of ${total} recently observed events (${(
(this.sessionLimitExceeded / total) *
100
).toFixed(
2
)}%) because the session limit has exceeded. Consider increasing sessionEventLimit (currently ${
this.config.sessionEventLimit
}) to ${total} or more to avoid data loss.`
);
} else {
InternalLogger.warn(
`Dropped ${
this.sessionLimitExceeded
} of ${total} recently observed events (${(
(this.sessionLimitExceeded / total) *
100
).toFixed(
2
)}%) because current session is not sampled. Consider increasing sessionSampleRate (currently ${
this.config.sessionSampleRate
}) to capture more user sessions.`
);
}
// reset
this.sessionLimitExceeded = 0;
}
return this.events.length !== 0;
}
/**
* Returns true if there are one or more event candidates in the cache.
*/
public hasCandidates() {
return this.candidates.size !== 0;
}
/**
* Removes and returns the next batch of events.
*/
public getEventBatch(flushCandidates = false): RumEvent[] {
if (this.config.debug && this.droppedEvent > 0) {
const total = this.events.length + this.droppedEvent;
InternalLogger.warn(
`Dropped ${
this.droppedEvent
} of ${total} recently observed events (${(
(this.droppedEvent / total) *
100
).toFixed(
2
)}%) due to insufficient in-memory queue. Increase eventCacheSize to ${total} (currently ${
this.config.eventCacheSize
}) to avoid data loss.`
);
// reset
this.droppedEvent = 0;
}
let batch: RumEvent[] = [];
// Prioritize candidates in the next event batch
if (flushCandidates && this.hasCandidates()) {
// Pull all candidates if they fit in the batch
if (this.candidates.size <= this.config.batchLimit) {
batch = Array.from(this.candidates.values());
this.candidates.clear();
} else {
// Pull candidates in FIFO order until batch limit is reached
let i = 0;
for (const key of Array.from(this.candidates.keys())) {
if (i++ >= this.config.batchLimit) {
break;
}
const event = this.candidates.get(key);
if (event) {
batch.push(event);
this.candidates.delete(key);
}
}
}
}
// Use remaining capacity for regular events.
if (this.events.length) {
if (this.events.length <= this.config.batchLimit - batch.length) {
batch.push(...this.events);
this.events = [];
} else {
// Dispatch the front of the array and retain the back of the array.
batch.push(
...this.events.splice(
0,
this.config.batchLimit - batch.length
)
);
}
}
return batch;
}
/**
* Returns an object containing the AppMonitor ID and application version.
*/
public getAppMonitorDetails(): AppMonitorDetails {
return this.appMonitorDetails;
}
/**
* Returns an object containing the session ID and user ID.
*/
public getUserDetails(): UserDetails {
return {
userId: this.sessionManager.getUserId(),
sessionId: this.sessionManager.getSession().sessionId
};
}
/**
* Set custom session attributes to add them to all event metadata.
*
* @param payload object containing custom attribute data in the form of key, value pairs
*/
public addSessionAttributes(sessionAttributes: {
[k: string]: string | number | boolean;
}): void {
this.sessionManager.addSessionAttributes(sessionAttributes);
}
/**
* Add an event to the cache.
*
* @param type The event schema.
*/
private addRecordToCache = (type: string, eventData: object) => {
if (!this.enabled) {
return;
}
if (this.events.length >= this.config.eventCacheSize) {
if (this.config.debug) {
this.droppedEvent += 1;
}
// Drop newest event and keep the older ones
// 1. Older events tend to be more relevant, such as session start
// or performance entries that are attributed to web vitals
// 2. Dropping an old event requires linear time
return;
}
const [event, parsedEvent] = this.createEvent(type, eventData);
this.eventBus.dispatch(Topic.EVENT, parsedEvent);
this.events.push(event);
};
/** Creates a RumEvent and a ParsedRumEvent from a type and details. */
private createEvent = (
type: string,
details: object
): [RumEvent, ParsedRumEvent] => {
// The data plane service model (i.e., LogEvents) does not adhere to the
// RUM agent data model, where sessions and pages are first class
// objects with their own attribute sets. Instead, we store session
// attributes and page attributes together as 'meta data'.
const metadata = {
...this.sessionManager.getAttributes(),
...this.pageManager.getAttributes(),
version: '1.0.0',
'aws:client': this.installationMethod,
'aws:clientVersion': webClientVersion
} as MetaData;
const partialEvent = {
id: v4(),
timestamp: new Date(),
type
};
return [
{
...partialEvent,
details: JSON.stringify(details),
metadata: JSON.stringify(metadata)
} as RumEvent,
{
...partialEvent,
details,
metadata
} as ParsedRumEvent
];
};
/**
* Returns {@code true} if the current url matches one of the allowedPages
* and does not match any of the deniedPages; returns {@code false}
* otherwise.
*/
private isCurrentUrlAllowed() {
const location = document.location.toString();
const exclude = this.config.pagesToExclude.some((re) =>
re.test(location)
);
const include = this.config.pagesToInclude.some((re) =>
re.test(location)
);
return include && !exclude;
}
}