Skip to content

Commit 2288caf

Browse files
Lms24lforst
andauthored
ref(core): Remove transaction name extraction from requestDataIntegration (#11513)
Co-authored-by: Luca Forstner <[email protected]>
1 parent aecbfaf commit 2288caf

File tree

5 files changed

+12
-86
lines changed

5 files changed

+12
-86
lines changed

packages/core/src/integrations/requestdata.ts

+5-50
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import type { Client, IntegrationFn, Span } from '@sentry/types';
1+
import type { IntegrationFn } from '@sentry/types';
22
import type { AddRequestDataToEventOptions, TransactionNamingScheme } from '@sentry/utils';
3-
import { addRequestDataToEvent, extractPathForTransaction } from '@sentry/utils';
3+
import { addRequestDataToEvent } from '@sentry/utils';
44
import { defineIntegration } from '../integration';
5-
import { spanToJSON } from '../utils/spanUtils';
65

76
export type RequestDataIntegrationOptions = {
87
/**
@@ -67,12 +66,11 @@ const _requestDataIntegration = ((options: RequestDataIntegrationOptions = {}) =
6766

6867
return {
6968
name: INTEGRATION_NAME,
70-
processEvent(event, _hint, client) {
69+
processEvent(event) {
7170
// Note: In the long run, most of the logic here should probably move into the request data utility functions. For
7271
// the moment it lives here, though, until https://github.com/getsentry/sentry-javascript/issues/5718 is addressed.
73-
// (TL;DR: Those functions touch many parts of the repo in many different ways, and need to be clened up. Once
72+
// (TL;DR: Those functions touch many parts of the repo in many different ways, and need to be cleaned up. Once
7473
// that's happened, it will be easier to add this logic in without worrying about unexpected side effects.)
75-
const { transactionNamingScheme } = _options;
7674

7775
const { sdkProcessingMetadata = {} } = event;
7876
const req = sdkProcessingMetadata.request;
@@ -83,38 +81,7 @@ const _requestDataIntegration = ((options: RequestDataIntegrationOptions = {}) =
8381

8482
const addRequestDataOptions = convertReqDataIntegrationOptsToAddReqDataOpts(_options);
8583

86-
const processedEvent = addRequestDataToEvent(event, req, addRequestDataOptions);
87-
88-
// Transaction events already have the right `transaction` value
89-
if (event.type === 'transaction' || transactionNamingScheme === 'handler') {
90-
return processedEvent;
91-
}
92-
93-
// In all other cases, use the request's associated transaction (if any) to overwrite the event's `transaction`
94-
// value with a high-quality one
95-
const reqWithTransaction = req as { _sentryTransaction?: Span };
96-
const transaction = reqWithTransaction._sentryTransaction;
97-
if (transaction) {
98-
const name = spanToJSON(transaction).description || '';
99-
100-
// TODO (v8): Remove the nextjs check and just base it on `transactionNamingScheme` for all SDKs. (We have to
101-
// keep it the way it is for the moment, because changing the names of transactions in Sentry has the potential
102-
// to break things like alert rules.)
103-
const shouldIncludeMethodInTransactionName =
104-
getSDKName(client) === 'sentry.javascript.nextjs'
105-
? name.startsWith('/api')
106-
: transactionNamingScheme !== 'path';
107-
108-
const [transactionValue] = extractPathForTransaction(req, {
109-
path: true,
110-
method: shouldIncludeMethodInTransactionName,
111-
customRoute: name,
112-
});
113-
114-
processedEvent.transaction = transactionValue;
115-
}
116-
117-
return processedEvent;
84+
return addRequestDataToEvent(event, req, addRequestDataOptions);
11885
},
11986
};
12087
}) satisfies IntegrationFn;
@@ -166,15 +133,3 @@ function convertReqDataIntegrationOptsToAddReqDataOpts(
166133
},
167134
};
168135
}
169-
170-
function getSDKName(client: Client): string | undefined {
171-
try {
172-
// For a long chain like this, it's fewer bytes to combine a try-catch with assuming everything is there than to
173-
// write out a long chain of `a && a.b && a.b.c && ...`
174-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
175-
return client.getOptions()._metadata!.sdk!.name;
176-
} catch (err) {
177-
// In theory we should never get here
178-
return undefined;
179-
}
180-
}

packages/nextjs/src/common/wrapApiHandlerWithSentry.ts

+4-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
startSpanManual,
77
withIsolationScope,
88
} from '@sentry/core';
9-
import { consoleSandbox, isString, logger, objectify, stripUrlQueryAndFragment } from '@sentry/utils';
9+
import { consoleSandbox, isString, logger, objectify } from '@sentry/utils';
1010

1111
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
1212
import type { AugmentedNextApiRequest, AugmentedNextApiResponse, NextApiHandler } from './types';
@@ -20,7 +20,7 @@ import { escapeNextjsTracing } from './utils/tracingUtils';
2020
*
2121
* @param apiHandler The handler exported from the user's API page route file, which may or may not already be
2222
* wrapped with `withSentry`
23-
* @param parameterizedRoute The page's route, passed in via the proxy loader
23+
* @param parameterizedRoute The page's parameterized route.
2424
* @returns The wrapped handler
2525
*/
2626
export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameterizedRoute: string): NextApiHandler {
@@ -62,30 +62,14 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
6262
baggage: req.headers?.baggage,
6363
},
6464
() => {
65-
// prefer the parameterized route, if we have it (which we will if we've auto-wrapped the route handler)
66-
let reqPath = parameterizedRoute;
67-
68-
// If not, fake it by just replacing parameter values with their names, hoping that none of them match either
69-
// each other or any hard-coded parts of the path
70-
if (!reqPath) {
71-
const url = `${req.url}`;
72-
// pull off query string, if any
73-
reqPath = stripUrlQueryAndFragment(url);
74-
// Replace with placeholder
75-
if (req.query) {
76-
for (const [key, value] of Object.entries(req.query)) {
77-
reqPath = reqPath.replace(`${value}`, `[${key}]`);
78-
}
79-
}
80-
}
81-
8265
const reqMethod = `${(req.method || 'GET').toUpperCase()} `;
8366

8467
isolationScope.setSDKProcessingMetadata({ request: req });
68+
isolationScope.setTransactionName(`${reqMethod}${parameterizedRoute}`);
8569

8670
return startSpanManual(
8771
{
88-
name: `${reqMethod}${reqPath}`,
72+
name: `${reqMethod}${parameterizedRoute}`,
8973
op: 'http.server',
9074
forceTransaction: true,
9175
attributes: {

packages/node/test/integration/scope.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ describe('Integration | Scope', () => {
8686
tag3: 'val3',
8787
tag4: 'val4',
8888
},
89-
...(enableTracing ? { transaction: 'outer' } : {}),
9089
}),
9190
{
9291
event_id: expect.any(String),
@@ -125,7 +124,6 @@ describe('Integration | Scope', () => {
125124
tag4: 'val4',
126125
},
127126
timestamp: expect.any(Number),
128-
transaction: 'outer',
129127
transaction_info: { source: 'custom' },
130128
type: 'transaction',
131129
}),
@@ -209,7 +207,6 @@ describe('Integration | Scope', () => {
209207
tag3: 'val3a',
210208
tag4: 'val4a',
211209
},
212-
...(enableTracing ? { transaction: 'outer' } : {}),
213210
}),
214211
{
215212
event_id: expect.any(String),
@@ -236,7 +233,6 @@ describe('Integration | Scope', () => {
236233
tag3: 'val3b',
237234
tag4: 'val4b',
238235
},
239-
...(enableTracing ? { transaction: 'outer' } : {}),
240236
}),
241237
{
242238
event_id: expect.any(String),

packages/opentelemetry/src/setupEventContextTrace.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { getRootSpan } from '@sentry/core';
21
import type { Client } from '@sentry/types';
32
import { dropUndefinedKeys } from '@sentry/utils';
43
import { getDynamicSamplingContextFromSpan } from './utils/dynamicSamplingContext';
54

5+
import { getRootSpan } from '@sentry/core';
66
import { getActiveSpan } from './utils/getActiveSpan';
7-
import { spanHasName, spanHasParentId } from './utils/spanTypes';
7+
import { spanHasParentId } from './utils/spanTypes';
88

99
/** Ensure the `trace` context is set on all events. */
1010
export function setupEventContextTrace(client: Client): void {
@@ -35,9 +35,6 @@ export function setupEventContextTrace(client: Client): void {
3535
...event.sdkProcessingMetadata,
3636
};
3737

38-
const transactionName = spanHasName(rootSpan) ? rootSpan.name : undefined;
39-
if (transactionName && !event.transaction) {
40-
event.transaction = transactionName;
41-
}
38+
return event;
4239
});
4340
}

packages/opentelemetry/test/integration/scope.test.ts

-6
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ describe('Integration | Scope', () => {
9393
tag3: 'val3',
9494
tag4: 'val4',
9595
},
96-
...(enableTracing ? { transaction: 'outer' } : undefined),
9796
}),
9897
{
9998
event_id: expect.any(String),
@@ -132,7 +131,6 @@ describe('Integration | Scope', () => {
132131
tag4: 'val4',
133132
},
134133
timestamp: expect.any(Number),
135-
transaction: 'outer',
136134
transaction_info: { source: 'custom' },
137135
type: 'transaction',
138136
}),
@@ -226,7 +224,6 @@ describe('Integration | Scope', () => {
226224
tag3: 'val3a',
227225
tag4: 'val4a',
228226
},
229-
...(enableTracing ? { transaction: 'outer' } : undefined),
230227
}),
231228
{
232229
event_id: expect.any(String),
@@ -253,7 +250,6 @@ describe('Integration | Scope', () => {
253250
tag3: 'val3b',
254251
tag4: 'val4b',
255252
},
256-
...(enableTracing ? { transaction: 'outer' } : undefined),
257253
}),
258254
{
259255
event_id: expect.any(String),
@@ -358,7 +354,6 @@ describe('Integration | Scope', () => {
358354
isolationTag1: 'val1',
359355
isolationTag2: 'val2',
360356
},
361-
...(enableTracing ? { transaction: 'outer' } : undefined),
362357
}),
363358
{
364359
event_id: expect.any(String),
@@ -385,7 +380,6 @@ describe('Integration | Scope', () => {
385380
isolationTag1: 'val1',
386381
isolationTag2: 'val2b',
387382
},
388-
...(enableTracing ? { transaction: 'outer' } : undefined),
389383
}),
390384
{
391385
event_id: expect.any(String),

0 commit comments

Comments
 (0)