Skip to content
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

feat(core): Deprecate the Hub constructor #10584

Merged
merged 1 commit into from
Feb 12, 2024
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
39 changes: 39 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,45 @@ If you are using the `Hub` right now, see the following table on how to migrate
| endSession() | `Sentry.endSession()` |
| shouldSendDefaultPii() | REMOVED - The closest equivalent is `Sentry.getClient().getOptions().sendDefaultPii` |

The `Hub` constructor is also deprecated and will be removed in the next major version. If you are creating Hubs for
multi-client use like so:

```ts
// OLD
const hub = new Hub();
hub.bindClient(client);
makeMain(hub);
```

instead initialize the client as follows:

```ts
// NEW
Sentry.withIsolationScope(() => {
Sentry.setCurrentClient(client);
client.init();
});
```

If you are using the Hub to capture events like so:

```ts
// OLD
const client = new Client();
const hub = new Hub(client);
hub.captureException();
```

instead capture isolated events as follows:

```ts
// NEW
const client = new Client();
const scope = new Scope();
scope.setClient(client);
scope.captureException();
```

## Deprecate `client.setupIntegrations()`

Instead, use the new `client.init()` method. You should probably not use this directly and instead use `Sentry.init()`,
Expand Down
1 change: 1 addition & 0 deletions packages/bun/test/integrations/bunserver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('Bun Serve Integration', () => {
beforeEach(() => {
const options = getDefaultBunClientOptions({ tracesSampleRate: 1, debug: true });
client = new BunClient(options);
// eslint-disable-next-line deprecation/deprecation
hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand Down
43 changes: 43 additions & 0 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,46 @@ export class Hub implements HubInterface {
* @param client bound to the hub.
* @param scope bound to the hub.
* @param version number, higher number means higher priority.
*
* @deprecated Instantiation of Hub objects is deprecated and the constructor will be removed in version 8 of the SDK.
*
* If you are currently using the Hub for multi-client use like so:
*
* ```
* // OLD
* const hub = new Hub();
* hub.bindClient(client);
* makeMain(hub)
* ```
*
* instead initialize the client as follows:
*
* ```
* // NEW
* Sentry.withIsolationScope(() => {
* Sentry.setCurrentClient(client);
* client.init();
* });
* ```
*
* If you are using the Hub to capture events like so:
*
* ```
* // OLD
* const client = new Client();
* const hub = new Hub(client);
* hub.captureException()
* ```
*
* instead capture isolated events as follows:
*
* ```
* // NEW
* const client = new Client();
* const scope = new Scope();
* scope.setClient(client);
* scope.captureException();
* ```
*/
public constructor(
client?: Client,
Expand Down Expand Up @@ -749,6 +789,7 @@ function getGlobalHub(registry: Carrier = getMainCarrier()): Hub {
// eslint-disable-next-line deprecation/deprecation
getHubFromCarrier(registry).isOlderThan(API_VERSION)
) {
// eslint-disable-next-line deprecation/deprecation
setHubOnCarrier(registry, new Hub());
}

Expand All @@ -774,6 +815,7 @@ export function ensureHubOnCarrier(carrier: Carrier, parent: Hub = getGlobalHub(
const scope = parent.getScope();
// eslint-disable-next-line deprecation/deprecation
const isolationScope = parent.getIsolationScope();
// eslint-disable-next-line deprecation/deprecation
setHubOnCarrier(carrier, new Hub(client, scope.clone(), isolationScope.clone()));
}
}
Expand Down Expand Up @@ -823,6 +865,7 @@ function hasHubOnCarrier(carrier: Carrier): boolean {
* @hidden
*/
export function getHubFromCarrier(carrier: Carrier): Hub {
// eslint-disable-next-line deprecation/deprecation
return getGlobalSingleton<Hub>('hub', () => new Hub(), carrier);
}

Expand Down
9 changes: 9 additions & 0 deletions packages/core/test/lib/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe('BaseClient', () => {
const options = getDefaultTestClientOptions({});
const client = new TestClient(options);
const scope = new Scope();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client, scope);

scope.addBreadcrumb({ message: 'hello' }, 100);
Expand All @@ -134,6 +135,7 @@ describe('BaseClient', () => {
const options = getDefaultTestClientOptions({});
const client = new TestClient(options);
const scope = new Scope();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client, scope);

scope.addBreadcrumb({ message: 'hello' }, 100);
Expand All @@ -149,6 +151,7 @@ describe('BaseClient', () => {
const options = getDefaultTestClientOptions({ maxBreadcrumbs: 1 });
const client = new TestClient(options);
const scope = new Scope();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client, scope);

scope.addBreadcrumb({ message: 'hello' }, 100);
Expand All @@ -165,6 +168,7 @@ describe('BaseClient', () => {
const options = getDefaultTestClientOptions({});
const client = new TestClient(options);
const scope = new Scope();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client, scope);

scope.addBreadcrumb({ message: 'hello' });
Expand All @@ -181,6 +185,7 @@ describe('BaseClient', () => {
const options = getDefaultTestClientOptions({ beforeBreadcrumb });
const client = new TestClient(options);
const scope = new Scope();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client, scope);

// eslint-disable-next-line deprecation/deprecation
Expand All @@ -196,6 +201,7 @@ describe('BaseClient', () => {
const options = getDefaultTestClientOptions({ beforeBreadcrumb });
const client = new TestClient(options);
const scope = new Scope();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client, scope);

// eslint-disable-next-line deprecation/deprecation
Expand All @@ -211,6 +217,7 @@ describe('BaseClient', () => {
const options = getDefaultTestClientOptions({ beforeBreadcrumb });
const client = new TestClient(options);
const scope = new Scope();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client, scope);

// eslint-disable-next-line deprecation/deprecation
Expand All @@ -226,6 +233,7 @@ describe('BaseClient', () => {
const options = getDefaultTestClientOptions({ beforeBreadcrumb });
const client = new TestClient(options);
const scope = new Scope();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client, scope);

// eslint-disable-next-line deprecation/deprecation
Expand Down Expand Up @@ -620,6 +628,7 @@ describe('BaseClient', () => {
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, maxBreadcrumbs: 1 });
const client = new TestClient(options);
const scope = new Scope();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client, scope);
// eslint-disable-next-line deprecation/deprecation
hub.addBreadcrumb({ message: '1' });
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/lib/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function getTestClient(): TestClient {
describe('withScope', () => {
beforeEach(() => {
const client = getTestClient();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand Down Expand Up @@ -173,6 +174,7 @@ describe('withScope', () => {
describe('session APIs', () => {
beforeEach(() => {
const client = getTestClient();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand Down Expand Up @@ -326,6 +328,7 @@ describe('isInitialized', () => {

it('returns true if client is setup', () => {
const client = getTestClient();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand Down
4 changes: 4 additions & 0 deletions packages/core/test/lib/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ describe('addIntegration', () => {
}

const client = getTestClient();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand All @@ -635,6 +636,7 @@ describe('addIntegration', () => {
setupOnce = jest.fn();
}

// eslint-disable-next-line deprecation/deprecation
const hub = new Hub();
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand All @@ -660,6 +662,7 @@ describe('addIntegration', () => {
}

const client = getTestClient();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand All @@ -683,6 +686,7 @@ describe('addIntegration', () => {
}

const client = getTestClient();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand Down
1 change: 1 addition & 0 deletions packages/core/test/lib/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ describe('withActiveSpan()', () => {
const options = getDefaultTestClientOptions({ enableTracing: true });
const client = new TestClient(options);
const scope = new Scope();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client, scope);
makeMain(hub); // eslint-disable-line deprecation/deprecation
});
Expand Down
1 change: 1 addition & 0 deletions packages/core/test/lib/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('SDK', () => {

describe('captureCheckIn', () => {
afterEach(function () {
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub();
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('getDynamicSamplingContextFromSpan', () => {
beforeEach(() => {
const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0, release: '1.0.1' });
const client = new TestClient(options);
// eslint-disable-next-line deprecation/deprecation
hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand Down
1 change: 1 addition & 0 deletions packages/core/test/lib/tracing/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('registerErrorHandlers()', () => {
mockAddGlobalErrorInstrumentationHandler.mockClear();
mockAddGlobalUnhandledRejectionInstrumentationHandler.mockClear();
const options = getDefaultBrowserClientOptions({ enableTracing: true });
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(new BrowserClient(options));
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand Down
4 changes: 4 additions & 0 deletions packages/core/test/lib/tracing/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('startSpan', () => {
beforeEach(() => {
const options = getDefaultTestClientOptions({ tracesSampleRate: 0.0 });
client = new TestClient(options);
// eslint-disable-next-line deprecation/deprecation
hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand Down Expand Up @@ -427,6 +428,7 @@ describe('startSpanManual', () => {
beforeEach(() => {
const options = getDefaultTestClientOptions({ tracesSampleRate: 1 });
client = new TestClient(options);
// eslint-disable-next-line deprecation/deprecation
hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand Down Expand Up @@ -537,6 +539,7 @@ describe('startInactiveSpan', () => {
beforeEach(() => {
const options = getDefaultTestClientOptions({ tracesSampleRate: 1 });
client = new TestClient(options);
// eslint-disable-next-line deprecation/deprecation
hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand Down Expand Up @@ -662,6 +665,7 @@ describe('continueTrace', () => {
beforeEach(() => {
const options = getDefaultTestClientOptions({ tracesSampleRate: 0.0 });
client = new TestClient(options);
// eslint-disable-next-line deprecation/deprecation
hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
Expand Down
4 changes: 2 additions & 2 deletions packages/deno/test/__snapshots__/mod.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ snapshot[`captureException 1`] = `
filename: "app:///test/mod.test.ts",
function: "<anonymous>",
in_app: true,
lineno: 46,
lineno: 47,
post_context: [
"",
" await delay(200);",
Expand All @@ -108,7 +108,7 @@ snapshot[`captureException 1`] = `
filename: "app:///test/mod.test.ts",
function: "something",
in_app: true,
lineno: 43,
lineno: 44,
post_context: [
" }",
"",
Expand Down
1 change: 1 addition & 0 deletions packages/deno/test/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function getTestClient(
});

const scope = new Scope();
// eslint-disable-next-line deprecation/deprecation
const hub = new Hub(client, scope);

return [hub, client];
Expand Down
2 changes: 1 addition & 1 deletion packages/node-experimental/src/sdk/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function getCurrentHub(): Hub {
*/
export function makeMain(hub: Hub): Hub {
// eslint-disable-next-line no-console
console.warn('makeMain is a noop in @sentry/node-experimental. Use `setCurrentScope` instead.');
console.warn('makeMain is a noop in @sentry/node-experimental. Use `setCurrentClient` instead.');
return hub;
}

Expand Down
Loading
Loading