Skip to content

fix: Send event timestamp #2575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2020
Merged
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
18 changes: 17 additions & 1 deletion packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { Scope } from '@sentry/hub';
import { Client, Event, EventHint, Integration, IntegrationClass, Options, SdkInfo, Severity } from '@sentry/types';
import { Dsn, isPrimitive, isThenable, logger, normalize, SyncPromise, truncate, uuid4 } from '@sentry/utils';
import {
Dsn,
isPrimitive,
isThenable,
logger,
normalize,
SyncPromise,
timestampWithMs,
truncate,
uuid4,
} from '@sentry/utils';

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

const prepared: Event = { ...event };

if (!prepared.timestamp) {
prepared.timestamp = timestampWithMs();
}

if (prepared.environment === undefined && environment !== undefined) {
prepared.environment = environment;
}

if (prepared.release === undefined && release !== undefined) {
prepared.release = release;
}
Expand Down
27 changes: 27 additions & 0 deletions packages/core/test/lib/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ jest.mock('@sentry/utils', () => {
truncate(str: string): string {
return str;
},
timestampWithMs(): number {
return 2020;
},
};
});

Expand Down Expand Up @@ -175,6 +178,7 @@ describe('BaseClient', () => {
},
],
},
timestamp: 2020,
});
});

Expand All @@ -185,6 +189,7 @@ describe('BaseClient', () => {
expect(TestBackend.instance!.event).toEqual({
event_id: '42',
message: 'test message',
timestamp: 2020,
});
});

Expand Down Expand Up @@ -232,6 +237,20 @@ describe('BaseClient', () => {
expect(TestBackend.instance!.event).toEqual({
event_id: '42',
message: 'message',
timestamp: 2020,
});
});

test('does not overwrite existing timestamp', () => {
expect.assertions(2);
const client = new TestClient({ dsn: PUBLIC_DSN });
const scope = new Scope();
client.captureEvent({ message: 'message', timestamp: 1234 }, undefined, scope);
expect(TestBackend.instance!.event!.message).toBe('message');
expect(TestBackend.instance!.event).toEqual({
event_id: '42',
message: 'message',
timestamp: 1234,
});
});

Expand All @@ -243,6 +262,7 @@ describe('BaseClient', () => {
expect(TestBackend.instance!.event!).toEqual({
event_id: 'wat',
message: 'message',
timestamp: 2020,
});
});

Expand All @@ -258,6 +278,7 @@ describe('BaseClient', () => {
environment: 'env',
event_id: '42',
message: 'message',
timestamp: 2020,
});
});

Expand All @@ -273,6 +294,7 @@ describe('BaseClient', () => {
event_id: '42',
message: 'message',
release: 'v1.0.0',
timestamp: 2020,
});
});

Expand Down Expand Up @@ -313,6 +335,7 @@ describe('BaseClient', () => {
extra: { b: 'b' },
message: 'message',
tags: { a: 'a' },
timestamp: 2020,
user: { id: 'user' },
});
});
Expand All @@ -327,6 +350,7 @@ describe('BaseClient', () => {
event_id: '42',
fingerprint: ['abcd'],
message: 'message',
timestamp: 2020,
});
});

Expand Down Expand Up @@ -370,6 +394,7 @@ describe('BaseClient', () => {
contexts: normalizedObject,
event_id: '42',
extra: normalizedObject,
timestamp: 2020,
user: normalizedObject,
});
});
Expand Down Expand Up @@ -414,6 +439,7 @@ describe('BaseClient', () => {
contexts: normalizedObject,
event_id: '42',
extra: normalizedObject,
timestamp: 2020,
user: normalizedObject,
});
});
Expand Down Expand Up @@ -463,6 +489,7 @@ describe('BaseClient', () => {
contexts: normalizedObject,
event_id: '42',
extra: normalizedObject,
timestamp: 2020,
user: normalizedObject,
});
});
Expand Down