Skip to content

ref(node): Use Sentry.continueTrace in node #9607

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 2 commits into from
Nov 21, 2023
Merged
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
44 changes: 20 additions & 24 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
captureException,
continueTrace,
flush,
getCurrentHub,
hasTracingEnabled,
Expand All @@ -19,7 +20,6 @@ import {
isThenable,
logger,
normalize,
tracingContextFromHeaders,
} from '@sentry/utils';
import type * as http from 'http';

Expand Down Expand Up @@ -57,35 +57,31 @@ export function tracingHandler(): (

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);

if (!hasTracingEnabled(options)) {
return next();
}

const [name, source] = extractPathForTransaction(req, { path: true, method: true });
const transaction = startTransaction(
{
name,
op: 'http.server',
origin: 'auto.http.node.tracingHandler',
...traceparentData,
metadata: {
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
// The request should already have been stored in `scope.sdkProcessingMetadata` (which will become
// `event.sdkProcessingMetadata` the same way the metadata here will) by `sentryRequestMiddleware`, but on the
// off chance someone is using `sentryTracingMiddleware` without `sentryRequestMiddleware`, it doesn't hurt to
// be sure
request: req,
source,
const transaction = continueTrace({ sentryTrace, baggage }, ctx =>
startTransaction(
{
name,
op: 'http.server',
origin: 'auto.http.node.tracingHandler',
...ctx,
metadata: {
...ctx.metadata,
// The request should already have been stored in `scope.sdkProcessingMetadata` (which will become
// `event.sdkProcessingMetadata` the same way the metadata here will) by `sentryRequestMiddleware`, but on the
// off chance someone is using `sentryTracingMiddleware` without `sentryRequestMiddleware`, it doesn't hurt to
// be sure
request: req,
source,
},
},
},
// extra context passed to the tracesSampler
{ request: extractRequestData(req) },
// extra context passed to the tracesSampler
{ request: extractRequestData(req) },
),
);

// We put the transaction on the scope so users can attach children to it
Expand Down