Skip to content

Commit

Permalink
fix(plugin-server): use UTC now instead of negative years for timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
bretthoerner committed Oct 16, 2024
1 parent cfb0455 commit e66f85e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
16 changes: 12 additions & 4 deletions plugin-server/src/worker/ingestion/timestamps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,21 @@ export function parseEventTimestamp(data: PluginEvent, callback?: IngestionWarni
parsedTs = now
}

if (!parsedTs.isValid) {
callback?.('ignored_invalid_timestamp', {
const parsedTsOutOfBounds = parsedTs.year < 0 || parsedTs.year > 9999
if (!parsedTs.isValid || parsedTsOutOfBounds) {
const details: Record<string, any> = {
eventUuid: data['uuid'] ?? '',
field: 'timestamp',
value: data['timestamp'] ?? '',
reason: parsedTs.invalidExplanation || 'unknown error',
})
reason: parsedTs.invalidExplanation || (parsedTsOutOfBounds ? 'out of bounds' : 'unknown error'),
}

if (parsedTsOutOfBounds) {
details['offset'] = data['offset']
details['parsed_year'] = parsedTs.year
}

callback?.('ignored_invalid_timestamp', details)
return DateTime.utc()
}

Expand Down
28 changes: 28 additions & 0 deletions plugin-server/tests/worker/ingestion/timestamps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,34 @@ describe('parseEventTimestamp()', () => {
expect(timestamp.toUTC().toISO()).toEqual('2021-10-29T01:43:54.000Z')
})

it('timestamps adjusted way out of bounds are ignored', () => {
const event = {
offset: 600000000000000,
timestamp: '2021-10-28T01:00:00.000Z',
sent_at: '2021-10-28T01:05:00.000Z',
now: '2021-10-28T01:10:00.000Z',
uuid: new UUIDT(),
} as any as PluginEvent

const callbackMock = jest.fn()
const timestamp = parseEventTimestamp(event, callbackMock)
expect(callbackMock.mock.calls).toEqual([
[
'ignored_invalid_timestamp',
{
field: 'timestamp',
eventUuid: event.uuid,
offset: 600000000000000,
parsed_year: -16992,
reason: 'out of bounds',
value: '2021-10-28T01:00:00.000Z',
},
],
])

expect(timestamp.toUTC().toISO()).toEqual('2020-08-12T01:02:00.000Z')
})

it('reports timestamp parsing error and fallbacks to DateTime.utc', () => {
const event = {
team_id: 123,
Expand Down

0 comments on commit e66f85e

Please sign in to comment.