Skip to content

Commit

Permalink
ref(node): Extract propagation context in tracingHandler (#8425)
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad authored Jul 4, 2023
1 parent 9ede8a3 commit a3178f7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
15 changes: 8 additions & 7 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ import type { AddRequestDataToEventOptions } from '@sentry/utils';
import {
addExceptionMechanism,
addRequestDataToTransaction,
baggageHeaderToDynamicSamplingContext,
dropUndefinedKeys,
extractPathForTransaction,
extractTraceparentData,
isString,
logger,
normalize,
tracingContextFromHeaders,
} from '@sentry/utils';
import type * as http from 'http';

Expand Down Expand Up @@ -63,11 +62,13 @@ export function tracingHandler(): (
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']);
const incomingBaggageHeaders = req.headers?.baggage;
const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(incomingBaggageHeaders);
const sentryTrace = req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined;
const baggage = req.headers?.baggage;
const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders(
sentryTrace,
baggage,
);
hub.getScope().setPropagationContext(propagationContext);

const [name, source] = extractPathForTransaction(req, { path: true, method: true });
const transaction = startTransaction(
Expand Down
28 changes: 25 additions & 3 deletions packages/node/test/handlers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Hub } from '@sentry/core';
import * as sentryCore from '@sentry/core';
import { setAsyncContextStrategy, Transaction } from '@sentry/core';
import type { Event } from '@sentry/types';
import type { Event, PropagationContext } from '@sentry/types';
import { SentryError } from '@sentry/utils';
import * as http from 'http';

Expand Down Expand Up @@ -209,6 +209,11 @@ describe('tracingHandler', () => {
jest.restoreAllMocks();
});

function getPropagationContext(): PropagationContext {
// @ts-expect-error accesing private property for test
return hub.getScope()._propagationContext;
}

it('creates a transaction when handling a request', () => {
const startTransaction = jest.spyOn(sentryCore, 'startTransaction');

Expand Down Expand Up @@ -251,6 +256,13 @@ describe('tracingHandler', () => {

const transaction = (res as any).__sentry_transaction;

expect(getPropagationContext()).toEqual({
traceId: '12312012123120121231201212312012',
parentSpanId: '1121201211212012',
spanId: expect.any(String),
sampled: false,
});

// since we have no tracesSampler defined, the default behavior (inherit if possible) applies
expect(transaction.traceId).toEqual('12312012123120121231201212312012');
expect(transaction.parentSpanId).toEqual('1121201211212012');
Expand All @@ -260,18 +272,26 @@ describe('tracingHandler', () => {

it("pulls parent's data from tracing and baggage headers on the request", () => {
req.headers = {
'sentry-trace': '12312012123120121231201212312012-1121201211212012-0',
'sentry-trace': '12312012123120121231201212312012-1121201211212012-1',
baggage: 'sentry-version=1.0,sentry-environment=production',
};

sentryTracingMiddleware(req, res, next);

expect(getPropagationContext()).toEqual({
traceId: '12312012123120121231201212312012',
parentSpanId: '1121201211212012',
spanId: expect.any(String),
sampled: true,
dsc: { version: '1.0', environment: 'production' },
});

const transaction = (res as any).__sentry_transaction;

// since we have no tracesSampler defined, the default behavior (inherit if possible) applies
expect(transaction.traceId).toEqual('12312012123120121231201212312012');
expect(transaction.parentSpanId).toEqual('1121201211212012');
expect(transaction.sampled).toEqual(false);
expect(transaction.sampled).toEqual(true);
expect(transaction.metadata?.dynamicSamplingContext).toStrictEqual({ version: '1.0', environment: 'production' });
});

Expand All @@ -283,6 +303,8 @@ describe('tracingHandler', () => {

sentryTracingMiddleware(req, res, next);

expect(getPropagationContext().dsc).toEqual({ version: '1.0', environment: 'production' });

const transaction = (res as any).__sentry_transaction;
expect(transaction.metadata?.dynamicSamplingContext).toStrictEqual({ version: '1.0', environment: 'production' });
});
Expand Down

0 comments on commit a3178f7

Please sign in to comment.