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
7 changes: 2 additions & 5 deletions packages/gatsby/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import { init } from '@sentry/gatsby';
import { getClient, init } from '@sentry/gatsby';

export function onClientEntry(_, pluginParams) {
const isIntialized = isSentryInitialized();
Expand Down Expand Up @@ -32,10 +32,7 @@ export function onClientEntry(_, pluginParams) {
}

function isSentryInitialized() {
// Although `window` should exist because we're in the browser (where this script
// is run), and `__SENTRY__.hub` is created when importing the Gatsby SDK, double
// check that in case something weird happens.
return !!(window && window.__SENTRY__ && window.__SENTRY__.hub && window.__SENTRY__.hub.getClient());
return !!getClient();
}

function areSentryOptionsDefined(params) {
Expand Down
31 changes: 14 additions & 17 deletions packages/gatsby/test/gatsby-browser.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-explicit-any */

import type { Client } from '@sentry/types';
import { onClientEntry } from '../gatsby-browser';
import { browserTracingIntegration } from '../src/index';
import { browserTracingIntegration, getCurrentScope, getIsolationScope, setCurrentClient } from '../src/index';

(global as any).__SENTRY_RELEASE__ = '683f3a6ab819d47d23abfca9a914c81f0524d35b';
(global as any).__SENTRY_DSN__ = 'https://[email protected]/0';
Expand Down Expand Up @@ -50,24 +48,21 @@ describe('onClientEntry', () => {
});

describe('inits Sentry once', () => {
beforeEach(() => {
getCurrentScope().clear();
getIsolationScope().clear();
getCurrentScope().setClient(undefined);
});

afterEach(() => {
delete (window as any).__SENTRY__;
(global.console.warn as jest.Mock).mockClear();
(global.console.error as jest.Mock).mockClear();
});

function setMockedSentryInWindow() {
(window as any).__SENTRY__ = {
hub: {
getClient: () => ({
// Empty object mocking the client
}),
},
};
}

it('initialized in injected config, without pluginParams', () => {
setMockedSentryInWindow();
const client = {} as Client;
setCurrentClient(client);

onClientEntry(undefined, { plugins: [] });
// eslint-disable-next-line no-console
expect(console.warn).not.toHaveBeenCalled();
Expand All @@ -77,7 +72,9 @@ describe('onClientEntry', () => {
});

it('initialized in injected config, with pluginParams', () => {
setMockedSentryInWindow();
const client = {} as Client;
setCurrentClient(client);

onClientEntry(undefined, { plugins: [], dsn: 'dsn', release: 'release' });
// eslint-disable-next-line no-console
expect((console.warn as jest.Mock).mock.calls[0]).toMatchInlineSnapshot(`
Expand Down
20 changes: 13 additions & 7 deletions packages/node/src/async/domain.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import * as domain from 'domain';
import type { Carrier } from '@sentry/core';
import { getGlobalHub } from '@sentry/core';
import { Hub as HubClass } from '@sentry/core';
import { ensureHubOnCarrier, getHubFromCarrier, setAsyncContextStrategy, setHubOnCarrier } from '@sentry/core';
import { setAsyncContextStrategy } from '@sentry/core';
import type { Client, Hub, Scope } from '@sentry/types';

type DomainWithHub = domain.Domain & {
hub?: Hub;
};

function getActiveDomain<T>(): T | undefined {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
return (domain as any).active as T | undefined;
}

function getCurrentDomainHub(): Hub | undefined {
const activeDomain = getActiveDomain<Carrier>();
const activeDomain = getActiveDomain<DomainWithHub>();

// If there's no active domain, just return undefined and the global hub will be used
if (!activeDomain) {
return undefined;
}

ensureHubOnCarrier(activeDomain);
if (activeDomain.hub) {
return activeDomain.hub;
}

return getHubFromCarrier(activeDomain);
activeDomain.hub = getCurrentHub();
return activeDomain.hub;
}

function getCurrentHub(): Hub {
Expand All @@ -33,11 +39,11 @@ function withExecutionContext<T>(
isolationScope: Scope,
callback: () => T,
): T {
const local = domain.create() as domain.Domain & Carrier;
const local = domain.create() as DomainWithHub;

// eslint-disable-next-line deprecation/deprecation
const newHub = new HubClass(client, scope, isolationScope);
setHubOnCarrier(local, newHub);
local.hub = newHub;

return local.bind(() => {
return callback();
Expand Down
8 changes: 4 additions & 4 deletions packages/node/test/async/domain.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint-disable deprecation/deprecation */
import { Hub, getCurrentHub, getCurrentScope, makeMain, setAsyncContextStrategy, withScope } from '@sentry/core';
import type { Hub } from '@sentry/core';
import { getCurrentHub, getCurrentScope, setAsyncContextStrategy, withScope } from '@sentry/core';
import { getIsolationScope, withIsolationScope } from '@sentry/core';
import type { Scope } from '@sentry/types';

import { setDomainAsyncContextStrategy } from '../../src/async/domain';

describe('setDomainAsyncContextStrategy()', () => {
beforeEach(() => {
const hub = new Hub();

makeMain(hub);
getCurrentScope().clear();
getIsolationScope().clear();
});

afterEach(() => {
Expand Down
8 changes: 3 additions & 5 deletions packages/node/test/async/hooks.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable deprecation/deprecation */
import type { Hub } from '@sentry/core';
import {
Hub,
getCurrentHub,
getCurrentScope,
getIsolationScope,
makeMain,
setAsyncContextStrategy,
withIsolationScope,
withScope,
Expand All @@ -15,9 +14,8 @@ import { setHooksAsyncContextStrategy } from '../../src/async/hooks';

describe('setHooksAsyncContextStrategy()', () => {
beforeEach(() => {
const hub = new Hub();

makeMain(hub);
getCurrentScope().clear();
getIsolationScope().clear();
});

afterEach(() => {
Expand Down
Loading