Skip to content

Commit 3ad3834

Browse files
committed
ref(tracing): Move extractTraceparentData into @sentry/utils
1 parent 8b44489 commit 3ad3834

File tree

4 files changed

+58
-36
lines changed

4 files changed

+58
-36
lines changed

packages/node/src/handlers.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
/* eslint-disable max-lines */
22
/* eslint-disable @typescript-eslint/no-explicit-any */
33
import { captureException, getCurrentHub, startTransaction, withScope } from '@sentry/core';
4-
import { extractTraceparentData, Span } from '@sentry/tracing';
5-
import { Event, ExtractedNodeRequestData, Transaction } from '@sentry/types';
6-
import { isPlainObject, isString, logger, normalize, stripUrlQueryAndFragment } from '@sentry/utils';
4+
import { Event, ExtractedNodeRequestData, Span, Transaction } from '@sentry/types';
5+
import {
6+
extractTraceparentData,
7+
isPlainObject,
8+
isString,
9+
logger,
10+
normalize,
11+
stripUrlQueryAndFragment,
12+
} from '@sentry/utils';
713
import * as cookie from 'cookie';
814
import * as domain from 'domain';
915
import * as http from 'http';

packages/tracing/src/utils.ts

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { getCurrentHub, Hub } from '@sentry/hub';
2-
import { Options, TraceparentData, Transaction } from '@sentry/types';
2+
import { Options, Transaction } from '@sentry/types';
33

4-
export const TRACEPARENT_REGEXP = new RegExp(
5-
'^[ \\t]*' + // whitespace
6-
'([0-9a-f]{32})?' + // trace_id
7-
'-?([0-9a-f]{16})?' + // span_id
8-
'-?([01])?' + // sampled
9-
'[ \\t]*$', // whitespace
10-
);
4+
/**
5+
* The `extractTraceparentData` function and `TRACEPARENT_REGEXP` constant used
6+
* to be declared in this file. It was later moved into `@sentry/utils` as part of a
7+
* move to remove `@sentry/tracing` dependencies from `@sentry/node` (`extractTraceparentData`
8+
* is the only tracing function used by `@sentry/node`).
9+
*
10+
* These exports are kept here for backwards compatability's sack.
11+
*
12+
* TODO(v7): Reorganize these exports
13+
*
14+
* See https://github.com/getsentry/sentry-javascript/issues/4642 for more details.
15+
*/
16+
export { TRACEPARENT_REGEXP, extractTraceparentData } from '@sentry/utils';
1117

1218
/**
1319
* Determines if tracing is currently enabled.
@@ -20,31 +26,6 @@ export function hasTracingEnabled(maybeOptions?: Options | undefined): boolean {
2026
return !!options && ('tracesSampleRate' in options || 'tracesSampler' in options);
2127
}
2228

23-
/**
24-
* Extract transaction context data from a `sentry-trace` header.
25-
*
26-
* @param traceparent Traceparent string
27-
*
28-
* @returns Object containing data from the header, or undefined if traceparent string is malformed
29-
*/
30-
export function extractTraceparentData(traceparent: string): TraceparentData | undefined {
31-
const matches = traceparent.match(TRACEPARENT_REGEXP);
32-
if (matches) {
33-
let parentSampled: boolean | undefined;
34-
if (matches[3] === '1') {
35-
parentSampled = true;
36-
} else if (matches[3] === '0') {
37-
parentSampled = false;
38-
}
39-
return {
40-
traceId: matches[1],
41-
parentSampled,
42-
parentSpanId: matches[2],
43-
};
44-
}
45-
return undefined;
46-
}
47-
4829
/** Grabs active transaction off scope, if any */
4930
export function getActiveTransaction<T extends Transaction>(maybeHub?: Hub): T | undefined {
5031
const hub = maybeHub || getCurrentHub();

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export * from './string';
2020
export * from './supports';
2121
export * from './syncpromise';
2222
export * from './time';
23+
export * from './tracing';
2324
export * from './env';
2425
export * from './envelope';
2526
export * from './clientreport';

packages/utils/src/tracing.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { TraceparentData } from '@sentry/types';
2+
3+
export const TRACEPARENT_REGEXP = new RegExp(
4+
'^[ \\t]*' + // whitespace
5+
'([0-9a-f]{32})?' + // trace_id
6+
'-?([0-9a-f]{16})?' + // span_id
7+
'-?([01])?' + // sampled
8+
'[ \\t]*$', // whitespace
9+
);
10+
11+
/**
12+
* Extract transaction context data from a `sentry-trace` header.
13+
*
14+
* @param traceparent Traceparent string
15+
*
16+
* @returns Object containing data from the header, or undefined if traceparent string is malformed
17+
*/
18+
export function extractTraceparentData(traceparent: string): TraceparentData | undefined {
19+
const matches = traceparent.match(TRACEPARENT_REGEXP);
20+
if (matches) {
21+
let parentSampled: boolean | undefined;
22+
if (matches[3] === '1') {
23+
parentSampled = true;
24+
} else if (matches[3] === '0') {
25+
parentSampled = false;
26+
}
27+
return {
28+
traceId: matches[1],
29+
parentSampled,
30+
parentSpanId: matches[2],
31+
};
32+
}
33+
return undefined;
34+
}

0 commit comments

Comments
 (0)