Skip to content

Commit 9d861fe

Browse files
authored
feat(v7/core): Deprecate Hub class (#11528)
Deprecate the `Hub` class which we'll remove in a follow-up PR in v8. The replacement is to mostly use the scope-centered APIs. Most APIs that relied on the `Hub` class (unfortunately quite a lot - looks like we weren't really consistent in using the Hub interface instead of the class) are already deprecated and already removed in v8.
1 parent ad2685a commit 9d861fe

File tree

48 files changed

+119
-22
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+119
-22
lines changed

packages/astro/src/index.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export {
3232
getCurrentScope,
3333
getGlobalScope,
3434
getIsolationScope,
35+
// eslint-disable-next-line deprecation/deprecation
3536
Hub,
3637
// eslint-disable-next-line deprecation/deprecation
3738
makeMain,

packages/browser/src/exports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export {
4242
getClient,
4343
isInitialized,
4444
getCurrentScope,
45+
// eslint-disable-next-line deprecation/deprecation
4546
Hub,
4647
// eslint-disable-next-line deprecation/deprecation
4748
lastEventId,

packages/bun/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export {
4949
getCurrentScope,
5050
getGlobalScope,
5151
getIsolationScope,
52+
// eslint-disable-next-line deprecation/deprecation
5253
Hub,
5354
// eslint-disable-next-line deprecation/deprecation
5455
lastEventId,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getDefaultBunClientOptions } from '../helpers';
99
const DEFAULT_PORT = 22114;
1010

1111
describe('Bun Serve Integration', () => {
12+
// eslint-disable-next-line deprecation/deprecation
1213
let hub: Hub;
1314
let client: BunClient;
1415

packages/core/src/exports.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export function captureEvent(event: Event, hint?: EventHint): string {
8787
*
8888
* @deprecated Use getCurrentScope() directly.
8989
*/
90+
// eslint-disable-next-line deprecation/deprecation
9091
export function configureScope(callback: (scope: Scope) => void): ReturnType<Hub['configureScope']> {
9192
// eslint-disable-next-line deprecation/deprecation
9293
getCurrentHub().configureScope(callback);
@@ -100,6 +101,7 @@ export function configureScope(callback: (scope: Scope) => void): ReturnType<Hub
100101
*
101102
* @param breadcrumb The breadcrumb to record.
102103
*/
104+
// eslint-disable-next-line deprecation/deprecation
103105
export function addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): ReturnType<Hub['addBreadcrumb']> {
104106
// eslint-disable-next-line deprecation/deprecation
105107
getCurrentHub().addBreadcrumb(breadcrumb, hint);
@@ -110,7 +112,7 @@ export function addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): Re
110112
* @param name of the context
111113
* @param context Any kind of data. This data will be normalized.
112114
*/
113-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
115+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, deprecation/deprecation
114116
export function setContext(name: string, context: { [key: string]: any } | null): ReturnType<Hub['setContext']> {
115117
// eslint-disable-next-line deprecation/deprecation
116118
getCurrentHub().setContext(name, context);
@@ -120,6 +122,7 @@ export function setContext(name: string, context: { [key: string]: any } | null)
120122
* Set an object that will be merged sent as extra data with the event.
121123
* @param extras Extras object to merge into current context.
122124
*/
125+
// eslint-disable-next-line deprecation/deprecation
123126
export function setExtras(extras: Extras): ReturnType<Hub['setExtras']> {
124127
// eslint-disable-next-line deprecation/deprecation
125128
getCurrentHub().setExtras(extras);
@@ -130,6 +133,7 @@ export function setExtras(extras: Extras): ReturnType<Hub['setExtras']> {
130133
* @param key String of extra
131134
* @param extra Any kind of data. This data will be normalized.
132135
*/
136+
// eslint-disable-next-line deprecation/deprecation
133137
export function setExtra(key: string, extra: Extra): ReturnType<Hub['setExtra']> {
134138
// eslint-disable-next-line deprecation/deprecation
135139
getCurrentHub().setExtra(key, extra);
@@ -139,6 +143,7 @@ export function setExtra(key: string, extra: Extra): ReturnType<Hub['setExtra']>
139143
* Set an object that will be merged sent as tags data with the event.
140144
* @param tags Tags context object to merge into current context.
141145
*/
146+
// eslint-disable-next-line deprecation/deprecation
142147
export function setTags(tags: { [key: string]: Primitive }): ReturnType<Hub['setTags']> {
143148
// eslint-disable-next-line deprecation/deprecation
144149
getCurrentHub().setTags(tags);
@@ -152,6 +157,7 @@ export function setTags(tags: { [key: string]: Primitive }): ReturnType<Hub['set
152157
* @param key String key of tag
153158
* @param value Value of tag
154159
*/
160+
// eslint-disable-next-line deprecation/deprecation
155161
export function setTag(key: string, value: Primitive): ReturnType<Hub['setTag']> {
156162
// eslint-disable-next-line deprecation/deprecation
157163
getCurrentHub().setTag(key, value);
@@ -162,6 +168,7 @@ export function setTag(key: string, value: Primitive): ReturnType<Hub['setTag']>
162168
*
163169
* @param user User context object to be set in the current context. Pass `null` to unset the user.
164170
*/
171+
// eslint-disable-next-line deprecation/deprecation
165172
export function setUser(user: User | null): ReturnType<Hub['setUser']> {
166173
// eslint-disable-next-line deprecation/deprecation
167174
getCurrentHub().setUser(user);
@@ -272,6 +279,7 @@ export function withActiveSpan<T>(span: Span, callback: (scope: Scope) => T): T
272279
export function startTransaction(
273280
context: TransactionContext,
274281
customSamplingContext?: CustomSamplingContext,
282+
// eslint-disable-next-line deprecation/deprecation
275283
): ReturnType<Hub['startTransaction']> {
276284
// eslint-disable-next-line deprecation/deprecation
277285
return getCurrentHub().startTransaction({ ...context }, customSamplingContext);

packages/core/src/hub.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export interface AsyncContextStrategy {
6666
/**
6767
* Gets the current async context. Returns undefined if there is no current async context.
6868
*/
69+
// eslint-disable-next-line deprecation/deprecation
6970
getCurrentHub: () => Hub | undefined;
7071
/**
7172
* Runs the supplied callback in its own async context.
@@ -88,6 +89,7 @@ export interface Layer {
8889
*/
8990
export interface Carrier {
9091
__SENTRY__?: {
92+
// eslint-disable-next-line deprecation/deprecation
9193
hub?: Hub;
9294
acs?: AsyncContextStrategy;
9395
/**
@@ -103,7 +105,15 @@ export interface Carrier {
103105
}
104106

105107
/**
106-
* @inheritDoc
108+
* @deprecated The `Hub` class will be removed in version 8 of the SDK in favour of `Scope` and `Client` objects.
109+
*
110+
* If you previously used the `Hub` class directly, replace it with `Scope` and `Client` objects. More information:
111+
* - [Multiple Sentry Instances](https://docs.sentry.io/platforms/javascript/best-practices/multiple-sentry-instances/)
112+
* - [Browser Extensions](https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/)
113+
*
114+
* Some of our APIs are typed with the Hub class instead of the interface (e.g. `getCurrentHub`). Most of them are deprecated
115+
* themselves and will also be removed in version 8. More information:
116+
* - [Migration Guide](https://github.com/getsentry/sentry-javascript/blob/develop/MIGRATION.md#deprecate-hub)
107117
*/
108118
export class Hub implements HubInterface {
109119
/** Is a {@link Layer}[] containing the client and scope */
@@ -531,6 +541,7 @@ export class Hub implements HubInterface {
531541
/**
532542
* @inheritDoc
533543
*/
544+
// eslint-disable-next-line deprecation/deprecation
534545
public run(callback: (hub: Hub) => void): void {
535546
// eslint-disable-next-line deprecation/deprecation
536547
const oldHub = makeMain(this);
@@ -739,6 +750,7 @@ export function getMainCarrier(): Carrier {
739750
*
740751
* @deprecated Use `setCurrentClient()` instead.
741752
*/
753+
// eslint-disable-next-line deprecation/deprecation
742754
export function makeMain(hub: Hub): Hub {
743755
const registry = getMainCarrier();
744756
const oldHub = getHubFromCarrier(registry);
@@ -755,6 +767,7 @@ export function makeMain(hub: Hub): Hub {
755767
*
756768
* @deprecated Use the respective replacement method directly instead.
757769
*/
770+
// eslint-disable-next-line deprecation/deprecation
758771
export function getCurrentHub(): Hub {
759772
// Get main carrier (global for every environment)
760773
const registry = getMainCarrier();
@@ -781,6 +794,7 @@ export function getIsolationScope(): Scope {
781794
return getCurrentHub().getIsolationScope();
782795
}
783796

797+
// eslint-disable-next-line deprecation/deprecation
784798
function getGlobalHub(registry: Carrier = getMainCarrier()): Hub {
785799
// If there's no hub, or its an old API, assign a new one
786800

@@ -802,6 +816,7 @@ function getGlobalHub(registry: Carrier = getMainCarrier()): Hub {
802816
*
803817
* If the carrier does not contain a hub, a new hub is created with the global hub client and scope.
804818
*/
819+
// eslint-disable-next-line deprecation/deprecation
805820
export function ensureHubOnCarrier(carrier: Carrier, parent: Hub = getGlobalHub()): void {
806821
// If there's no hub on current domain, or it's an old API, assign a new one
807822
if (
@@ -864,6 +879,7 @@ function hasHubOnCarrier(carrier: Carrier): boolean {
864879
* @param carrier object
865880
* @hidden
866881
*/
882+
// eslint-disable-next-line deprecation/deprecation
867883
export function getHubFromCarrier(carrier: Carrier): Hub {
868884
// eslint-disable-next-line deprecation/deprecation
869885
return getGlobalSingleton<Hub>('hub', () => new Hub(), carrier);
@@ -875,6 +891,7 @@ export function getHubFromCarrier(carrier: Carrier): Hub {
875891
* @param hub Hub
876892
* @returns A boolean indicating success or failure
877893
*/
894+
// eslint-disable-next-line deprecation/deprecation
878895
export function setHubOnCarrier(carrier: Carrier, hub: Hub): boolean {
879896
if (!carrier) return false;
880897
const __SENTRY__ = (carrier.__SENTRY__ = carrier.__SENTRY__ || {});

packages/core/src/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export {
4444
getCurrentHub,
4545
getIsolationScope,
4646
getHubFromCarrier,
47+
// eslint-disable-next-line deprecation/deprecation
4748
Hub,
4849
// eslint-disable-next-line deprecation/deprecation
4950
makeMain,
@@ -84,12 +85,7 @@ export { hasTracingEnabled } from './utils/hasTracingEnabled';
8485
export { isSentryRequestUrl } from './utils/isSentryRequestUrl';
8586
export { handleCallbackErrors } from './utils/handleCallbackErrors';
8687
export { parameterize } from './utils/parameterize';
87-
export {
88-
spanToTraceHeader,
89-
spanToJSON,
90-
spanIsSampled,
91-
spanToTraceContext,
92-
} from './utils/spanUtils';
88+
export { spanToTraceHeader, spanToJSON, spanIsSampled, spanToTraceContext } from './utils/spanUtils';
9389
export { getRootSpan } from './utils/getRootSpan';
9490
export { applySdkMetadata } from './utils/sdkMetadata';
9591
export { DEFAULT_ENVIRONMENT } from './constants';

packages/core/src/tracing/hubextensions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { sampleTransaction } from './sampling';
1111
import { Transaction } from './transaction';
1212

1313
/** Returns all trace headers that are currently on the top scope. */
14+
// eslint-disable-next-line deprecation/deprecation
1415
function traceHeaders(this: Hub): { [key: string]: string } {
1516
// eslint-disable-next-line deprecation/deprecation
1617
const scope = this.getScope();
@@ -40,6 +41,7 @@ function traceHeaders(this: Hub): { [key: string]: string } {
4041
* @see {@link Hub.startTransaction}
4142
*/
4243
function _startTransaction(
44+
// eslint-disable-next-line deprecation/deprecation
4345
this: Hub,
4446
transactionContext: TransactionContext,
4547
customSamplingContext?: CustomSamplingContext,
@@ -88,6 +90,7 @@ The transaction will not be sampled. Please use the ${configInstrumenter} instru
8890
* Create new idle transaction.
8991
*/
9092
export function startIdleTransaction(
93+
// eslint-disable-next-line deprecation/deprecation
9194
hub: Hub,
9295
transactionContext: TransactionContext,
9396
idleTimeout: number,

packages/core/src/tracing/idletransaction.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class IdleTransaction extends Transaction {
102102
*/
103103
public constructor(
104104
transactionContext: TransactionContext,
105+
// eslint-disable-next-line deprecation/deprecation
105106
private readonly _idleHub: Hub,
106107
/**
107108
* The time to wait in ms until the idle transaction will be finished. This timer is started each time

packages/core/src/tracing/trace.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ export const continueTrace: ContinueTrace = <V>(
330330
};
331331

332332
function createChildSpanOrTransaction(
333+
// eslint-disable-next-line deprecation/deprecation
333334
hub: Hub,
334335
{
335336
parentSpan,

packages/core/src/tracing/transaction.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
2626
/**
2727
* The reference to the current hub.
2828
*/
29+
// eslint-disable-next-line deprecation/deprecation
2930
public _hub: Hub;
3031

3132
protected _name: string;
@@ -48,6 +49,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
4849
*
4950
* @deprecated Transactions will be removed in v8. Use spans instead.
5051
*/
52+
// eslint-disable-next-line deprecation/deprecation
5153
public constructor(transactionContext: TransactionContext, hub?: Hub) {
5254
super(transactionContext);
5355
this._contexts = {};
@@ -250,6 +252,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
250252
*
251253
* @internal
252254
*/
255+
// eslint-disable-next-line deprecation/deprecation
253256
public setHub(hub: Hub): void {
254257
this._hub = hub;
255258
}

packages/core/src/tracing/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getCurrentHub } from '../hub';
99
*
1010
* @deprecated You should not rely on the transaction, but just use `startSpan()` APIs instead.
1111
*/
12+
// eslint-disable-next-line deprecation/deprecation
1213
export function getActiveTransaction<T extends Transaction>(maybeHub?: Hub): T | undefined {
1314
// eslint-disable-next-line deprecation/deprecation
1415
const hub = maybeHub || getCurrentHub();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { addTracingExtensions } from '../../../src/tracing';
55
import { TestClient, getDefaultTestClientOptions } from '../../mocks/client';
66

77
describe('getDynamicSamplingContextFromSpan', () => {
8+
// eslint-disable-next-line deprecation/deprecation
89
let hub: Hub;
910
beforeEach(() => {
1011
const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0, release: '1.0.1' });

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const enum Type {
2929
Async = 'async',
3030
}
3131

32+
// eslint-disable-next-line deprecation/deprecation
3233
let hub: Hub;
3334
let client: TestClient;
3435

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export {
4848
getCurrentScope,
4949
getGlobalScope,
5050
getIsolationScope,
51+
// eslint-disable-next-line deprecation/deprecation
5152
Hub,
5253
// eslint-disable-next-line deprecation/deprecation
5354
lastEventId,

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,47 +42,47 @@ snapshot[`captureException 1`] = `
4242
stacktrace: {
4343
frames: [
4444
{
45-
colno: 20,
45+
colno: "{{colno}}",
4646
filename: "ext:cli/40_testing.js",
4747
function: "outerWrapped",
4848
in_app: false,
49-
lineno: 472,
49+
lineno: "{{lineno}}",
5050
},
5151
{
52-
colno: 33,
52+
colno: "{{colno}}",
5353
filename: "ext:cli/40_testing.js",
5454
function: "exitSanitizer",
5555
in_app: false,
56-
lineno: 458,
56+
lineno: "{{lineno}}",
5757
},
5858
{
59-
colno: 31,
59+
colno: "{{colno}}",
6060
filename: "ext:cli/40_testing.js",
6161
function: "resourceSanitizer",
6262
in_app: false,
63-
lineno: 410,
63+
lineno: "{{lineno}}",
6464
},
6565
{
66-
colno: 33,
66+
colno: "{{colno}}",
6767
filename: "ext:cli/40_testing.js",
6868
function: "asyncOpSanitizer",
6969
in_app: false,
70-
lineno: 177,
70+
lineno: "{{lineno}}",
7171
},
7272
{
73-
colno: 11,
73+
colno: "{{colno}}",
7474
filename: "ext:cli/40_testing.js",
7575
function: "innerWrapped",
7676
in_app: false,
77-
lineno: 526,
77+
lineno: "{{lineno}}",
7878
},
7979
{
80-
colno: 27,
80+
colno: "{{colno}}",
8181
context_line: " client.captureException(something());",
8282
filename: "app:///test/mod.test.ts",
8383
function: "<anonymous>",
8484
in_app: true,
85-
lineno: 47,
85+
lineno: "{{lineno}}",
8686
post_context: [
8787
"",
8888
" await delay(200);",
@@ -103,12 +103,12 @@ snapshot[`captureException 1`] = `
103103
],
104104
},
105105
{
106-
colno: 12,
106+
colno: "{{colno}}",
107107
context_line: " return new Error('Some unhandled error');",
108108
filename: "app:///test/mod.test.ts",
109109
function: "something",
110110
in_app: true,
111-
lineno: 44,
111+
lineno: "{{lineno}}",
112112
post_context: [
113113
" }",
114114
"",

packages/deno/test/mod.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { makeTestTransport } from './transport.ts';
1010
function getTestClient(
1111
callback: (event?: sentryTypes.Event) => void,
1212
integrations: sentryTypes.Integration[] = [],
13+
// eslint-disable-next-line deprecation/deprecation
1314
): [Hub, DenoClient] {
1415
const client = new DenoClient({
1516
dsn: 'https://[email protected]/5650507',

0 commit comments

Comments
 (0)