Skip to content

Commit 43d5df0

Browse files
committed
Move auto-instrumentation back to tracing
1 parent bf3e03f commit 43d5df0

File tree

7 files changed

+85
-92
lines changed

7 files changed

+85
-92
lines changed

Diff for: packages/core/src/tracing/hubextensions.ts

+6-82
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
import type {
2-
ClientOptions,
3-
CustomSamplingContext,
4-
Integration,
5-
IntegrationClass,
6-
Options,
7-
SamplingContext,
8-
TransactionContext,
9-
} from '@sentry/types';
10-
import { dynamicRequire, isNaN, isNodeEnv, loadModule, logger } from '@sentry/utils';
1+
import type { ClientOptions, CustomSamplingContext, Options, SamplingContext, TransactionContext } from '@sentry/types';
2+
import { isNaN, logger } from '@sentry/utils';
113

12-
import type { Hub } from '..';
13-
import { getMainCarrier } from '..';
14-
import { registerErrorInstrumentation } from './errors';
4+
import type { Hub } from '../';
5+
import { getMainCarrier } from '../';
156
import { IdleTransaction } from './idletransaction';
167
import { Transaction } from './transaction';
178
import { hasTracingEnabled } from './utils';
@@ -220,9 +211,9 @@ export function startIdleTransaction(
220211
}
221212

