From 70c35ea2a7581ed7f79fa5f2f8fae5b38cdeeffb Mon Sep 17 00:00:00 2001 From: Daniel Griesser Date: Fri, 14 May 2021 13:59:05 +0200 Subject: [PATCH 1/4] feat: initalScope in SDK Options --- packages/browser/test/unit/index.test.ts | 23 +++++++++++++++++++++++ packages/core/src/sdk.ts | 1 + packages/types/src/options.ts | 6 ++++++ 3 files changed, 30 insertions(+) diff --git a/packages/browser/test/unit/index.test.ts b/packages/browser/test/unit/index.test.ts index 8067cc340148..5922834c1583 100644 --- a/packages/browser/test/unit/index.test.ts +++ b/packages/browser/test/unit/index.test.ts @@ -179,6 +179,29 @@ describe('SentryBrowser initialization', () => { delete global.SENTRY_RELEASE; }); + it('should use initalScope object', () => { + init({ dsn, initialScope: { tags: { a: 'b' } } }); + expect(global.__SENTRY__.hub._stack[0].scope._tags).to.deep.equal({ a: 'b' }); + }); + + it('should use initalScope Scope', () => { + const scope = new Scope(); + scope.setTags({ a: 'b' }); + init({ dsn, initialScope: scope }); + expect(global.__SENTRY__.hub._stack[0].scope._tags).to.deep.equal({ a: 'b' }); + }); + + it('should use initalScope callback', () => { + init({ + dsn, + initialScope: scope => { + scope.setTags({ a: 'b' }); + return scope; + }, + }); + expect(global.__SENTRY__.hub._stack[0].scope._tags).to.deep.equal({ a: 'b' }); + }); + it('should have initialization proceed as normal if window.SENTRY_RELEASE is not set', () => { // This is mostly a happy-path test to ensure that the initialization doesn't throw an error. init({ dsn }); diff --git a/packages/core/src/sdk.ts b/packages/core/src/sdk.ts index 17497c57f2be..f674a3e900f4 100644 --- a/packages/core/src/sdk.ts +++ b/packages/core/src/sdk.ts @@ -17,6 +17,7 @@ export function initAndBind(clientClass: Cl logger.enable(); } const hub = getCurrentHub(); + hub.getScope()?.update(options.initialScope); const client = new clientClass(options); hub.bindClient(client); } diff --git a/packages/types/src/options.ts b/packages/types/src/options.ts index b850fff6da1c..e56502856ed5 100644 --- a/packages/types/src/options.ts +++ b/packages/types/src/options.ts @@ -2,6 +2,7 @@ import { Breadcrumb, BreadcrumbHint } from './breadcrumb'; import { Event, EventHint } from './event'; import { Integration } from './integration'; import { LogLevel } from './loglevel'; +import { CaptureContext } from './scope'; import { SdkMetadata } from './sdkmetadata'; import { SamplingContext } from './transaction'; import { Transport, TransportClass, TransportOptions } from './transport'; @@ -124,6 +125,11 @@ export interface Options { */ autoSessionTracking?: boolean; + /** + * Set data to the inital scope + */ + initialScope?: CaptureContext; + /** * Set of metadata about the SDK that can be internally used to enhance envelopes and events, * and provide additional data about every request. From 48c694b7b433d23311a000bacf0c22955b185188 Mon Sep 17 00:00:00 2001 From: Daniel Griesser Date: Fri, 14 May 2021 15:59:29 +0200 Subject: [PATCH 2/4] fix: Tests --- packages/core/test/lib/sdk.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/core/test/lib/sdk.test.ts b/packages/core/test/lib/sdk.test.ts index 6107d48a6345..9ca99171b135 100644 --- a/packages/core/test/lib/sdk.test.ts +++ b/packages/core/test/lib/sdk.test.ts @@ -3,6 +3,7 @@ import { Client, Integration } from '@sentry/types'; import { installedIntegrations } from '../../src/integration'; import { initAndBind } from '../../src/sdk'; import { TestClient } from '../mocks/client'; +import { Scope } from '@sentry/hub'; // eslint-disable-next-line no-var declare var global: any; @@ -16,11 +17,15 @@ jest.mock('@sentry/hub', () => { getCurrentHub(): { bindClient(client: Client): boolean; getClient(): boolean; + getScope(): Scope; } { return { getClient(): boolean { return false; }, + getScope(): Scope { + return new Scope(); + }, bindClient(client: Client): boolean { client.setupIntegrations(); return true; From 09c9d9137a1656a3c22bbe164637d8a075e4e603 Mon Sep 17 00:00:00 2001 From: Daniel Griesser Date: Mon, 17 May 2021 08:09:30 +0200 Subject: [PATCH 3/4] Update packages/browser/test/unit/index.test.ts Co-authored-by: Rodolfo Carvalho --- packages/browser/test/unit/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/browser/test/unit/index.test.ts b/packages/browser/test/unit/index.test.ts index 5922834c1583..bab0d2ac8840 100644 --- a/packages/browser/test/unit/index.test.ts +++ b/packages/browser/test/unit/index.test.ts @@ -179,7 +179,7 @@ describe('SentryBrowser initialization', () => { delete global.SENTRY_RELEASE; }); - it('should use initalScope object', () => { + it('should use initialScope', () => { init({ dsn, initialScope: { tags: { a: 'b' } } }); expect(global.__SENTRY__.hub._stack[0].scope._tags).to.deep.equal({ a: 'b' }); }); From 2dfbd36c6ce22f2053172054fb81cc00fab4c2d7 Mon Sep 17 00:00:00 2001 From: Daniel Griesser Date: Mon, 17 May 2021 08:19:47 +0200 Subject: [PATCH 4/4] fix: Linter --- packages/core/test/lib/sdk.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/test/lib/sdk.test.ts b/packages/core/test/lib/sdk.test.ts index 9ca99171b135..04f704d0698e 100644 --- a/packages/core/test/lib/sdk.test.ts +++ b/packages/core/test/lib/sdk.test.ts @@ -1,9 +1,9 @@ +import { Scope } from '@sentry/hub'; import { Client, Integration } from '@sentry/types'; import { installedIntegrations } from '../../src/integration'; import { initAndBind } from '../../src/sdk'; import { TestClient } from '../mocks/client'; -import { Scope } from '@sentry/hub'; // eslint-disable-next-line no-var declare var global: any;