Skip to content

Commit 8ac381c

Browse files
authored
feat(core): Deprecate the Hub constructor (#10584)
1 parent 43b8a3d commit 8ac381c

File tree

22 files changed

+145
-5
lines changed

22 files changed

+145
-5
lines changed

MIGRATION.md

+39
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,45 @@ If you are using the `Hub` right now, see the following table on how to migrate
272272
| endSession() | `Sentry.endSession()` |
273273
| shouldSendDefaultPii() | REMOVED - The closest equivalent is `Sentry.getClient().getOptions().sendDefaultPii` |
274274

275+
The `Hub` constructor is also deprecated and will be removed in the next major version. If you are creating Hubs for
276+
multi-client use like so:
277+
278+
```ts
279+
// OLD
280+
const hub = new Hub();
281+
hub.bindClient(client);
282+
makeMain(hub);
283+
```
284+
285+
instead initialize the client as follows:
286+
287+
```ts
288+
// NEW
289+
Sentry.withIsolationScope(() => {
290+
Sentry.setCurrentClient(client);
291+
client.init();
292+
});
293+
```
294+
295+
If you are using the Hub to capture events like so:
296+
297+
```ts
298+
// OLD
299+
const client = new Client();
300+
const hub = new Hub(client);
301+
hub.captureException();
302+
```
303+
304+
instead capture isolated events as follows:
305+
306+
```ts
307+
// NEW
308+
const client = new Client();
309+
const scope = new Scope();
310+
scope.setClient(client);
311+
scope.captureException();
312+
```
313+
275314
## Deprecate `client.setupIntegrations()`
276315

277316
Instead, use the new `client.init()` method. You should probably not use this directly and instead use `Sentry.init()`,

packages/bun/test/integrations/bunserver.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('Bun Serve Integration', () => {
1919
beforeEach(() => {
2020
const options = getDefaultBunClientOptions({ tracesSampleRate: 1, debug: true });
2121
client = new BunClient(options);
22+
// eslint-disable-next-line deprecation/deprecation
2223
hub = new Hub(client);
2324
// eslint-disable-next-line deprecation/deprecation
2425
makeMain(hub);

packages/core/src/hub.ts

+43
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,46 @@ export class Hub implements HubInterface {
121121
* @param client bound to the hub.
122122
* @param scope bound to the hub.
123123
* @param version number, higher number means higher priority.
124+
*
125+
* @deprecated Instantiation of Hub objects is deprecated and the constructor will be removed in version 8 of the SDK.
126+
*
127+
* If you are currently using the Hub for multi-client use like so:
128+
*
129+
* ```
130+
* // OLD
131+
* const hub = new Hub();
132+
* hub.bindClient(client);
133+
* makeMain(hub)
134+
* ```
135+
*
136+
* instead initialize the client as follows:
137+
*
138+
* ```
139+
* // NEW
140+
* Sentry.withIsolationScope(() => {
141+
* Sentry.setCurrentClient(client);
142+
* client.init();
143+
* });
144+
* ```
145+
*
146+
* If you are using the Hub to capture events like so:
147+
*
148+
* ```
149+
* // OLD
150+
* const client = new Client();
151+
* const hub = new Hub(client);
152+
* hub.captureException()
153+
* ```
154+
*
155+
* instead capture isolated events as follows:
156+
*
157+
* ```
158+
* // NEW
159+
* const client = new Client();
160+
* const scope = new Scope();
161+
* scope.setClient(client);
162+
* scope.captureException();
163+
* ```
124164
*/
125165
public constructor(
126166
client?: Client,
@@ -749,6 +789,7 @@ function getGlobalHub(registry: Carrier = getMainCarrier()): Hub {
749789
// eslint-disable-next-line deprecation/deprecation
750790
getHubFromCarrier(registry).isOlderThan(API_VERSION)
751791
) {
792+
// eslint-disable-next-line deprecation/deprecation
752793
setHubOnCarrier(registry, new Hub());
753794
}
754795

@@ -774,6 +815,7 @@ export function ensureHubOnCarrier(carrier: Carrier, parent: Hub = getGlobalHub(
774815
const scope = parent.getScope();
775816
// eslint-disable-next-line deprecation/deprecation
776817
const isolationScope = parent.getIsolationScope();
818+
// eslint-disable-next-line deprecation/deprecation
777819
setHubOnCarrier(carrier, new Hub(client, scope.clone(), isolationScope.clone()));
778820
}
779821
}
@@ -823,6 +865,7 @@ function hasHubOnCarrier(carrier: Carrier): boolean {
823865
* @hidden
824866
*/
825867
export function getHubFromCarrier(carrier: Carrier): Hub {
868+
// eslint-disable-next-line deprecation/deprecation
826869
return getGlobalSingleton<Hub>('hub', () => new Hub(), carrier);
827870
}
828871

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

+9
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ describe('BaseClient', () => {
119119
const options = getDefaultTestClientOptions({});
120120
const client = new TestClient(options);
121121
const scope = new Scope();
122+
// eslint-disable-next-line deprecation/deprecation
122123
const hub = new Hub(client, scope);
123124

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

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

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

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

186191
// eslint-disable-next-line deprecation/deprecation
@@ -196,6 +201,7 @@ describe('BaseClient', () => {
196201
const options = getDefaultTestClientOptions({ beforeBreadcrumb });
197202
const client = new TestClient(options);
198203
const scope = new Scope();
204+
// eslint-disable-next-line deprecation/deprecation
199205
const hub = new Hub(client, scope);
200206

201207
// eslint-disable-next-line deprecation/deprecation
@@ -211,6 +217,7 @@ describe('BaseClient', () => {
211217
const options = getDefaultTestClientOptions({ beforeBreadcrumb });
212218
const client = new TestClient(options);
213219
const scope = new Scope();
220+
// eslint-disable-next-line deprecation/deprecation
214221
const hub = new Hub(client, scope);
215222

216223
// eslint-disable-next-line deprecation/deprecation
@@ -226,6 +233,7 @@ describe('BaseClient', () => {
226233
const options = getDefaultTestClientOptions({ beforeBreadcrumb });
227234
const client = new TestClient(options);
228235
const scope = new Scope();
236+
// eslint-disable-next-line deprecation/deprecation
229237
const hub = new Hub(client, scope);
230238

231239
// eslint-disable-next-line deprecation/deprecation
@@ -620,6 +628,7 @@ describe('BaseClient', () => {
620628
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, maxBreadcrumbs: 1 });
621629
const client = new TestClient(options);
622630
const scope = new Scope();
631+
// eslint-disable-next-line deprecation/deprecation
623632
const hub = new Hub(client, scope);
624633
// eslint-disable-next-line deprecation/deprecation
625634
hub.addBreadcrumb({ message: '1' });

packages/core/test/lib/exports.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function getTestClient(): TestClient {
3131
describe('withScope', () => {
3232
beforeEach(() => {
3333
const client = getTestClient();
34+
// eslint-disable-next-line deprecation/deprecation
3435
const hub = new Hub(client);
3536
// eslint-disable-next-line deprecation/deprecation
3637
makeMain(hub);
@@ -173,6 +174,7 @@ describe('withScope', () => {
173174
describe('session APIs', () => {
174175
beforeEach(() => {
175176
const client = getTestClient();
177+
// eslint-disable-next-line deprecation/deprecation
176178
const hub = new Hub(client);
177179
// eslint-disable-next-line deprecation/deprecation
178180
makeMain(hub);
@@ -326,6 +328,7 @@ describe('isInitialized', () => {
326328

327329
it('returns true if client is setup', () => {
328330
const client = getTestClient();
331+
// eslint-disable-next-line deprecation/deprecation
329332
const hub = new Hub(client);
330333
// eslint-disable-next-line deprecation/deprecation
331334
makeMain(hub);

packages/core/test/lib/integration.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ describe('addIntegration', () => {
617617
}
618618

619619
const client = getTestClient();
620+
// eslint-disable-next-line deprecation/deprecation
620621
const hub = new Hub(client);
621622
// eslint-disable-next-line deprecation/deprecation
622623
makeMain(hub);
@@ -635,6 +636,7 @@ describe('addIntegration', () => {
635636
setupOnce = jest.fn();
636637
}
637638

639+
// eslint-disable-next-line deprecation/deprecation
638640
const hub = new Hub();
639641
// eslint-disable-next-line deprecation/deprecation
640642
makeMain(hub);
@@ -660,6 +662,7 @@ describe('addIntegration', () => {
660662
}
661663

662664
const client = getTestClient();
665+
// eslint-disable-next-line deprecation/deprecation
663666
const hub = new Hub(client);
664667
// eslint-disable-next-line deprecation/deprecation
665668
makeMain(hub);
@@ -683,6 +686,7 @@ describe('addIntegration', () => {
683686
}
684687

685688
const client = getTestClient();
689+
// eslint-disable-next-line deprecation/deprecation
686690
const hub = new Hub(client);
687691
// eslint-disable-next-line deprecation/deprecation
688692
makeMain(hub);

packages/core/test/lib/scope.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ describe('withActiveSpan()', () => {
522522
const options = getDefaultTestClientOptions({ enableTracing: true });
523523
const client = new TestClient(options);
524524
const scope = new Scope();
525+
// eslint-disable-next-line deprecation/deprecation
525526
const hub = new Hub(client, scope);
526527
makeMain(hub); // eslint-disable-line deprecation/deprecation
527528
});

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

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ describe('SDK', () => {
8787

8888
describe('captureCheckIn', () => {
8989
afterEach(function () {
90+
// eslint-disable-next-line deprecation/deprecation
9091
const hub = new Hub();
9192
// eslint-disable-next-line deprecation/deprecation
9293
makeMain(hub);

packages/core/test/lib/tracing/dynamicSamplingContext.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('getDynamicSamplingContextFromSpan', () => {
99
beforeEach(() => {
1010
const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0, release: '1.0.1' });
1111
const client = new TestClient(options);
12+
// eslint-disable-next-line deprecation/deprecation
1213
hub = new Hub(client);
1314
// eslint-disable-next-line deprecation/deprecation
1415
makeMain(hub);

packages/core/test/lib/tracing/errors.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('registerErrorHandlers()', () => {
3434
mockAddGlobalErrorInstrumentationHandler.mockClear();
3535
mockAddGlobalUnhandledRejectionInstrumentationHandler.mockClear();
3636
const options = getDefaultBrowserClientOptions({ enableTracing: true });
37+
// eslint-disable-next-line deprecation/deprecation
3738
const hub = new Hub(new BrowserClient(options));
3839
// eslint-disable-next-line deprecation/deprecation
3940
makeMain(hub);

packages/core/test/lib/tracing/trace.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('startSpan', () => {
3636
beforeEach(() => {
3737
const options = getDefaultTestClientOptions({ tracesSampleRate: 0.0 });
3838
client = new TestClient(options);
39+
// eslint-disable-next-line deprecation/deprecation
3940
hub = new Hub(client);
4041
// eslint-disable-next-line deprecation/deprecation
4142
makeMain(hub);
@@ -427,6 +428,7 @@ describe('startSpanManual', () => {
427428
beforeEach(() => {
428429
const options = getDefaultTestClientOptions({ tracesSampleRate: 1 });
429430
client = new TestClient(options);
431+
// eslint-disable-next-line deprecation/deprecation
430432
hub = new Hub(client);
431433
// eslint-disable-next-line deprecation/deprecation
432434
makeMain(hub);
@@ -537,6 +539,7 @@ describe('startInactiveSpan', () => {
537539
beforeEach(() => {
538540
const options = getDefaultTestClientOptions({ tracesSampleRate: 1 });
539541
client = new TestClient(options);
542+
// eslint-disable-next-line deprecation/deprecation
540543
hub = new Hub(client);
541544
// eslint-disable-next-line deprecation/deprecation
542545
makeMain(hub);
@@ -662,6 +665,7 @@ describe('continueTrace', () => {
662665
beforeEach(() => {
663666
const options = getDefaultTestClientOptions({ tracesSampleRate: 0.0 });
664667
client = new TestClient(options);
668+
// eslint-disable-next-line deprecation/deprecation
665669
hub = new Hub(client);
666670
// eslint-disable-next-line deprecation/deprecation
667671
makeMain(hub);

packages/deno/test/__snapshots__/mod.test.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ snapshot[`captureException 1`] = `
8282
filename: "app:///test/mod.test.ts",
8383
function: "<anonymous>",
8484
in_app: true,
85-
lineno: 46,
85+
lineno: 47,
8686
post_context: [
8787
"",
8888
" await delay(200);",
@@ -108,7 +108,7 @@ snapshot[`captureException 1`] = `
108108
filename: "app:///test/mod.test.ts",
109109
function: "something",
110110
in_app: true,
111-
lineno: 43,
111+
lineno: 44,
112112
post_context: [
113113
" }",
114114
"",

packages/deno/test/mod.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function getTestClient(
2222
});
2323

2424
const scope = new Scope();
25+
// eslint-disable-next-line deprecation/deprecation
2526
const hub = new Hub(client, scope);
2627

2728
return [hub, client];

packages/node-experimental/src/sdk/hub.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export function getCurrentHub(): Hub {
144144
*/
145145
export function makeMain(hub: Hub): Hub {
146146
// eslint-disable-next-line no-console
147-
console.warn('makeMain is a noop in @sentry/node-experimental. Use `setCurrentScope` instead.');
147+
console.warn('makeMain is a noop in @sentry/node-experimental. Use `setCurrentClient` instead.');
148148
return hub;
149149
}
150150

0 commit comments

Comments
 (0)