Skip to content

feat(core): Remove most Hub class exports #11536

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

Merged
merged 3 commits into from
Apr 11, 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
BrowserClient,
Hub,
Scope,
breadcrumbsIntegration,
dedupeIntegration,
defaultStackParser,
Expand Down Expand Up @@ -31,6 +31,9 @@ const client = new BrowserClient({
integrations,
});

const hub = new Hub(client);
const scope = new Scope();
scope.setClient(client);

hub.captureException(new Error('test client'));
client.init();

scope.captureException(new Error('test client'));
1 change: 0 additions & 1 deletion packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export {
getCurrentScope,
getGlobalScope,
getIsolationScope,
Hub,
setCurrentClient,
Scope,
SDK_VERSION,
Expand Down
1 change: 0 additions & 1 deletion packages/aws-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export {
getCurrentScope,
getGlobalScope,
getIsolationScope,
Hub,
setCurrentClient,
Scope,
SDK_VERSION,
Expand Down
1 change: 0 additions & 1 deletion packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export {
getCurrentScope,
getIsolationScope,
getGlobalScope,
Hub,
setCurrentClient,
Scope,
continueTrace,
Expand Down
1 change: 0 additions & 1 deletion packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export {
getCurrentScope,
getGlobalScope,
getIsolationScope,
Hub,
setCurrentClient,
Scope,
SDK_VERSION,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/asyncContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface AsyncContextStrategy {
/**
* Gets the currently active hub.
*/
// eslint-disable-next-line deprecation/deprecation
getCurrentHub: () => Hub;

/**
Expand Down
13 changes: 6 additions & 7 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { GLOBAL_OBJ, isThenable, logger, timestampInSeconds, uuid4 } from '@sent
import { DEFAULT_ENVIRONMENT } from './constants';
import { getClient, getCurrentScope, getIsolationScope } from './currentScopes';
import { DEBUG_BUILD } from './debug-build';
import type { Hub } from './hub';
import { closeSession, makeSession, updateSession } from './session';
import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent';
import { parseEventHintOrCaptureContext } from './utils/prepareEvent';
Expand Down Expand Up @@ -71,15 +70,15 @@ export function captureEvent(event: Event, hint?: EventHint): string {
* @param context Any kind of data. This data will be normalized.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function setContext(name: string, context: { [key: string]: any } | null): ReturnType<Hub['setContext']> {
export function setContext(name: string, context: { [key: string]: any } | null): void {
getIsolationScope().setContext(name, context);
}

/**
* Set an object that will be merged sent as extra data with the event.
* @param extras Extras object to merge into current context.
*/
export function setExtras(extras: Extras): ReturnType<Hub['setExtras']> {
export function setExtras(extras: Extras): void {
getIsolationScope().setExtras(extras);
}

Expand All @@ -88,15 +87,15 @@ export function setExtras(extras: Extras): ReturnType<Hub['setExtras']> {
* @param key String of extra
* @param extra Any kind of data. This data will be normalized.
*/
export function setExtra(key: string, extra: Extra): ReturnType<Hub['setExtra']> {
export function setExtra(key: string, extra: Extra): void {
getIsolationScope().setExtra(key, extra);
}

/**
* Set an object that will be merged sent as tags data with the event.
* @param tags Tags context object to merge into current context.
*/
export function setTags(tags: { [key: string]: Primitive }): ReturnType<Hub['setTags']> {
export function setTags(tags: { [key: string]: Primitive }): void {
getIsolationScope().setTags(tags);
}

Expand All @@ -108,7 +107,7 @@ export function setTags(tags: { [key: string]: Primitive }): ReturnType<Hub['set
* @param key String key of tag
* @param value Value of tag
*/
export function setTag(key: string, value: Primitive): ReturnType<Hub['setTag']> {
export function setTag(key: string, value: Primitive): void {
getIsolationScope().setTag(key, value);
}

Expand All @@ -117,7 +116,7 @@ export function setTag(key: string, value: Primitive): ReturnType<Hub['setTag']>
*
* @param user User context object to be set in the current context. Pass `null` to unset the user.
*/
export function setUser(user: User | null): ReturnType<Hub['setUser']> {
export function setUser(user: User | null): void {
getIsolationScope().setUser(user);
}

Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/getCurrentHubShim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ import {
/**
* This is for legacy reasons, and returns a proxy object instead of a hub to be used.
*
* @deprecated Use the methods directly.
* @deprecated Use the methods directly from the top level Sentry API (e.g. `Sentry.withScope`)
* For more information see our migration guide for
* [replacing `getCurrentHub` and `Hub`](https://github.com/getsentry/sentry-javascript/blob/develop/MIGRATION.md#deprecate-hub)
* usage
*/
// eslint-disable-next-line deprecation/deprecation
export function getCurrentHubShim(): Hub {
return {
bindClient(client: Client): void {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export interface Layer {

/**
* @inheritDoc
* @deprecated This class will be removed in v8 (tmp-deprecating so we're aware of where this is a problem)
*/
// eslint-disable-next-line deprecation/deprecation
export class Hub implements HubInterface {
/** Is a {@link Layer}[] containing the client and scope */
private readonly _stack: Layer[];
Expand Down Expand Up @@ -503,6 +505,7 @@ export class Hub implements HubInterface {
*
* @deprecated Use the respective replacement method directly instead.
*/
// eslint-disable-next-line deprecation/deprecation
export function getCurrentHub(): HubInterface {
// Get main carrier (global for every environment)
const carrier = getMainCarrier();
Expand All @@ -525,8 +528,10 @@ export function getDefaultIsolationScope(): Scope {
* Get the global hub.
* This will be removed during the v8 cycle and is only here to make migration easier.
*/
// eslint-disable-next-line deprecation/deprecation
export function getGlobalHub(): HubInterface {
const registry = getMainCarrier();
// eslint-disable-next-line deprecation/deprecation
const sentry = getSentryCarrier(registry) as { hub?: HubInterface };

// If there's no hub, or its an old API, assign a new one
Expand Down Expand Up @@ -560,6 +565,7 @@ function withScope<T>(callback: (scope: ScopeInterface) => T): T {
}

function withSetScope<T>(scope: ScopeInterface, callback: (scope: ScopeInterface) => T): T {
// eslint-disable-next-line deprecation/deprecation
const hub = getGlobalHub() as Hub;
// eslint-disable-next-line deprecation/deprecation
return hub.withScope(() => {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export {
export {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
// eslint-disable-next-line deprecation/deprecation
Hub,
getGlobalHub,
getDefaultCurrentScope,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function setCurrentClient(client: Client): void {
}
}

// eslint-disable-next-line deprecation/deprecation
function isHubClass(hub: HubInterface): hub is Hub {
// eslint-disable-next-line deprecation/deprecation
return !!(hub as Hub).getStackTop;
Expand Down
1 change: 0 additions & 1 deletion packages/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export {
getCurrentScope,
getGlobalScope,
getIsolationScope,
Hub,
setCurrentClient,
Scope,
SDK_VERSION,
Expand Down
1 change: 0 additions & 1 deletion packages/google-cloud-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export {
getCurrentScope,
getGlobalScope,
getIsolationScope,
Hub,
setCurrentClient,
Scope,
SDK_VERSION,
Expand Down
1 change: 0 additions & 1 deletion packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export {
close,
createTransport,
flush,
Hub,
SDK_VERSION,
getSpanStatusFromHttpCode,
setHttpStatus,
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry/src/asyncContextStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function setOpenTelemetryContextAsyncContextStrategy(): void {
};
}

// eslint-disable-next-line deprecation/deprecation
function getCurrentHub(): Hub {
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHubShim();
Expand Down
1 change: 0 additions & 1 deletion packages/remix/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export {
getCurrentScope,
getGlobalScope,
getIsolationScope,
Hub,
setCurrentClient,
NodeClient,
Scope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('Unit | session | createSession', () => {
jest.spyOn(Sentry, 'getCurrentHub').mockImplementation(() => {
return {
captureEvent: captureEventMock,
// eslint-disable-next-line deprecation/deprecation
} as unknown as Hub;
});
});
Expand Down
1 change: 0 additions & 1 deletion packages/sveltekit/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export {
getCurrentScope,
getGlobalScope,
getIsolationScope,
Hub,
NodeClient,
setCurrentClient,
Scope,
Expand Down
7 changes: 7 additions & 0 deletions packages/types/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import type { User } from './user';
/**
* Internal class used to make sure we always have the latest internal functions
* working in case we have a version conflict.
*
* @deprecated This interface will be removed in a future major version of the SDK in favour of
* `Scope` and `Client` objects and APIs.
*
* Most APIs referencing `Hub` are themselves and will be removed in version 8 of the SDK. More information:
* - [Migration Guide](https://github.com/getsentry/sentry-javascript/blob/develop/MIGRATION.md#deprecate-hub)
*
*/
export interface Hub {
/**
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type { Event, EventHint, EventType, ErrorEvent, TransactionEvent } from '
export type { EventProcessor } from './eventprocessor';
export type { Exception } from './exception';
export type { Extra, Extras } from './extra';
// eslint-disable-next-line deprecation/deprecation
export type { Hub } from './hub';
export type { Integration, IntegrationClass, IntegrationFn } from './integration';
export type { Mechanism } from './mechanism';
Expand Down
1 change: 1 addition & 0 deletions packages/vercel-edge/src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void {
};
}

// eslint-disable-next-line deprecation/deprecation
function getCurrentHub(): Hub {
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHubShim();
Expand Down
1 change: 0 additions & 1 deletion packages/vercel-edge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export {
getCurrentScope,
getGlobalScope,
getIsolationScope,
Hub,
setCurrentClient,
Scope,
SDK_VERSION,
Expand Down