Skip to content

Commit a306abd

Browse files
AbhiPrasadtimfish
andauthored
feat(tracing): Move common tracing code to core (#7339)
Co-authored-by: Tim Fish <[email protected]>
1 parent cf430fe commit a306abd

33 files changed

+223
-240
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export type { ClientClass } from './sdk';
22
export type { Carrier, Layer } from './hub';
33
export type { OfflineStore, OfflineTransportOptions } from './transports/offline';
44

5+
export * from './tracing';
56
export {
67
addBreadcrumb,
78
captureException,

packages/tracing/src/hubextensions.ts renamed to packages/core/src/tracing/hubextensions.ts

Lines changed: 7 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
import type { Hub } from '@sentry/core';
2-
import { getMainCarrier, hasTracingEnabled } from '@sentry/core';
3-
import type {
4-
ClientOptions,
5-
CustomSamplingContext,
6-
Integration,
7-
IntegrationClass,
8-
Options,
9-
SamplingContext,
10-
TransactionContext,
11-
} from '@sentry/types';
12-
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';
133

14-
import { registerErrorInstrumentation } from './errors';
4+
import type { Hub } from '../hub';
5+
import { getMainCarrier } from '../hub';
6+
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
157
import { IdleTransaction } from './idletransaction';
168
import { Transaction } from './transaction';
179

@@ -225,9 +217,9 @@ export function startIdleTransaction(
225217
}
226218

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

packages/tracing/src/idletransaction.ts renamed to packages/core/src/tracing/idletransaction.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
/* eslint-disable max-lines */
2-
import type { Hub } from '@sentry/core';
32
import type { TransactionContext } from '@sentry/types';
43
import { logger, timestampWithMs } from '@sentry/utils';
54

5+
import type { Hub } from '../hub';
66
import type { Span } from './span';
77
import { SpanRecorder } from './span';
88
import { Transaction } from './transaction';
99

10-
export const DEFAULT_IDLE_TIMEOUT = 1000;
11-
export const DEFAULT_FINAL_TIMEOUT = 30000;
12-
export const DEFAULT_HEARTBEAT_INTERVAL = 5000;
10+
export const TRACING_DEFAULTS = {
11+
idleTimeout: 1000,
12+
finalTimeout: 30000,
13+
heartbeatInterval: 5000,
14+
};
1315

1416
/**
1517
* @inheritDoc
@@ -81,12 +83,12 @@ export class IdleTransaction extends Transaction {
8183
* The time to wait in ms until the idle transaction will be finished. This timer is started each time
8284
* there are no active spans on this transaction.
8385
*/
84-
private readonly _idleTimeout: number = DEFAULT_IDLE_TIMEOUT,
86+
private readonly _idleTimeout: number = TRACING_DEFAULTS.idleTimeout,
8587
/**
8688
* The final value in ms that a transaction cannot exceed
8789
*/
88-
private readonly _finalTimeout: number = DEFAULT_FINAL_TIMEOUT,
89-
private readonly _heartbeatInterval: number = DEFAULT_HEARTBEAT_INTERVAL,
90+
private readonly _finalTimeout: number = TRACING_DEFAULTS.finalTimeout,
91+
private readonly _heartbeatInterval: number = TRACING_DEFAULTS.heartbeatInterval,
9092
// Whether or not the transaction should put itself on the scope when it starts and pop itself off when it ends
9193
private readonly _onScope: boolean = false,
9294
) {

packages/core/src/tracing/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export { startIdleTransaction, addTracingExtensions } from './hubextensions';
2+
export { IdleTransaction, TRACING_DEFAULTS } from './idletransaction';
3+
export { Span, spanStatusfromHttpCode } from './span';
4+
export { Transaction } from './transaction';
5+
export { extractTraceparentData, getActiveTransaction, stripUrlQueryAndFragment, TRACEPARENT_REGEXP } from './utils';
6+
// eslint-disable-next-line deprecation/deprecation
7+
export { SpanStatus } from './spanstatus';
8+
export type { SpanStatusType } from './span';
File renamed without changes.

packages/tracing/src/transaction.ts renamed to packages/core/src/tracing/transaction.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { Hub } from '@sentry/core';
2-
import { DEFAULT_ENVIRONMENT, getCurrentHub } from '@sentry/core';
31
import type {
42
Context,
53
Contexts,
@@ -13,6 +11,9 @@ import type {
1311
} from '@sentry/types';
1412
import { dropUndefinedKeys, logger } from '@sentry/utils';
1513

14+
import { DEFAULT_ENVIRONMENT } from '../constants';
15+
import type { Hub } from '../hub';
16+
import { getCurrentHub } from '../hub';
1617
import { Span as SpanClass, SpanRecorder } from './span';
1718

1819
/** JSDoc */
Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import type { Hub } from '@sentry/core';
2-
import { getCurrentHub, hasTracingEnabled as _hasTracingEnabled } from '@sentry/core';
3-
import type { Options, Transaction } from '@sentry/types';
1+
import type { Transaction } from '@sentry/types';
2+
3+
import type { Hub } from '../hub';
4+
import { getCurrentHub } from '../hub';
45

56
/**
67
* The `extractTraceparentData` function and `TRACEPARENT_REGEXP` constant used
@@ -16,40 +17,12 @@ import type { Options, Transaction } from '@sentry/types';
1617
*/
1718
export { TRACEPARENT_REGEXP, extractTraceparentData } from '@sentry/utils';
1819

19-
/**
20-
* Determines if tracing is currently enabled.
21-
*
22-
* Tracing is enabled when at least one of `tracesSampleRate` and `tracesSampler` is defined in the SDK config.
23-
* @deprecated This export has moved to `@sentry/core`. This export will be removed from `@sentry/tracing` in v8.
24-
*/
25-
export function hasTracingEnabled(
26-
maybeOptions?: Pick<Options, 'tracesSampleRate' | 'tracesSampler' | 'enableTracing'> | undefined,
27-
): boolean {
28-
return _hasTracingEnabled(maybeOptions);
29-
}
30-
3120
/** Grabs active transaction off scope, if any */
3221
export function getActiveTransaction<T extends Transaction>(maybeHub?: Hub): T | undefined {
3322
const hub = maybeHub || getCurrentHub();
3423
const scope = hub.getScope();
3524
return scope && (scope.getTransaction() as T | undefined);
3625
}
3726

38-
/**
39-
* Converts from milliseconds to seconds
40-
* @param time time in ms
41-
*/
42-
export function msToSec(time: number): number {
43-
return time / 1000;
44-
}
45-
46-
/**
47-
* Converts from seconds to milliseconds
48-
* @param time time in seconds
49-
*/
50-
export function secToMs(time: number): number {
51-
return time * 1000;
52-
}
53-
5427
// so it can be used in manual instrumentation without necessitating a hard dependency on @sentry/utils
5528
export { stripUrlQueryAndFragment } from '@sentry/utils';

packages/nextjs/src/index.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export declare const ErrorBoundary: typeof clientSdk.ErrorBoundary;
3636
export declare const showReportDialog: typeof clientSdk.showReportDialog;
3737
export declare const withErrorBoundary: typeof clientSdk.withErrorBoundary;
3838

39+
export declare const Span: typeof edgeSdk.Span;
40+
3941
/**
4042
* @deprecated Use `wrapApiHandlerWithSentry` instead
4143
*/

packages/tracing/src/browser/backgroundtab.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import type { IdleTransaction, SpanStatusType } from '@sentry/core';
2+
import { getActiveTransaction } from '@sentry/core';
13
import { logger } from '@sentry/utils';
24

3-
import type { IdleTransaction } from '../idletransaction';
4-
import type { SpanStatusType } from '../span';
5-
import { getActiveTransaction } from '../utils';
65
import { WINDOW } from './types';
76

87
/**

0 commit comments

Comments
 (0)