Skip to content
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
10 changes: 9 additions & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ npx @sentry/migr8@latest
This will let you select which updates to run, and automatically update your code. Make sure to still review all code
changes!

## Deprecate `getCurrentHub()`

In v8, you will no longer have a Hub, only Scopes as a concept. This also means that `getCurrentHub()` will eventually
be removed.

Instead of `getCurrentHub()`, use the respective replacement API directly - see [Deprecate Hub](#deprecate-hub) for
details.

## Deprecate `hub.bindClient()` and `makeMain()`

Instead, either directly use `initAndBind()`, or the new APIs `setCurrentClient()` and `client.init()`. See
Expand Down Expand Up @@ -54,7 +62,7 @@ If you are using the `Hub` right now, see the following table on how to migrate
| ---------------------- | ------------------------------------------------------------------------------------ |
| `new Hub()` | `withScope()`, `withIsolationScope()` or `new Scope()` |
| hub.isOlderThan() | REMOVED - Was used to compare `Hub` instances, which are gonna be removed |
| hub.bindClient() | A combination of `scope.setClient()` and `client.setupIntegrations()` |
| hub.bindClient() | A combination of `scope.setClient()` and `client.init()` |
| hub.pushScope() | `Sentry.withScope()` |
| hub.popScope() | `Sentry.withScope()` |
| hub.withScope() | `Sentry.withScope()` |
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
getActiveTransaction,
getHubFromCarrier,
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
getCurrentScope,
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export {
createTransport,
flush,
getHubFromCarrier,
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
getCurrentScope,
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ interface ShowReportDialogFunction {
export const showReportDialog: ShowReportDialogFunction = (
// eslint-disable-next-line deprecation/deprecation
options: ReportDialogOptions = {},
// eslint-disable-next-line deprecation/deprecation
hub: Hub = getCurrentHub(),
) => {
// doesn't work without a document (React Native)
Expand Down
6 changes: 2 additions & 4 deletions packages/browser/test/unit/profiling/hubextensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const patchedEncoder = (!global.window.TextEncoder && (global.window.TextEncoder
// @ts-expect-error patch the encoder on the window, else importing JSDOM fails (deleted in afterAll)
const patchedDecoder = (!global.window.TextDecoder && (global.window.TextDecoder = TextDecoder)) || true;

import { getCurrentHub } from '@sentry/core';
import { setCurrentClient } from '@sentry/core';
import type { Transaction } from '@sentry/types';
import { JSDOM } from 'jsdom';

Expand All @@ -30,7 +30,6 @@ describe('BrowserProfilingIntegration', () => {
// @ts-expect-error need to override global document
global.location = dom.window.location;

const hub = getCurrentHub();
const client: any = {
getDsn() {
return {};
Expand All @@ -47,8 +46,7 @@ describe('BrowserProfilingIntegration', () => {
},
};

// eslint-disable-next-line deprecation/deprecation
hub.bindClient(client);
setCurrentClient(client);
});

// Reset back to previous values
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
getActiveTransaction,
getHubFromCarrier,
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
getCurrentScope,
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,17 @@ export function withScope<T>(scope: ScopeInterface | undefined, callback: (scope
export function withScope<T>(
...rest: [callback: (scope: Scope) => T] | [scope: ScopeInterface | undefined, callback: (scope: Scope) => T]
): T {
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHub();

// If a scope is defined, we want to make this the active scope instead of the default one
if (rest.length === 2) {
const [scope, callback] = rest;
if (!scope) {
// eslint-disable-next-line deprecation/deprecation
return getCurrentHub().withScope(callback);
return hub.withScope(callback);
}

const hub = getCurrentHub();
// eslint-disable-next-line deprecation/deprecation
return hub.withScope(() => {
// eslint-disable-next-line deprecation/deprecation
Expand All @@ -207,7 +209,7 @@ export function withScope<T>(
}

// eslint-disable-next-line deprecation/deprecation
return getCurrentHub().withScope(rest[0]);
return hub.withScope(rest[0]);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,8 @@ export function makeMain(hub: Hub): Hub {
* If a hub is already registered in the global carrier but this module
* contains a more recent version, it replaces the registered version.
* Otherwise, the currently registered hub will be returned.
*
* @deprecated Use the respective replacement method directly instead.
*/
export function getCurrentHub(): Hub {
// Get main carrier (global for every environment)
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 @@ -37,6 +37,7 @@ export {
captureSession,
} from './exports';
export {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getIsolationScope,
getHubFromCarrier,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Client, ClientOptions } from '@sentry/types';
import { consoleSandbox, logger } from '@sentry/utils';

import { DEBUG_BUILD } from './debug-build';
import { getCurrentScope } from './exports';
import { getCurrentHub } from './hub';

/** A class object that can instantiate Client objects. */
Expand Down Expand Up @@ -29,9 +30,7 @@ export function initAndBind<F extends Client, O extends ClientOptions>(
});
}
}
const hub = getCurrentHub();
// eslint-disable-next-line deprecation/deprecation
const scope = hub.getScope();
const scope = getCurrentScope();
scope.update(options.initialScope);

const client = new clientClass(options);
Expand All @@ -43,6 +42,7 @@ export function initAndBind<F extends Client, O extends ClientOptions>(
* Make the given client the current client.
*/
export function setCurrentClient(client: Client): void {
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHub();
// eslint-disable-next-line deprecation/deprecation
const top = hub.getStackTop();
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export function trace<T>(
// eslint-disable-next-line @typescript-eslint/no-empty-function
afterFinish: () => void = () => {},
): T {
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHub();
const scope = getCurrentScope();
// eslint-disable-next-line deprecation/deprecation
Expand Down Expand Up @@ -182,6 +183,7 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span |
const ctx = normalizeContext(context);

return withScope(context.scope, scope => {
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHub();
// eslint-disable-next-line deprecation/deprecation
const parentSpan = scope.getSpan();
Expand Down Expand Up @@ -226,6 +228,7 @@ export function startSpanManual<T>(
const ctx = normalizeContext(context);

return withScope(context.scope, scope => {
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHub();
// eslint-disable-next-line deprecation/deprecation
const parentSpan = scope.getSpan();
Expand Down Expand Up @@ -266,6 +269,7 @@ export function startInactiveSpan(context: StartSpanOptions): Span | undefined {
}

const ctx = normalizeContext(context);
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHub();
const parentSpan = context.scope
? // eslint-disable-next-line deprecation/deprecation
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/tracing/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
this._measurements = {};
this._contexts = {};

// eslint-disable-next-line deprecation/deprecation
this._hub = hub || getCurrentHub();

this._name = transactionContext.name || '';
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/tracing/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getCurrentHub } from '../hub';
* @deprecated You should not rely on the transaction, but just use `startSpan()` APIs instead.
*/
export function getActiveTransaction<T extends Transaction>(maybeHub?: Hub): T | undefined {
// eslint-disable-next-line deprecation/deprecation
const hub = maybeHub || getCurrentHub();
// eslint-disable-next-line deprecation/deprecation
const scope = hub.getScope();
Expand Down
2 changes: 2 additions & 0 deletions packages/core/test/lib/async-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { getCurrentHub, runWithAsyncContext } from '../../src';
describe('runWithAsyncContext()', () => {
it('without strategy hubs should be equal', () => {
runWithAsyncContext(() => {
// eslint-disable-next-line deprecation/deprecation
const hub1 = getCurrentHub();
runWithAsyncContext(() => {
// eslint-disable-next-line deprecation/deprecation
const hub2 = getCurrentHub();
expect(hub1).toBe(hub2);
});
Expand Down
21 changes: 11 additions & 10 deletions packages/core/test/lib/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { captureCheckIn, getCurrentHub } from '@sentry/core';
import { Hub, captureCheckIn, makeMain, setCurrentClient } from '@sentry/core';
import type { Client, Integration } from '@sentry/types';

import { installedIntegrations } from '../../src/integration';
Expand Down Expand Up @@ -39,21 +39,22 @@ describe('SDK', () => {
});

describe('captureCheckIn', () => {
afterEach(function () {
const hub = new Hub();
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
});

it('returns an id when client is defined', () => {
const hub = getCurrentHub();
jest.spyOn(hub, 'getClient').mockImplementation(() => {
return {
captureCheckIn: () => 'some-id-wasd-1234',
} as unknown as Client;
});
const client = {
captureCheckIn: () => 'some-id-wasd-1234',
} as unknown as Client;
setCurrentClient(client);

expect(captureCheckIn({ monitorSlug: 'gogogo', status: 'in_progress' })).toStrictEqual('some-id-wasd-1234');
});

it('returns an id when client is undefined', () => {
const hub = getCurrentHub();
jest.spyOn(hub, 'getClient').mockImplementation(() => undefined);

expect(captureCheckIn({ monitorSlug: 'gogogo', status: 'in_progress' })).toStrictEqual(expect.any(String));
});
});
1 change: 1 addition & 0 deletions packages/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
getActiveTransaction,
getHubFromCarrier,
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
getCurrentScope,
Expand Down
1 change: 1 addition & 0 deletions packages/hub/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class Scope extends ScopeCore {}
/**
* @deprecated This export has moved to @sentry/core. The @sentry/hub package will be removed in v8.
*/
// eslint-disable-next-line deprecation/deprecation
export const getCurrentHub = getCurrentHubCore;

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/nextjs/test/serverSdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ describe('Server init()', () => {
});

it("initializes both global hub and domain hub when there's an active domain", () => {
// eslint-disable-next-line deprecation/deprecation
const globalHub = getCurrentHub();

runWithAsyncContext(() => {
// eslint-disable-next-line deprecation/deprecation
const globalHub2 = getCurrentHub();
// If we call runWithAsyncContext before init, it executes the callback in the same context as there is no
// strategy yet
Expand All @@ -131,6 +133,7 @@ describe('Server init()', () => {
init({});

runWithAsyncContext(() => {
// eslint-disable-next-line deprecation/deprecation
const domainHub = getCurrentHub();
// this tag should end up only in the domain hub
// eslint-disable-next-line deprecation/deprecation
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
getActiveTransaction,
getHubFromCarrier,
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
getCurrentScope,
Expand Down
1 change: 1 addition & 0 deletions packages/node/test/async/domain.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable deprecation/deprecation */
import { Hub, makeMain } from '@sentry/core';
import { getIsolationScope, withIsolationScope } from '@sentry/core';
import { getCurrentHub, runWithAsyncContext, setAsyncContextStrategy } from '@sentry/core';
Expand Down
1 change: 1 addition & 0 deletions packages/node/test/async/hooks.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable deprecation/deprecation */
import {
Hub,
getCurrentHub,
Expand Down
1 change: 1 addition & 0 deletions packages/node/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ describe('SentryNode', () => {
const client = new NodeClient(options);

runWithAsyncContext(() => {
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHub();
setCurrentClient(client);
client.init();
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry-node/src/spanprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,15 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
return;
}

// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHub();
otelSpan.events.forEach(event => {
maybeCaptureExceptionForTimedEvent(hub, event, otelSpan);
});

if (sentrySpan instanceof Transaction) {
updateTransactionWithOtelData(sentrySpan, otelSpan);
sentrySpan.setHub(getCurrentHub());
sentrySpan.setHub(hub);
} else {
updateSpanWithOtelData(sentrySpan, otelSpan);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/react/src/profiler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ function useProfiler(
export { withProfiler, Profiler, useProfiler };

/** Grabs active transaction off scope */
export function getActiveTransaction<T extends Transaction>(hub: Hub = getCurrentHub()): T | undefined {
export function getActiveTransaction<T extends Transaction>(
// eslint-disable-next-line deprecation/deprecation
hub: Hub = getCurrentHub(),
): T | undefined {
if (hub) {
// eslint-disable-next-line deprecation/deprecation
const scope = hub.getScope();
Expand Down
1 change: 1 addition & 0 deletions packages/remix/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
getActiveTransaction,
getHubFromCarrier,
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
Hub,
// eslint-disable-next-line deprecation/deprecation
Expand Down
1 change: 1 addition & 0 deletions packages/remix/src/utils/instrumentServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBui
}

return runWithAsyncContext(async () => {
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHub();
const options = getClient()?.getOptions();
const scope = getCurrentScope();
Expand Down
1 change: 1 addition & 0 deletions packages/remix/src/utils/serverAdapters/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function wrapExpressRequestHandler(
res.end = wrapEndMethod(res.end);

const request = extractRequestData(req);
// eslint-disable-next-line deprecation/deprecation
const hub = getCurrentHub();
const options = getClient()?.getOptions();
const scope = getCurrentScope();
Expand Down
10 changes: 6 additions & 4 deletions packages/replay/test/unit/session/createSession.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Sentry from '@sentry/core';
import type { Hub } from '@sentry/core';

import { WINDOW } from '../../../src/constants';
import { createSession } from '../../../src/session/createSession';
Expand All @@ -20,10 +21,11 @@ describe('Unit | session | createSession', () => {

beforeAll(() => {
WINDOW.sessionStorage.clear();
jest.spyOn(Sentry, 'getCurrentHub');
(Sentry.getCurrentHub as jest.Mock).mockImplementation(() => ({
captureEvent: captureEventMock,
}));
jest.spyOn(Sentry, 'getCurrentHub').mockImplementation(() => {
return {
captureEvent: captureEventMock,
} as unknown as Hub;
});
});

afterEach(() => {
Expand Down
1 change: 1 addition & 0 deletions packages/serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export {
createTransport,
// eslint-disable-next-line deprecation/deprecation
getActiveTransaction,
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
getCurrentScope,
Expand Down
Loading