Skip to content
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
7 changes: 6 additions & 1 deletion packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
* @inheritDoc
*/
public captureSession(session: Session): void {
if (!this._isEnabled()) {
logger.warn('SDK not enabled, will not capture session.');
return;
}

if (!(typeof session.release === 'string')) {
logger.warn('Discarded session because of missing or non-string release');
} else {
Expand Down Expand Up @@ -485,7 +490,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
const { beforeSend, sampleRate } = this.getOptions();

if (!this._isEnabled()) {
return SyncPromise.reject(new SentryError('SDK not enabled, will not send event.'));
return SyncPromise.reject(new SentryError('SDK not enabled, will not capture event.'));
}

const isTransaction = event.type === 'transaction';
Expand Down
20 changes: 19 additions & 1 deletion packages/core/test/lib/base.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hub, Scope } from '@sentry/hub';
import { Hub, Scope, Session } from '@sentry/hub';
import { Event, Severity, Span } from '@sentry/types';
import { logger, SentryError, SyncPromise } from '@sentry/utils';

Expand Down Expand Up @@ -964,4 +964,22 @@ describe('BaseClient', () => {
]);
});
});

describe('captureSession()', () => {
test('sends sessions to the backend', () => {
expect.assertions(1);
const client = new TestClient({ dsn: PUBLIC_DSN });
const session = new Session({ release: 'test' });
client.captureSession(session);
expect(TestBackend.instance!.session).toEqual(session);
});

test('skips when disabled', () => {
expect.assertions(1);
const client = new TestClient({ enabled: false, dsn: PUBLIC_DSN });
const session = new Session({ release: 'test' });
client.captureSession(session);
expect(TestBackend.instance!.session).toBeUndefined();
});
});
});
6 changes: 6 additions & 0 deletions packages/core/test/mocks/backend.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Session } from '@sentry/hub';
import { Event, Options, Severity, Transport } from '@sentry/types';
import { SyncPromise } from '@sentry/utils';

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

public event?: Event;
public session?: Session;

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

public sendSession(session: Session): void {
this.session = session;
}

protected _setupTransport(): Transport {
if (!this._options.dsn) {
// We return the noop transport here in case there is no Dsn.
Expand Down