Skip to content

Commit cda029d

Browse files
HazATrhcarvalho
andauthored
feat: initalScope in SDK Options (#3544)
* feat: initalScope in SDK Options * fix: Tests * Update packages/browser/test/unit/index.test.ts Co-authored-by: Rodolfo Carvalho <[email protected]> * fix: Linter Co-authored-by: Rodolfo Carvalho <[email protected]>
1 parent e59bb03 commit cda029d

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

packages/browser/test/unit/index.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,29 @@ describe('SentryBrowser initialization', () => {
179179
delete global.SENTRY_RELEASE;
180180
});
181181

182+
it('should use initialScope', () => {
183+
init({ dsn, initialScope: { tags: { a: 'b' } } });
184+
expect(global.__SENTRY__.hub._stack[0].scope._tags).to.deep.equal({ a: 'b' });
185+
});
186+
187+
it('should use initalScope Scope', () => {
188+
const scope = new Scope();
189+
scope.setTags({ a: 'b' });
190+
init({ dsn, initialScope: scope });
191+
expect(global.__SENTRY__.hub._stack[0].scope._tags).to.deep.equal({ a: 'b' });
192+
});
193+
194+
it('should use initalScope callback', () => {
195+
init({
196+
dsn,
197+
initialScope: scope => {
198+
scope.setTags({ a: 'b' });
199+
return scope;
200+
},
201+
});
202+
expect(global.__SENTRY__.hub._stack[0].scope._tags).to.deep.equal({ a: 'b' });
203+
});
204+
182205
it('should have initialization proceed as normal if window.SENTRY_RELEASE is not set', () => {
183206
// This is mostly a happy-path test to ensure that the initialization doesn't throw an error.
184207
init({ dsn });

packages/core/src/sdk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function initAndBind<F extends Client, O extends Options>(clientClass: Cl
1717
logger.enable();
1818
}
1919
const hub = getCurrentHub();
20+
hub.getScope()?.update(options.initialScope);
2021
const client = new clientClass(options);
2122
hub.bindClient(client);
2223
}

packages/core/test/lib/sdk.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Scope } from '@sentry/hub';
12
import { Client, Integration } from '@sentry/types';
23

34
import { installedIntegrations } from '../../src/integration';
@@ -16,11 +17,15 @@ jest.mock('@sentry/hub', () => {
1617
getCurrentHub(): {
1718
bindClient(client: Client): boolean;
1819
getClient(): boolean;
20+
getScope(): Scope;
1921
} {
2022
return {
2123
getClient(): boolean {
2224
return false;
2325
},
26+
getScope(): Scope {
27+
return new Scope();
28+
},
2429
bindClient(client: Client): boolean {
2530
client.setupIntegrations();
2631
return true;

packages/types/src/options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Breadcrumb, BreadcrumbHint } from './breadcrumb';
22
import { Event, EventHint } from './event';
33
import { Integration } from './integration';
44
import { LogLevel } from './loglevel';
5+
import { CaptureContext } from './scope';
56
import { SdkMetadata } from './sdkmetadata';
67
import { SamplingContext } from './transaction';
78
import { Transport, TransportClass, TransportOptions } from './transport';
@@ -124,6 +125,11 @@ export interface Options {
124125
*/
125126
autoSessionTracking?: boolean;
126127

128+
/**
129+
* Set data to the inital scope
130+
*/
131+
initialScope?: CaptureContext;
132+
127133
/**
128134
* Set of metadata about the SDK that can be internally used to enhance envelopes and events,
129135
* and provide additional data about every request.

0 commit comments

Comments
 (0)