Skip to content
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
1 change: 0 additions & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"dependencies": {
"@sentry/core": "6.18.1",
"@sentry/hub": "6.18.1",
"@sentry/tracing": "6.18.1",
"@sentry/types": "6.18.1",
"@sentry/utils": "6.18.1",
"cookie": "^0.4.1",
Expand Down
12 changes: 9 additions & 3 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/* eslint-disable max-lines */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { captureException, getCurrentHub, startTransaction, withScope } from '@sentry/core';
import { extractTraceparentData, Span } from '@sentry/tracing';
import { Event, ExtractedNodeRequestData, Transaction } from '@sentry/types';
import { isPlainObject, isString, logger, normalize, stripUrlQueryAndFragment } from '@sentry/utils';
import { Event, ExtractedNodeRequestData, Span, Transaction } from '@sentry/types';
import {
extractTraceparentData,
isPlainObject,
isString,
logger,
normalize,
stripUrlQueryAndFragment,
} from '@sentry/utils';
import * as cookie from 'cookie';
import * as domain from 'domain';
import * as http from 'http';
Expand Down
47 changes: 14 additions & 33 deletions packages/tracing/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { getCurrentHub, Hub } from '@sentry/hub';
import { Options, TraceparentData, Transaction } from '@sentry/types';
import { Options, Transaction } from '@sentry/types';

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

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

/**
* Extract transaction context data from a `sentry-trace` header.
*
* @param traceparent Traceparent string
*
* @returns Object containing data from the header, or undefined if traceparent string is malformed
*/
export function extractTraceparentData(traceparent: string): TraceparentData | undefined {
const matches = traceparent.match(TRACEPARENT_REGEXP);
if (matches) {
let parentSampled: boolean | undefined;
if (matches[3] === '1') {
parentSampled = true;
} else if (matches[3] === '0') {
parentSampled = false;
}
return {
traceId: matches[1],
parentSampled,
parentSpanId: matches[2],
};
}
return undefined;
}

/** Grabs active transaction off scope, if any */
export function getActiveTransaction<T extends Transaction>(maybeHub?: Hub): T | undefined {
const hub = maybeHub || getCurrentHub();
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from './string';
export * from './supports';
export * from './syncpromise';
export * from './time';
export * from './tracing';
export * from './env';
export * from './envelope';
export * from './clientreport';
34 changes: 34 additions & 0 deletions packages/utils/src/tracing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { TraceparentData } from '@sentry/types';

export const TRACEPARENT_REGEXP = new RegExp(
'^[ \\t]*' + // whitespace
'([0-9a-f]{32})?' + // trace_id
'-?([0-9a-f]{16})?' + // span_id
'-?([01])?' + // sampled
'[ \\t]*$', // whitespace
);

/**
* Extract transaction context data from a `sentry-trace` header.
*
* @param traceparent Traceparent string
*
* @returns Object containing data from the header, or undefined if traceparent string is malformed
*/
export function extractTraceparentData(traceparent: string): TraceparentData | undefined {
const matches = traceparent.match(TRACEPARENT_REGEXP);
if (matches) {
let parentSampled: boolean | undefined;
if (matches[3] === '1') {
parentSampled = true;
} else if (matches[3] === '0') {
parentSampled = false;
}
return {
traceId: matches[1],
parentSampled,
parentSpanId: matches[2],
};
}
return undefined;
}