222213
/**
223-
* @private
214+
* Adds tracing extensions to the global hub.
224215
*/
225-
export function _addTracingExtensions(): void {
216+
export function addTracingExtensions(): void {
226217
const carrier = getMainCarrier();
227218
if (!carrier.__SENTRY__) {
228219
return;
@@ -235,70 +226,3 @@ export function _addTracingExtensions(): void {
235226
carrier.__SENTRY__.extensions.traceHeaders = traceHeaders;
236227
}
237228
}
238-
239-
/**
240-
* @private
241-
*/
242-
function _autoloadDatabaseIntegrations(): void {
243-
const carrier = getMainCarrier();
244-
if (!carrier.__SENTRY__) {
245-
return;
246-
}
247-
248-
const packageToIntegrationMapping: Record<string, () => Integration> = {
249-
mongodb() {
250-
const integration = dynamicRequire(module, './integrations/node/mongo') as {
251-
Mongo: IntegrationClass<Integration>;
252-
};
253-
return new integration.Mongo();
254-
},
255-
mongoose() {
256-
const integration = dynamicRequire(module, './integrations/node/mongo') as {
257-
Mongo: IntegrationClass<Integration>;
258-
};
259-
return new integration.Mongo({ mongoose: true });
260-
},
261-
mysql() {
262-
const integration = dynamicRequire(module, './integrations/node/mysql') as {
263-
Mysql: IntegrationClass<Integration>;
264-
};
265-
return new integration.Mysql();
266-
},
267-
pg() {
268-
const integration = dynamicRequire(module, './integrations/node/postgres') as {
269-
Postgres: IntegrationClass<Integration>;
270-
};
271-
return new integration.Postgres();
272-
},
273-
};
274-
275-
const mappedPackages = Object.keys(packageToIntegrationMapping)
276-
.filter(moduleName => !!loadModule(moduleName))
277-
.map(pkg => {
278-
try {
279-
return packageToIntegrationMapping[pkg]();
280-
} catch (e) {
281-
return undefined;
282-
}
283-
})
284-
.filter(p => p) as Integration[];
285-
286-
if (mappedPackages.length > 0) {
287-
carrier.__SENTRY__.integrations = [...(carrier.__SENTRY__.integrations || []), ...mappedPackages];
288-
}
289-
}
290-
291-
/**
292-
* This patches the global object and injects the Tracing extensions methods
293-
*/
294-
export function addExtensionMethods(): void {
295-
_addTracingExtensions();
296-
297-
// Detect and automatically load specified integrations.
298-
if (isNodeEnv()) {
299-
_autoloadDatabaseIntegrations();
300-
}
301-
302-
// If an error happens globally, we should make sure transaction status is set to error.
303-
registerErrorInstrumentation();
304-
}

Diff for: packages/core/src/tracing/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './errors';
21
export * from './hubextensions';
32
export * from './idletransaction';
43
export * from './span';

Diff for: packages/core/src/tracing/errors.ts renamed to packages/tracing/src/errors.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import type { SpanStatusType } from '@sentry/core';
2+
import { getActiveTransaction } from '@sentry/core';
13
import { addInstrumentationHandler, logger } from '@sentry/utils';
24

3-
import type { SpanStatusType } from './span';
4-
import { getActiveTransaction } from './utils';
5-
65
/**
76
* Configures global error listeners
87
*/

Diff for: packages/tracing/src/hubextensions.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { addTracingExtensions, getMainCarrier } from '@sentry/core';
2+
import type { Integration, IntegrationClass } from '@sentry/types';
3+
import { dynamicRequire, isNodeEnv, loadModule } from '@sentry/utils';
4+
5+
import { registerErrorInstrumentation } from './errors';
6+
7+
/**
8+
* @private
9+
*/
10+
function _autoloadDatabaseIntegrations(): void {
11+
const carrier = getMainCarrier();
12+
if (!carrier.__SENTRY__) {
13+
return;
14+
}
15+
16+
const packageToIntegrationMapping: Record<string, () => Integration> = {
17+
mongodb() {
18+
const integration = dynamicRequire(module, './integrations/node/mongo') as {
19+
Mongo: IntegrationClass<Integration>;
20+
};
21+
return new integration.Mongo();
22+
},
23+
mongoose() {
24+
const integration = dynamicRequire(module, './integrations/node/mongo') as {
25+
Mongo: IntegrationClass<Integration>;
26+
};
27+
return new integration.Mongo({ mongoose: true });
28+
},
29+
mysql() {
30+
const integration = dynamicRequire(module, './integrations/node/mysql') as {
31+
Mysql: IntegrationClass<Integration>;
32+
};
33+
return new integration.Mysql();
34+
},
35+
pg() {
36+
const integration = dynamicRequire(module, './integrations/node/postgres') as {
37+
Postgres: IntegrationClass<Integration>;
38+
};
39+
return new integration.Postgres();
40+
},
41+
};
42+
43+
const mappedPackages = Object.keys(packageToIntegrationMapping)
44+
.filter(moduleName => !!loadModule(moduleName))
45+
.map(pkg => {
46+
try {
47+
return packageToIntegrationMapping[pkg]();
48+
} catch (e) {
49+
return undefined;
50+
}
51+
})
52+
.filter(p => p) as Integration[];
53+
54+
if (mappedPackages.length > 0) {
55+
carrier.__SENTRY__.integrations = [...(carrier.__SENTRY__.integrations || []), ...mappedPackages];
56+
}
57+
}
58+
59+
/**
60+
* This patches the global object and injects the Tracing extensions methods
61+
*/
62+
export function addExtensionMethods(): void {
63+
addTracingExtensions();
64+
65+
// Detect and automatically load specified integrations.
66+
if (isNodeEnv()) {
67+
_autoloadDatabaseIntegrations();
68+
}
69+
70+
// If an error happens globally, we should make sure transaction status is set to error.
71+
registerErrorInstrumentation();
72+
}

Diff for: packages/tracing/src/index.bundle.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ export {
5454
export { SDK_VERSION } from '@sentry/browser';
5555

5656
import { Integrations as BrowserIntegrations } from '@sentry/browser';
57-
import { addExtensionMethods } from '@sentry/core';
5857
import type { Integration } from '@sentry/types';
5958
import { GLOBAL_OBJ } from '@sentry/utils';
6059

6160
import { BrowserTracing } from './browser';
61+
import { addExtensionMethods } from './hubextensions';
6262

6363
export { Span } from '@sentry/core';
6464

Diff for: packages/tracing/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { addExtensionMethods } from '@sentry/core';
21
export {
32
extractTraceparentData,
43
getActiveTransaction,
@@ -15,6 +14,7 @@ export {
1514
} from '@sentry/core';
1615
export type { SpanStatusType } from '@sentry/core';
1716

17+
import { addExtensionMethods } from './hubextensions';
1818
import * as Integrations from './integrations';
1919

2020
export type { RequestInstrumentationOptions } from './browser';

Diff for: packages/tracing/test/errors.test.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { BrowserClient } from '@sentry/browser';
2-
import { Hub, makeMain } from '@sentry/core';
2+
import { addTracingExtensions, Hub, makeMain } from '@sentry/core';
33
import type { InstrumentHandlerCallback, InstrumentHandlerType } from '@sentry/utils';
44

5-
import { registerErrorInstrumentation } from '../../core/src/tracing/errors';
6-
import { _addTracingExtensions } from '../../core/src/tracing/hubextensions';
5+
import { registerErrorInstrumentation } from '../src/errors';
76
import { getDefaultBrowserClientOptions } from './testutils';
87

98
const mockAddInstrumentationHandler = jest.fn();
@@ -28,7 +27,7 @@ jest.mock('@sentry/utils', () => {
2827
});
2928

3029
beforeAll(() => {
31-
_addTracingExtensions();
30+
addTracingExtensions();
3231
});
3332

3433
describe('registerErrorHandlers()', () => {

0 commit comments

Comments
 (0)