Skip to content

Commit e675c79

Browse files
authored
fix(sessions): Do not track sessions if not enabled (#3686)
Fixes #3685 The patch updates the @sentry/core baseClient to not send sessions to Sentry if it is disabled. This makes sessions match the behaviour of events and integrations (both of which get disabled when the enabled option is set to false).
1 parent aaabc50 commit e675c79

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

packages/core/src/baseclient.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
152152
* @inheritDoc
153153
*/
154154
public captureSession(session: Session): void {
155+
if (!this._isEnabled()) {
156+
logger.warn('SDK not enabled, will not capture session.');
157+
return;
158+
}
159+
155160
if (!(typeof session.release === 'string')) {
156161
logger.warn('Discarded session because of missing or non-string release');
157162
} else {
@@ -485,7 +490,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
485490
const { beforeSend, sampleRate } = this.getOptions();
486491

487492
if (!this._isEnabled()) {
488-
return SyncPromise.reject(new SentryError('SDK not enabled, will not send event.'));
493+
return SyncPromise.reject(new SentryError('SDK not enabled, will not capture event.'));
489494
}
490495

491496
const isTransaction = event.type === 'transaction';

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Hub, Scope } from '@sentry/hub';
1+
import { Hub, Scope, Session } from '@sentry/hub';
22
import { Event, Severity, Span } from '@sentry/types';
33
import { logger, SentryError, SyncPromise } from '@sentry/utils';
44

@@ -964,4 +964,22 @@ describe('BaseClient', () => {
964964
]);
965965
});
966966
});
967+
968+
describe('captureSession()', () => {
969+
test('sends sessions to the backend', () => {
970+
expect.assertions(1);
971+
const client = new TestClient({ dsn: PUBLIC_DSN });
972+
const session = new Session({ release: 'test' });
973+
client.captureSession(session);
974+
expect(TestBackend.instance!.session).toEqual(session);
975+
});
976+
977+
test('skips when disabled', () => {
978+
expect.assertions(1);
979+
const client = new TestClient({ enabled: false, dsn: PUBLIC_DSN });
980+
const session = new Session({ release: 'test' });
981+
client.captureSession(session);
982+
expect(TestBackend.instance!.session).toBeUndefined();
983+
});
984+
});
967985
});

packages/core/test/mocks/backend.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Session } from '@sentry/hub';
12
import { Event, Options, Severity, Transport } from '@sentry/types';
23
import { SyncPromise } from '@sentry/utils';
34

@@ -14,6 +15,7 @@ export class TestBackend extends BaseBackend<TestOptions> {
1415
public static sendEventCalled?: (event: Event) => void;
1516

1617
public event?: Event;
18+
public session?: Session;
1719

1820
public constructor(protected readonly _options: TestOptions) {
1921
super(_options);
@@ -50,6 +52,10 @@ export class TestBackend extends BaseBackend<TestOptions> {
5052
TestBackend.sendEventCalled && TestBackend.sendEventCalled(event);
5153
}
5254

55+
public sendSession(session: Session): void {
56+
this.session = session;
57+
}
58+
5359
protected _setupTransport(): Transport {
5460
if (!this._options.dsn) {
5561
// We return the noop transport here in case there is no Dsn.

0 commit comments

Comments
 (0)