Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions workers/grouper/src/data-filter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import type { EventAddons, EventData } from '@hawk.so/types';
import { unsafeFields } from '../../../lib/utils/unsafeFields';
import { rightTrim } from '../../../lib/utils/string';

/**
* Maximum depth for object traversal to prevent excessive memory allocations
*/
const MAX_TRAVERSAL_DEPTH = 20;

/**
* Maximum length for event title
*/
const MAX_TITLE_LENGTH = 1000;

/**
* Recursively iterate through object and call function on each key
*
Expand Down Expand Up @@ -142,6 +148,20 @@ export default class DataFilter {
});
}

/**
* Trim event title to the maximum allowed length.
* It mutates the original object.
*
* Should be called before hashing so the hash and the stored title stay consistent.
*
* @param event - event to process
*/
public trimEventTitle(event: EventData<EventAddons>): void {
if (typeof event.title === 'string') {
event.title = rightTrim(event.title, MAX_TITLE_LENGTH);
}
}

/**
* Recursively iterates object and applies filtering to its entries
*
Expand Down
7 changes: 6 additions & 1 deletion workers/grouper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const DB_DUPLICATE_KEY_ERROR = '11000';
const DAILY_METRICS_RETENTION_DAYS = 90;

/**
* Maximum length for backtrace code line or title
* Maximum length for backtrace code line
*/
const MAX_CODE_LINE_LENGTH = 140;

Expand Down Expand Up @@ -209,6 +209,11 @@ export default class GrouperWorker extends Worker {
};
}

/**
* Trim title before hashing so hash and stored title stay consistent
*/
this.dataFilter.trimEventTitle(task.payload);

let uniqueEventHash = await session.measureStep('hash', () => this.getUniqueEventHash(task));
let existedEvent: GroupedEventDBScheme;
let repetitionId = null;
Expand Down
14 changes: 14 additions & 0 deletions workers/grouper/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ describe('GrouperWorker', () => {
expect(await eventsCollection.find().count()).toBe(1);
});

test('Should trim event title to the maximum length before saving', async () => {
const longTitle = 'A'.repeat(5000);

await worker.handle(generateTask({ title: longTitle }));

const savedEvent = await eventsCollection.findOne({});

/**
* 1000 chars + ellipsis
*/
expect(savedEvent.payload.title.length).toBe(1001);
expect(savedEvent.payload.title.endsWith('…')).toBe(true);
});

test('Should increment total events count on each processing', async () => {
await worker.handle(generateTask());
await worker.handle(generateTask());
Expand Down
Loading