Skip to content

Commit a3eed12

Browse files
authored
ref(tracing): Consolidate utils (#2917)
1 parent 9ae55d4 commit a3eed12

File tree

10 files changed

+39
-40
lines changed

10 files changed

+39
-40
lines changed

packages/tracing/src/browser/backgroundtab.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getGlobalObject, logger } from '@sentry/utils';
22

33
import { IdleTransaction } from '../idletransaction';
44
import { SpanStatus } from '../spanstatus';
5-
import { getActiveTransaction } from './utils';
5+
import { getActiveTransaction } from '../utils';
66

77
const global = getGlobalObject<Window>();
88

packages/tracing/src/browser/browsertracing.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { logger } from '@sentry/utils';
55
import { startIdleTransaction } from '../hubextensions';
66
import { DEFAULT_IDLE_TIMEOUT, IdleTransaction } from '../idletransaction';
77
import { SpanStatus } from '../spanstatus';
8-
import { extractTraceparentData } from '../utils';
8+
import { extractTraceparentData, secToMs } from '../utils';
99
import { registerBackgroundTabDetection } from './backgroundtab';
1010
import { MetricsInstrumentation } from './metrics';
1111
import {
@@ -14,7 +14,6 @@ import {
1414
RequestInstrumentationOptions,
1515
} from './request';
1616
import { defaultBeforeNavigate, defaultRoutingInstrumentation } from './router';
17-
import { secToMs } from './utils';
1817

1918
export const DEFAULT_MAX_TRANSACTION_DURATION_SECONDS = 600;
2019

packages/tracing/src/browser/metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getGlobalObject, logger } from '@sentry/utils';
55

66
import { Span } from '../span';
77
import { Transaction } from '../transaction';
8-
import { msToSec } from './utils';
8+
import { msToSec } from '../utils';
99

1010
const global = getGlobalObject<Window>();
1111

packages/tracing/src/browser/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { addInstrumentationHandler, isInstanceOf, isMatchingPattern } from '@sentry/utils';
22

33
import { Span } from '../span';
4-
import { getActiveTransaction } from './utils';
4+
import { getActiveTransaction } from '../utils';
55

66
export const DEFAULT_TRACING_ORIGINS = ['localhost', /^\//];
77

packages/tracing/src/browser/utils.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

packages/tracing/src/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { addInstrumentationHandler, logger } from '@sentry/utils';
22

3-
import { getActiveTransaction } from './browser/utils';
43
import { SpanStatus } from './spanstatus';
4+
import { getActiveTransaction } from './utils';
55

66
/**
77
* Configures global error listeners

packages/tracing/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ addExtensionMethods();
1515

1616
export { addExtensionMethods };
1717

18-
export * from './utils';
18+
export { extractTraceparentData, getActiveTransaction, hasTracingEnabled, stripUrlQueryAndFragment } from './utils';

packages/tracing/src/utils.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Options, TraceparentData } from '@sentry/types';
1+
import { getCurrentHub, Hub } from '@sentry/hub';
2+
import { Options, TraceparentData, Transaction } from '@sentry/types';
23

34
export const TRACEPARENT_REGEXP = new RegExp(
45
'^[ \\t]*' + // whitespace
@@ -42,5 +43,33 @@ export function extractTraceparentData(traceparent: string): TraceparentData | u
4243
return undefined;
4344
}
4445

46+
/** Grabs active transaction off scope */
47+
export function getActiveTransaction<T extends Transaction>(hub: Hub = getCurrentHub()): T | undefined {
48+
if (hub) {
49+
const scope = hub.getScope();
50+
if (scope) {
51+
return scope.getTransaction() as T | undefined;
52+
}
53+
}
54+
55+
return undefined;
56+
}
57+
58+
/**
59+
* Converts from milliseconds to seconds
60+
* @param time time in ms
61+
*/
62+
export function msToSec(time: number): number {
63+
return time / 1000;
64+
}
65+
66+
/**
67+
* Converts from seconds to milliseconds
68+
* @param time time in seconds
69+
*/
70+
export function secToMs(time: number): number {
71+
return time * 1000;
72+
}
73+
4574
// so it can be used in manual instrumentation without necessitating a hard dependency on @sentry/utils
4675
export { stripUrlQueryAndFragment } from '@sentry/utils';

packages/tracing/test/browser/browsertracing.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
} from '../../src/browser/browsertracing';
1212
import { defaultRequestInstrumentionOptions } from '../../src/browser/request';
1313
import { defaultRoutingInstrumentation } from '../../src/browser/router';
14-
import { getActiveTransaction, secToMs } from '../../src/browser/utils';
1514
import { DEFAULT_IDLE_TIMEOUT, IdleTransaction } from '../../src/idletransaction';
15+
import { getActiveTransaction, secToMs } from '../../src/utils';
1616

1717
let mockChangeHistory: ({ to, from }: { to: string; from?: string }) => void = () => undefined;
1818

packages/tracing/test/span.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { BrowserClient } from '@sentry/browser';
22
import { Hub, Scope } from '@sentry/hub';
33

4-
import { Span, SpanStatus, TRACEPARENT_REGEXP, Transaction } from '../src';
4+
import { Span, SpanStatus, Transaction } from '../src';
5+
import { TRACEPARENT_REGEXP } from '../src/utils';
56

67
describe('Span', () => {
78
let hub: Hub;

0 commit comments

Comments
 (0)