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
26 changes: 19 additions & 7 deletions packages/nextjs/src/utils/instrumentServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { deepReadDirSync } from '@sentry/node';
import { getActiveTransaction, hasTracingEnabled } from '@sentry/tracing';
import { extractTraceparentData, getActiveTransaction, hasTracingEnabled } from '@sentry/tracing';
import { Event as SentryEvent } from '@sentry/types';
import { fill, logger } from '@sentry/utils';
import { fill, isString, logger } from '@sentry/utils';
import * as domain from 'domain';
import * as http from 'http';
import { default as createNextServer } from 'next';
Expand Down Expand Up @@ -200,18 +200,30 @@ function makeWrappedReqHandler(origReqHandler: ReqHandler): WrappedReqHandler {

// We only want to record page and API requests
if (hasTracingEnabled() && shouldTraceRequest(req.url, publicDirFiles)) {
// If there is a trace header set, extract the data from it (parentSpanId, traceId, and sampling decision)
let traceparentData;
if (req.headers && isString(req.headers['sentry-trace'])) {
traceparentData = extractTraceparentData(req.headers['sentry-trace'] as string);
logger.log(`[Tracing] Continuing trace ${traceparentData?.traceId}.`);
}

// pull off query string, if any
const reqPath = req.url.split('?')[0];

// requests for pages will only ever be GET requests, so don't bother to include the method in the transaction
// name; requests to API routes could be GET, POST, PUT, etc, so do include it there
const namePrefix = req.url.startsWith('/api') ? `${(req.method || 'GET').toUpperCase()} ` : '';

const transaction = Sentry.startTransaction({
name: `${namePrefix}${reqPath}`,
op: 'http.server',
metadata: { requestPath: req.url.split('?')[0] },
});
const transaction = Sentry.startTransaction(
{
name: `${namePrefix}${reqPath}`,
op: 'http.server',
metadata: { requestPath: req.url.split('?')[0] },
...traceparentData,
},
// extra context passed to the `tracesSampler`
{ request: req },
);

currentScope.setSpan(transaction);

Expand Down
1 change: 1 addition & 0 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function tracingHandler(): (
op: 'http.server',
...traceparentData,
},
// extra context passed to the tracesSampler
{ request: extractRequestData(req) },
);

Expand Down