Skip to content

Commit

Permalink
feat: keep earliest event when cache is full (#537)
Browse files Browse the repository at this point in the history
* chore: drop new events when event cache is full

* add doc

* words
  • Loading branch information
williazz committed May 14, 2024
1 parent b9823bd commit 339ab09
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/event-cache/EventCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,11 @@ export class EventCache {
}

if (this.events.length === this.config.eventCacheSize) {
// Make room in the cache by dropping the oldest event.
this.events.shift();
// 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;
}

// The data plane service model (i.e., LogEvents) does not adhere to the
Expand Down
4 changes: 2 additions & 2 deletions src/event-cache/__tests__/EventCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('EventCache tests', () => {
expect(eventCache.getEventBatch()[0].type).toEqual(EVENT2_SCHEMA);
});

test('when cache size reached, recordEvent drops oldest event', async () => {
test('when cache size reached, recordEvent drops the newest event', async () => {
// Init
const EVENT1_SCHEMA = 'com.amazon.rum.event1';
const EVENT2_SCHEMA = 'com.amazon.rum.event2';
Expand All @@ -175,7 +175,7 @@ describe('EventCache tests', () => {
{
id: expect.stringMatching(/[0-9a-f\-]+/),
timestamp: new Date(),
type: EVENT2_SCHEMA,
type: EVENT1_SCHEMA,
metadata: `{"version":"1.0.0","aws:client":"${INSTALL_MODULE}","aws:clientVersion":"${WEB_CLIENT_VERSION}"}`,
details: '{}'
}
Expand Down

0 comments on commit 339ab09

Please sign in to comment.