Skip to content

fix(node): Add conditions to TracingHandler.startTransaction #5485

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 4 commits into from
Jul 28, 2022
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
20 changes: 19 additions & 1 deletion packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ export function tracingHandler(): (
res: http.ServerResponse,
next: (error?: any) => void,
): void {
const hub = getCurrentHub();
const options = hub.getClient()?.getOptions();

if (!options || req.method?.toUpperCase() === 'OPTIONS' || req.method?.toUpperCase() === 'HEAD') {
return next();
}

// TODO: This is the `hasTracingEnabled` check, but we're doing it manually since `@sentry/tracing` isn't a
// dependency of `@sentry/node`. Long term, that function should probably move to `@sentry/hub.
if (!('tracesSampleRate' in options) && !('tracesSampler' in options)) {
__DEBUG_BUILD__ &&
logger.warn(
'Sentry `tracingHandler` is being used, but tracing is disabled. Please enable tracing by setting ' +
'either `tracesSampleRate` or `tracesSampler` in your `Sentry.init()` options.',
);
return next();
}

// If there is a trace header set, we extract the data from it (parentSpanId, traceId, and sampling decision)
const traceparentData =
req.headers && isString(req.headers['sentry-trace']) && extractTraceparentData(req.headers['sentry-trace']);
Expand All @@ -53,7 +71,7 @@ export function tracingHandler(): (
);

// We put the transaction on the scope so users can attach children to it
getCurrentHub().configureScope(scope => {
hub.configureScope(scope => {
scope.setSpan(transaction);
});

Expand Down
31 changes: 30 additions & 1 deletion packages/node/test/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,16 @@ describe('tracingHandler', () => {

const sentryTracingMiddleware = tracingHandler();

let req: http.IncomingMessage, res: http.ServerResponse, next: () => undefined;
let hub: Hub, req: http.IncomingMessage, res: http.ServerResponse, next: () => undefined;

function createNoOpSpy() {
const noop = { noop: () => undefined }; // this is wrapped in an object so jest can spy on it
return jest.spyOn(noop, 'noop') as any;
}

beforeEach(() => {
hub = new Hub(new NodeClient(getDefaultNodeClientOptions({ tracesSampleRate: 1.0 })));
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
req = {
headers,
method,
Expand All @@ -181,6 +183,33 @@ describe('tracingHandler', () => {
expect(startTransaction).toHaveBeenCalled();
});

it("doesn't create a transaction when handling a `HEAD` request", () => {
const startTransaction = jest.spyOn(sentryCore, 'startTransaction');
req.method = 'HEAD';

sentryTracingMiddleware(req, res, next);

expect(startTransaction).not.toHaveBeenCalled();
});

it("doesn't create a transaction when handling an `OPTIONS` request", () => {
const startTransaction = jest.spyOn(sentryCore, 'startTransaction');
req.method = 'OPTIONS';

sentryTracingMiddleware(req, res, next);

expect(startTransaction).not.toHaveBeenCalled();
});

it("doesn't create a transaction if tracing is disabled", () => {
delete hub.getClient()?.getOptions().tracesSampleRate;
const startTransaction = jest.spyOn(sentryCore, 'startTransaction');

sentryTracingMiddleware(req, res, next);

expect(startTransaction).not.toHaveBeenCalled();
});

it("pulls parent's data from tracing header on the request", () => {
req.headers = { 'sentry-trace': '12312012123120121231201212312012-1121201211212012-0' };

Expand Down