Skip to content

ref: Make scope setters on hub only write to isolation scope #10572

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 7 commits into from
Feb 12, 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
11 changes: 6 additions & 5 deletions packages/astro/test/client/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { BrowserClient } from '@sentry/browser';
import { getActiveSpan } from '@sentry/browser';
import { browserTracingIntegration, getCurrentScope } from '@sentry/browser';
import { browserTracingIntegration } from '@sentry/browser';
import * as SentryBrowser from '@sentry/browser';
import { BrowserTracing, SDK_VERSION, WINDOW, getClient } from '@sentry/browser';
import { vi } from 'vitest';

import { getIsolationScope } from '@sentry/core';
import { init } from '../../../astro/src/client/sdk';

const browserInit = vi.spyOn(SentryBrowser, 'init');
Expand Down Expand Up @@ -38,16 +39,16 @@ describe('Sentry client SDK', () => {
);
});

it('sets the runtime tag on the scope', () => {
const currentScope = getCurrentScope();
it('sets the runtime tag on the isolation scope', () => {
const isolationScope = getIsolationScope();

// @ts-expect-error need access to protected _tags attribute
expect(currentScope._tags).toEqual({});
expect(isolationScope._tags).toEqual({});

init({ dsn: 'https://[email protected]/1337' });

// @ts-expect-error need access to protected _tags attribute
expect(currentScope._tags).toEqual({ runtime: 'browser' });
expect(isolationScope._tags).toEqual({ runtime: 'browser' });
});

describe('automatically adds integrations', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/astro/test/server/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ describe('Sentry server SDK', () => {
);
});

it('sets the runtime tag on the scope', () => {
const currentScope = SentryNode.getCurrentScope();
it('sets the runtime tag on the isolation scope', () => {
const isolationScope = SentryNode.getIsolationScope();

// @ts-expect-error need access to protected _tags attribute
expect(currentScope._tags).toEqual({});
expect(isolationScope._tags).toEqual({});

init({ dsn: 'https://[email protected]/1337' });

// @ts-expect-error need access to protected _tags attribute
expect(currentScope._tags).toEqual({ runtime: 'node' });
expect(isolationScope._tags).toEqual({ runtime: 'node' });
});
});
});
18 changes: 0 additions & 18 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,6 @@ export class Hub implements HubInterface {
* @deprecated Use `Sentry.setUser()` instead.
*/
public setUser(user: User | null): void {
// TODO(v8): The top level `Sentry.setUser()` function should write ONLY to the isolation scope.
// eslint-disable-next-line deprecation/deprecation
this.getScope().setUser(user);
// eslint-disable-next-line deprecation/deprecation
this.getIsolationScope().setUser(user);
}
Expand All @@ -425,9 +422,6 @@ export class Hub implements HubInterface {
* @deprecated Use `Sentry.setTags()` instead.
*/
public setTags(tags: { [key: string]: Primitive }): void {
// TODO(v8): The top level `Sentry.setTags()` function should write ONLY to the isolation scope.
// eslint-disable-next-line deprecation/deprecation
this.getScope().setTags(tags);
// eslint-disable-next-line deprecation/deprecation
this.getIsolationScope().setTags(tags);
}
Expand All @@ -437,9 +431,6 @@ export class Hub implements HubInterface {
* @deprecated Use `Sentry.setExtras()` instead.
*/
public setExtras(extras: Extras): void {
// TODO(v8): The top level `Sentry.setExtras()` function should write ONLY to the isolation scope.
// eslint-disable-next-line deprecation/deprecation
this.getScope().setExtras(extras);
// eslint-disable-next-line deprecation/deprecation
this.getIsolationScope().setExtras(extras);
}
Expand All @@ -449,9 +440,6 @@ export class Hub implements HubInterface {
* @deprecated Use `Sentry.setTag()` instead.
*/
public setTag(key: string, value: Primitive): void {
// TODO(v8): The top level `Sentry.setTag()` function should write ONLY to the isolation scope.
// eslint-disable-next-line deprecation/deprecation
this.getScope().setTag(key, value);
// eslint-disable-next-line deprecation/deprecation
this.getIsolationScope().setTag(key, value);
}
Expand All @@ -461,9 +449,6 @@ export class Hub implements HubInterface {
* @deprecated Use `Sentry.setExtra()` instead.
*/
public setExtra(key: string, extra: Extra): void {
// TODO(v8): The top level `Sentry.setExtra()` function should write ONLY to the isolation scope.
// eslint-disable-next-line deprecation/deprecation
this.getScope().setExtra(key, extra);
// eslint-disable-next-line deprecation/deprecation
this.getIsolationScope().setExtra(key, extra);
}
Expand All @@ -474,9 +459,6 @@ export class Hub implements HubInterface {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public setContext(name: string, context: { [key: string]: any } | null): void {
// TODO(v8): The top level `Sentry.setContext()` function should write ONLY to the isolation scope.
// eslint-disable-next-line deprecation/deprecation
this.getScope().setContext(name, context);
// eslint-disable-next-line deprecation/deprecation
this.getIsolationScope().setContext(name, context);
}
Expand Down
41 changes: 1 addition & 40 deletions packages/nextjs/test/serverSdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { runWithAsyncContext } from '@sentry/core';
import * as SentryNode from '@sentry/node';
import { NodeClient, getClient, getCurrentHub, getCurrentScope } from '@sentry/node';
import { getClient, getCurrentScope } from '@sentry/node';
import type { Integration } from '@sentry/types';
import { GLOBAL_OBJ, logger } from '@sentry/utils';

Expand Down Expand Up @@ -113,44 +112,6 @@ describe('Server init()', () => {
expect(loggerLogSpy).toHaveBeenCalledWith('An event processor returned `null`, will not send event.');
});

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
expect(globalHub2).toBe(globalHub);
// eslint-disable-next-line deprecation/deprecation
expect(globalHub.getClient()).toBeUndefined();
// eslint-disable-next-line deprecation/deprecation
expect(globalHub2.getClient()).toBeUndefined();

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
domainHub.setTag('dogs', 'areGreat');

// eslint-disable-next-line deprecation/deprecation
expect(globalHub.getClient()).toEqual(expect.any(NodeClient));
// eslint-disable-next-line deprecation/deprecation
expect(domainHub.getClient()).toBe(globalHub.getClient());
// @ts-expect-error need access to protected _tags attribute
// eslint-disable-next-line deprecation/deprecation
expect(globalHub.getScope()._tags).toEqual({ runtime: 'node' });
// @ts-expect-error need access to protected _tags attribute
// eslint-disable-next-line deprecation/deprecation
expect(domainHub.getScope()._tags).toEqual({ runtime: 'node', dogs: 'areGreat' });
});
});
});

