Skip to content

ref(tracing): Consolidate utils #2917

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 1 commit into from
Sep 17, 2020
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
2 changes: 1 addition & 1 deletion packages/tracing/src/browser/backgroundtab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getGlobalObject, logger } from '@sentry/utils';

import { IdleTransaction } from '../idletransaction';
import { SpanStatus } from '../spanstatus';
import { getActiveTransaction } from './utils';
import { getActiveTransaction } from '../utils';

const global = getGlobalObject<Window>();

Expand Down
3 changes: 1 addition & 2 deletions packages/tracing/src/browser/browsertracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { logger } from '@sentry/utils';
import { startIdleTransaction } from '../hubextensions';
import { DEFAULT_IDLE_TIMEOUT, IdleTransaction } from '../idletransaction';
import { SpanStatus } from '../spanstatus';
import { extractTraceparentData } from '../utils';
import { extractTraceparentData, secToMs } from '../utils';
import { registerBackgroundTabDetection } from './backgroundtab';
import { MetricsInstrumentation } from './metrics';
import {
Expand All @@ -14,7 +14,6 @@ import {
RequestInstrumentationOptions,
} from './request';
import { defaultBeforeNavigate, defaultRoutingInstrumentation } from './router';
import { secToMs } from './utils';

export const DEFAULT_MAX_TRANSACTION_DURATION_SECONDS = 600;

Expand Down
2 changes: 1 addition & 1 deletion packages/tracing/src/browser/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getGlobalObject, logger } from '@sentry/utils';

import { Span } from '../span';
import { Transaction } from '../transaction';
import { msToSec } from './utils';
import { msToSec } from '../utils';

const global = getGlobalObject<Window>();

Expand Down
2 changes: 1 addition & 1 deletion packages/tracing/src/browser/request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { addInstrumentationHandler, isInstanceOf, isMatchingPattern } from '@sentry/utils';

import { Span } from '../span';
import { getActiveTransaction } from './utils';
import { getActiveTransaction } from '../utils';

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

Expand Down
30 changes: 0 additions & 30 deletions packages/tracing/src/browser/utils.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/tracing/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { addInstrumentationHandler, logger } from '@sentry/utils';

import { getActiveTransaction } from './browser/utils';
import { SpanStatus } from './spanstatus';
import { getActiveTransaction } from './utils';

/**
* Configures global error listeners
Expand Down
2 changes: 1 addition & 1 deletion packages/tracing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ addExtensionMethods();

export { addExtensionMethods };

export * from './utils';
export { extractTraceparentData, getActiveTransaction, hasTracingEnabled, stripUrlQueryAndFragment } from './utils';
31 changes: 30 additions & 1 deletion packages/tracing/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Options, TraceparentData } from '@sentry/types';
import { getCurrentHub, Hub } from '@sentry/hub';
import { Options, TraceparentData, Transaction } from '@sentry/types';

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

/** Grabs active transaction off scope */
export function getActiveTransaction<T extends Transaction>(hub: Hub = getCurrentHub()): T | undefined {
if (hub) {
const scope = hub.getScope();
if (scope) {
return scope.getTransaction() as T | undefined;
}
}

return undefined;
}

/**
* Converts from milliseconds to seconds
* @param time time in ms
*/
export function msToSec(time: number): number {
return time / 1000;
}

/**
* Converts from seconds to milliseconds
* @param time time in seconds
*/
export function secToMs(time: number): number {
return time * 1000;
}

// so it can be used in manual instrumentation without necessitating a hard dependency on @sentry/utils
export { stripUrlQueryAndFragment } from '@sentry/utils';
2 changes: 1 addition & 1 deletion packages/tracing/test/browser/browsertracing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
} from '../../src/browser/browsertracing';
import { defaultRequestInstrumentionOptions } from '../../src/browser/request';
import { defaultRoutingInstrumentation } from '../../src/browser/router';
import { getActiveTransaction, secToMs } from '../../src/browser/utils';
import { DEFAULT_IDLE_TIMEOUT, IdleTransaction } from '../../src/idletransaction';
import { getActiveTransaction, secToMs } from '../../src/utils';

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

Expand Down
3 changes: 2 additions & 1 deletion packages/tracing/test/span.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BrowserClient } from '@sentry/browser';
import { Hub, Scope } from '@sentry/hub';

import { Span, SpanStatus, TRACEPARENT_REGEXP, Transaction } from '../src';
import { Span, SpanStatus, Transaction } from '../src';
import { TRACEPARENT_REGEXP } from '../src/utils';

describe('Span', () => {
let hub: Hub;
Expand Down