Skip to content

Commit e7bf856

Browse files
authored
fix: Send event timestamp (getsentry#2575)
1 parent 488a273 commit e7bf856

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

packages/core/src/baseclient.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { Scope } from '@sentry/hub';
22
import { Client, Event, EventHint, Integration, IntegrationClass, Options, SdkInfo, Severity } from '@sentry/types';
3-
import { Dsn, isPrimitive, isThenable, logger, normalize, SyncPromise, truncate, uuid4 } from '@sentry/utils';
3+
import {
4+
Dsn,
5+
isPrimitive,
6+
isThenable,
7+
logger,
8+
normalize,
9+
SyncPromise,
10+
timestampWithMs,
11+
truncate,
12+
uuid4,
13+
} from '@sentry/utils';
414

515
import { Backend, BackendClass } from './basebackend';
616
import { IntegrationIndex, setupIntegrations } from './integration';
@@ -257,9 +267,15 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
257267
const { environment, release, dist, maxValueLength = 250, normalizeDepth = 3 } = this.getOptions();
258268

259269
const prepared: Event = { ...event };
270+
271+
if (!prepared.timestamp) {
272+
prepared.timestamp = timestampWithMs();
273+
}
274+
260275
if (prepared.environment === undefined && environment !== undefined) {
261276
prepared.environment = environment;
262277
}
278+
263279
if (prepared.release === undefined && release !== undefined) {
264280
prepared.release = release;
265281
}

packages/core/test/lib/base.test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ jest.mock('@sentry/utils', () => {
3939
truncate(str: string): string {
4040
return str;
4141
},
42+
timestampWithMs(): number {
43+
return 2020;
44+
},
4245
};
4346
});
4447

@@ -175,6 +178,7 @@ describe('BaseClient', () => {
175178
},
176179
],
177180
},
181+
timestamp: 2020,
178182
});
179183
});
180184

@@ -185,6 +189,7 @@ describe('BaseClient', () => {
185189
expect(TestBackend.instance!.event).toEqual({
186190
event_id: '42',
187191
message: 'test message',
192+
timestamp: 2020,
188193
});
189194
});
190195

@@ -232,6 +237,20 @@ describe('BaseClient', () => {
232237
expect(TestBackend.instance!.event).toEqual({
233238
event_id: '42',
234239
message: 'message',
240+
timestamp: 2020,
241+
});
242+
});
243+
244+
test('does not overwrite existing timestamp', () => {
245+
expect.assertions(2);
246+
const client = new TestClient({ dsn: PUBLIC_DSN });
247+
const scope = new Scope();
248+
client.captureEvent({ message: 'message', timestamp: 1234 }, undefined, scope);
249+
expect(TestBackend.instance!.event!.message).toBe('message');
250+
expect(TestBackend.instance!.event).toEqual({
251+
event_id: '42',
252+
message: 'message',
253+
timestamp: 1234,
235254
});
236255
});
237256

@@ -243,6 +262,7 @@ describe('BaseClient', () => {
243262
expect(TestBackend.instance!.event!).toEqual({
244263
event_id: 'wat',
245264
message: 'message',
265+
timestamp: 2020,
246266
});
247267
});
248268

@@ -258,6 +278,7 @@ describe('BaseClient', () => {
258278
environment: 'env',
259279
event_id: '42',
260280
message: 'message',
281+
timestamp: 2020,
261282
});
262283
});
263284

@@ -273,6 +294,7 @@ describe('BaseClient', () => {
273294
event_id: '42',
274295
message: 'message',
275296
release: 'v1.0.0',
297+
timestamp: 2020,
276298
});
277299
});
278300

@@ -313,6 +335,7 @@ describe('BaseClient', () => {
313335
extra: { b: 'b' },
314336
message: 'message',
315337
tags: { a: 'a' },
338+
timestamp: 2020,
316339
user: { id: 'user' },
317340
});
318341
});
@@ -327,6 +350,7 @@ describe('BaseClient', () => {
327350
event_id: '42',
328351
fingerprint: ['abcd'],
329352
message: 'message',
353+
timestamp: 2020,
330354
});
331355
});
332356

@@ -370,6 +394,7 @@ describe('BaseClient', () => {
370394
contexts: normalizedObject,
371395
event_id: '42',
372396
extra: normalizedObject,
397+
timestamp: 2020,
373398
user: normalizedObject,
374399
});
375400
});
@@ -414,6 +439,7 @@ describe('BaseClient', () => {
414439
contexts: normalizedObject,
415440
event_id: '42',
416441
extra: normalizedObject,
442+
timestamp: 2020,
417443
user: normalizedObject,
418444
});
419445
});
@@ -463,6 +489,7 @@ describe('BaseClient', () => {
463489
contexts: normalizedObject,
464490
event_id: '42',
465491
extra: normalizedObject,
492+
timestamp: 2020,
466493
user: normalizedObject,
467494
});
468495
});

0 commit comments

Comments
 (0)