describe('integrations', () => {
// Options passed by `@sentry/nextjs`'s `init` to `@sentry/node`'s `init` after modifying them
type ModifiedInitOptions = { integrations: Integration[]; defaultIntegrations: Integration[] };
Expand Down
6 changes: 3 additions & 3 deletions packages/sveltekit/src/client/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { applySdkMetadata, hasTracingEnabled } from '@sentry/core';
import { applySdkMetadata, hasTracingEnabled, setTag } from '@sentry/core';
import type { BrowserOptions, browserTracingIntegration } from '@sentry/svelte';
import { getDefaultIntegrations as getDefaultSvelteIntegrations } from '@sentry/svelte';
import { WINDOW, getCurrentScope, init as initSvelteSdk } from '@sentry/svelte';
import { WINDOW, init as initSvelteSdk } from '@sentry/svelte';
import type { Integration } from '@sentry/types';

import {
Expand Down Expand Up @@ -42,7 +42,7 @@ export function init(options: BrowserOptions): void {
restoreFetch(actualFetch);
}

getCurrentScope().setTag('runtime', 'browser');
setTag('runtime', 'browser');
}

// TODO v8: Remove this again
Expand Down
4 changes: 2 additions & 2 deletions packages/sveltekit/src/server/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { applySdkMetadata, getCurrentScope } from '@sentry/core';
import { applySdkMetadata, setTag } from '@sentry/core';
import type { NodeOptions } from '@sentry/node';
import { getDefaultIntegrations as getDefaultNodeIntegrations } from '@sentry/node';
import { init as initNodeSdk } from '@sentry/node';
Expand All @@ -19,5 +19,5 @@ export function init(options: NodeOptions): void {

initNodeSdk(opts);

getCurrentScope().setTag('runtime', 'node');
setTag('runtime', 'node');
}
10 changes: 5 additions & 5 deletions packages/sveltekit/test/client/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getClient, getCurrentScope } from '@sentry/core';
import { getClient, getIsolationScope } from '@sentry/core';
import type { BrowserClient } from '@sentry/svelte';
import * as SentrySvelte from '@sentry/svelte';
import { SDK_VERSION, WINDOW, browserTracingIntegration } from '@sentry/svelte';
Expand Down Expand Up @@ -38,16 +38,16 @@ describe('Sentry client SDK', () => {
);
});

it('sets the runtime tag on the scope', () => {
const currentScope = getCurrentScope();
it('sets the runtime tag on the isolation scope', () => {
const isolationScope = getIsolationScope();

// @ts-expect-error need access to protected _tags attribute
expect(currentScope._tags).toEqual({});
expect(isolationScope._tags).toEqual({});

init({ dsn: 'https://[email protected]/1337' });

// @ts-expect-error need access to protected _tags attribute
expect(currentScope._tags).toEqual({ runtime: 'browser' });
expect(isolationScope._tags).toEqual({ runtime: 'browser' });
});

describe('automatically added integrations', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/sveltekit/test/server/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ describe('Sentry server SDK', () => {
);
});

it('sets the runtime tag on the scope', () => {
const currentScope = SentryNode.getCurrentScope();
it('sets the runtime tag on the isolation scope', () => {
const isolationScope = SentryNode.getIsolationScope();

// @ts-expect-error need access to protected _tags attribute
expect(currentScope._tags).toEqual({});
expect(isolationScope._tags).toEqual({});

init({ dsn: 'https://[email protected]/1337' });

// @ts-expect-error need access to protected _tags attribute
expect(currentScope._tags).toEqual({ runtime: 'node' });
expect(isolationScope._tags).toEqual({ runtime: 'node' });
});

it('adds rewriteFramesIntegration by default', () => {
Expand Down