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(v7/core): Deprecate Hub class #11528

Merged
merged 5 commits into from
Apr 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export {
getCurrentScope,
getGlobalScope,
getIsolationScope,
// eslint-disable-next-line deprecation/deprecation
Hub,
// eslint-disable-next-line deprecation/deprecation
makeMain,
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 @@ -42,6 +42,7 @@ export {
getClient,
isInitialized,
getCurrentScope,
// eslint-disable-next-line deprecation/deprecation
Hub,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
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 @@ -49,6 +49,7 @@ export {
getCurrentScope,
getGlobalScope,
getIsolationScope,
// eslint-disable-next-line deprecation/deprecation
Hub,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
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 @@ -9,6 +9,7 @@ import { getDefaultBunClientOptions } from '../helpers';
const DEFAULT_PORT = 22114;

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

Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function captureEvent(event: Event, hint?: EventHint): string {
*
* @deprecated Use getCurrentScope() directly.
*/
// eslint-disable-next-line deprecation/deprecation
export function configureScope(callback: (scope: Scope) => void): ReturnType<Hub['configureScope']> {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub().configureScope(callback);
Expand All @@ -100,6 +101,7 @@ export function configureScope(callback: (scope: Scope) => void): ReturnType<Hub
*
* @param breadcrumb The breadcrumb to record.
*/
// eslint-disable-next-line deprecation/deprecation
export function addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): ReturnType<Hub['addBreadcrumb']> {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub().addBreadcrumb(breadcrumb, hint);
Expand All @@ -110,7 +112,7 @@ export function addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): Re
* @param name of the context
* @param context Any kind of data. This data will be normalized.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any, deprecation/deprecation
export function setContext(name: string, context: { [key: string]: any } | null): ReturnType<Hub['setContext']> {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub().setContext(name, context);
Expand All @@ -120,6 +122,7 @@ export function setContext(name: string, context: { [key: string]: any } | null)
* Set an object that will be merged sent as extra data with the event.
* @param extras Extras object to merge into current context.
*/
// eslint-disable-next-line deprecation/deprecation
export function setExtras(extras: Extras): ReturnType<Hub['setExtras']> {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub().setExtras(extras);
Expand All @@ -130,6 +133,7 @@ export function setExtras(extras: Extras): ReturnType<Hub['setExtras']> {
* @param key String of extra
* @param extra Any kind of data. This data will be normalized.
*/
// eslint-disable-next-line deprecation/deprecation
export function setExtra(key: string, extra: Extra): ReturnType<Hub['setExtra']> {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub().setExtra(key, extra);
Expand All @@ -139,6 +143,7 @@ export function setExtra(key: string, extra: Extra): ReturnType<Hub['setExtra']>
* Set an object that will be merged sent as tags data with the event.
* @param tags Tags context object to merge into current context.
*/
// eslint-disable-next-line deprecation/deprecation
export function setTags(tags: { [key: string]: Primitive }): ReturnType<Hub['setTags']> {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub().setTags(tags);
Expand All @@ -152,6 +157,7 @@ export function setTags(tags: { [key: string]: Primitive }): ReturnType<Hub['set
* @param key String key of tag
* @param value Value of tag
*/
// eslint-disable-next-line deprecation/deprecation
export function setTag(key: string, value: Primitive): ReturnType<Hub['setTag']> {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub().setTag(key, value);
Expand All @@ -162,6 +168,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.
*/
// eslint-disable-next-line deprecation/deprecation
export function setUser(user: User | null): ReturnType<Hub['setUser']> {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub().setUser(user);
Expand Down Expand Up @@ -272,6 +279,7 @@ export function withActiveSpan<T>(span: Span, callback: (scope: Scope) => T): T
export function startTransaction(
context: TransactionContext,
customSamplingContext?: CustomSamplingContext,
// eslint-disable-next-line deprecation/deprecation
): ReturnType<Hub['startTransaction']> {
// eslint-disable-next-line deprecation/deprecation
return getCurrentHub().startTransaction({ ...context }, customSamplingContext);
Expand Down
19 changes: 18 additions & 1 deletion packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface AsyncContextStrategy {
/**
* Gets the current async context. Returns undefined if there is no current async context.
*/
// eslint-disable-next-line deprecation/deprecation
getCurrentHub: () => Hub | undefined;
/**
* Runs the supplied callback in its own async context.
Expand All @@ -88,6 +89,7 @@ export interface Layer {
*/
export interface Carrier {
__SENTRY__?: {
// eslint-disable-next-line deprecation/deprecation
hub?: Hub;
acs?: AsyncContextStrategy;
/**
Expand All @@ -103,7 +105,15 @@ export interface Carrier {
}

/**
* @inheritDoc
* @deprecated The `Hub` class will be removed in version 8 of the SDK in favour of `Scope` and `Client` objects.
*
* If you previously used the `Hub` class directly, replace it with `Scope` and `Client` objects. More information:
* - [Multiple Sentry Instances](https://docs.sentry.io/platforms/javascript/best-practices/multiple-sentry-instances/)
* - [Browser Extensions](https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/)
*
* Some of our APIs are typed with the Hub class instead of the interface (e.g. `getCurrentHub`). Most of them are deprecated
* themselves and will also be removed in version 8. More information:
* - [Migration Guide](https://github.com/getsentry/sentry-javascript/blob/develop/MIGRATION.md#deprecate-hub)
*/
export class Hub implements HubInterface {
/** Is a {@link Layer}[] containing the client and scope */
Expand Down Expand Up @@ -531,6 +541,7 @@ export class Hub implements HubInterface {
/**
* @inheritDoc
*/
// eslint-disable-next-line deprecation/deprecation
public run(callback: (hub: Hub) => void): void {
// eslint-disable-next-line deprecation/deprecation
const oldHub = makeMain(this);
Expand Down Expand Up @@ -739,6 +750,7 @@ export function getMainCarrier(): Carrier {
*
* @deprecated Use `setCurrentClient()` instead.
*/
// eslint-disable-next-line deprecation/deprecation
export function makeMain(hub: Hub): Hub {
const registry = getMainCarrier();
const oldHub = getHubFromCarrier(registry);
Expand All @@ -755,6 +767,7 @@ export function makeMain(hub: Hub): Hub {
*
* @deprecated Use the respective replacement method directly instead.
*/
// eslint-disable-next-line deprecation/deprecation
export function getCurrentHub(): Hub {
// Get main carrier (global for every environment)
const registry = getMainCarrier();
Expand All @@ -781,6 +794,7 @@ export function getIsolationScope(): Scope {
return getCurrentHub().getIsolationScope();
}

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

Expand All @@ -802,6 +816,7 @@ function getGlobalHub(registry: Carrier = getMainCarrier()): Hub {
*
* If the carrier does not contain a hub, a new hub is created with the global hub client and scope.
*/
// eslint-disable-next-line deprecation/deprecation
export function ensureHubOnCarrier(carrier: Carrier, parent: Hub = getGlobalHub()): void {
// If there's no hub on current domain, or it's an old API, assign a new one
if (
Expand Down Expand Up @@ -864,6 +879,7 @@ function hasHubOnCarrier(carrier: Carrier): boolean {
* @param carrier object
* @hidden
*/
// eslint-disable-next-line deprecation/deprecation
export function getHubFromCarrier(carrier: Carrier): Hub {
// eslint-disable-next-line deprecation/deprecation
return getGlobalSingleton<Hub>('hub', () => new Hub(), carrier);
Expand All @@ -875,6 +891,7 @@ export function getHubFromCarrier(carrier: Carrier): Hub {
* @param hub Hub
* @returns A boolean indicating success or failure
*/
// eslint-disable-next-line deprecation/deprecation
export function setHubOnCarrier(carrier: Carrier, hub: Hub): boolean {
if (!carrier) return false;
const __SENTRY__ = (carrier.__SENTRY__ = carrier.__SENTRY__ || {});
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export {
getCurrentHub,
getIsolationScope,
getHubFromCarrier,
// eslint-disable-next-line deprecation/deprecation
Hub,
// eslint-disable-next-line deprecation/deprecation
makeMain,
Expand Down Expand Up @@ -84,12 +85,7 @@ export { hasTracingEnabled } from './utils/hasTracingEnabled';
export { isSentryRequestUrl } from './utils/isSentryRequestUrl';
export { handleCallbackErrors } from './utils/handleCallbackErrors';
export { parameterize } from './utils/parameterize';
export {
spanToTraceHeader,
spanToJSON,
spanIsSampled,
spanToTraceContext,
} from './utils/spanUtils';
export { spanToTraceHeader, spanToJSON, spanIsSampled, spanToTraceContext } from './utils/spanUtils';
export { getRootSpan } from './utils/getRootSpan';
export { applySdkMetadata } from './utils/sdkMetadata';
export { DEFAULT_ENVIRONMENT } from './constants';
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/tracing/hubextensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { sampleTransaction } from './sampling';
import { Transaction } from './transaction';

/** Returns all trace headers that are currently on the top scope. */
// eslint-disable-next-line deprecation/deprecation
function traceHeaders(this: Hub): { [key: string]: string } {
// eslint-disable-next-line deprecation/deprecation
const scope = this.getScope();
Expand Down Expand Up @@ -40,6 +41,7 @@ function traceHeaders(this: Hub): { [key: string]: string } {
* @see {@link Hub.startTransaction}
*/
function _startTransaction(
// eslint-disable-next-line deprecation/deprecation
this: Hub,
transactionContext: TransactionContext,
customSamplingContext?: CustomSamplingContext,
Expand Down Expand Up @@ -88,6 +90,7 @@ The transaction will not be sampled. Please use the ${configInstrumenter} instru
* Create new idle transaction.
*/
export function startIdleTransaction(
// eslint-disable-next-line deprecation/deprecation
hub: Hub,
transactionContext: TransactionContext,
idleTimeout: number,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/tracing/idletransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class IdleTransaction extends Transaction {
*/
public constructor(
transactionContext: TransactionContext,
// eslint-disable-next-line deprecation/deprecation
private readonly _idleHub: Hub,
/**
* The time to wait in ms until the idle transaction will be finished. This timer is started each time
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ export const continueTrace: ContinueTrace = <V>(
};

function createChildSpanOrTransaction(
// eslint-disable-next-line deprecation/deprecation
hub: Hub,
{
parentSpan,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/tracing/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
/**
* The reference to the current hub.
*/
// eslint-disable-next-line deprecation/deprecation
public _hub: Hub;

protected _name: string;
Expand All @@ -48,6 +49,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
*
* @deprecated Transactions will be removed in v8. Use spans instead.
*/
// eslint-disable-next-line deprecation/deprecation
public constructor(transactionContext: TransactionContext, hub?: Hub) {
super(transactionContext);
this._contexts = {};
Expand Down Expand Up @@ -250,6 +252,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
*
* @internal
*/
// eslint-disable-next-line deprecation/deprecation
public setHub(hub: Hub): void {
this._hub = hub;
}
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 @@ -9,6 +9,7 @@ import { getCurrentHub } from '../hub';
*
* @deprecated You should not rely on the transaction, but just use `startSpan()` APIs instead.
*/
// eslint-disable-next-line deprecation/deprecation
export function getActiveTransaction<T extends Transaction>(maybeHub?: Hub): T | undefined {
// eslint-disable-next-line deprecation/deprecation
const hub = maybeHub || getCurrentHub();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { addTracingExtensions } from '../../../src/tracing';
import { TestClient, getDefaultTestClientOptions } from '../../mocks/client';

describe('getDynamicSamplingContextFromSpan', () => {
// eslint-disable-next-line deprecation/deprecation
let hub: Hub;
beforeEach(() => {
const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0, release: '1.0.1' });
Expand Down
1 change: 1 addition & 0 deletions packages/core/test/lib/tracing/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const enum Type {
Async = 'async',
}

// eslint-disable-next-line deprecation/deprecation
let hub: Hub;
let client: TestClient;

Expand Down
1 change: 1 addition & 0 deletions packages/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export {
getCurrentScope,
getGlobalScope,
getIsolationScope,
// eslint-disable-next-line deprecation/deprecation
Hub,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
Expand Down
28 changes: 14 additions & 14 deletions packages/deno/test/__snapshots__/mod.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -42,47 +42,47 @@ snapshot[`captureException 1`] = `
stacktrace: {
frames: [
{
colno: 20,
colno: "{{colno}}",
filename: "ext:cli/40_testing.js",
function: "outerWrapped",
in_app: false,
lineno: 472,
lineno: "{{lineno}}",
},
{
colno: 33,
colno: "{{colno}}",
filename: "ext:cli/40_testing.js",
function: "exitSanitizer",
in_app: false,
lineno: 458,
lineno: "{{lineno}}",
},
{
colno: 31,
colno: "{{colno}}",
filename: "ext:cli/40_testing.js",
function: "resourceSanitizer",
in_app: false,
lineno: 410,
lineno: "{{lineno}}",
},
{
colno: 33,
colno: "{{colno}}",
filename: "ext:cli/40_testing.js",
function: "asyncOpSanitizer",
in_app: false,
lineno: 177,
lineno: "{{lineno}}",
},
{
colno: 11,
colno: "{{colno}}",
filename: "ext:cli/40_testing.js",
function: "innerWrapped",
in_app: false,
lineno: 526,
lineno: "{{lineno}}",
},
{
colno: 27,
colno: "{{colno}}",
context_line: " client.captureException(something());",
filename: "app:///test/mod.test.ts",
function: "<anonymous>",
in_app: true,
lineno: 47,
lineno: "{{lineno}}",
post_context: [
"",
" await delay(200);",
Expand All @@ -103,12 +103,12 @@ snapshot[`captureException 1`] = `
],
},
{
colno: 12,
colno: "{{colno}}",
context_line: " return new Error('Some unhandled error');",
filename: "app:///test/mod.test.ts",
function: "something",
in_app: true,
lineno: 44,
lineno: "{{lineno}}",
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 @@ -10,6 +10,7 @@ import { makeTestTransport } from './transport.ts';
function getTestClient(
callback: (event?: sentryTypes.Event) => void,
integrations: sentryTypes.Integration[] = [],
// eslint-disable-next-line deprecation/deprecation
): [Hub, DenoClient] {
const client = new DenoClient({
dsn: 'https://233a45e5efe34c47a3536797ce15dafa@nothing.here/5650507',
Expand Down