11// import/export got a false positive, and affects most of our index barrel files
22// can be removed once following issue is fixed: https://github.com/import-js/eslint-plugin-import/issues/703
33/* eslint-disable import/export */
4- import { HTTP_TARGET , URL_QUERY } from '@sentry/conventions/attributes' ;
54import type { EventProcessor } from '@sentry/core' ;
65import {
76 _INTERNAL_getActiveClient ,
87 applySdkMetadata ,
98 debug ,
109 getGlobalScope ,
11- getRootSpan ,
1210 getVercelEnv ,
1311 GLOBAL_OBJ ,
1412} from '@sentry/core' ;
@@ -17,20 +15,12 @@ import { getDefaultIntegrations, httpIntegration, init as nodeInit } from '@sent
1715import { DEBUG_BUILD } from '../common/debug-build' ;
1816import { devErrorSymbolicationEventProcessor } from '../common/devErrorSymbolicationEventProcessor' ;
1917import { isPrerenderControlFlowError } from '../common/nextNavigationErrorUtils' ;
20- import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../common/span-attributes-with-logic-attached' ;
2118import { isBuild } from '../common/utils/isBuild' ;
2219import { isAsyncContextOwnedByCloudflare , isCloudflareWaitUntilAvailable } from '../common/utils/responseEnd' ;
23- import { setUrlProcessingMetadata } from '../common/utils/setUrlProcessingMetadata' ;
2420import { distDirRewriteFramesIntegration } from './distDirRewriteFramesIntegration' ;
25- import { enhanceMiddlewareRootSpan } from '../common/enhanceMiddlewareRootSpan' ;
26- import { backfillHttpServerStatus } from '../common/utils/backfillHttpServerStatus' ;
27- import { createLiveRootSpanAdapter } from '../common/utils/liveRootSpanAdapter' ;
28- import { enhanceHandleRequestRootSpan } from './enhanceHandleRequestRootSpan' ;
29- import { handleOnSpanStart } from './handleOnSpanStart' ;
21+ import { addNextjsServerSpanHooks , NEXTJS_SERVER_IGNORE_SPANS } from './handleOnSpanStart' ;
3022import { prepareSafeIdGeneratorContext } from './prepareSafeIdGeneratorContext' ;
3123import { nextjsUseCacheIntegration } from './useCacheInstrumentation' ;
32- import { maybeCompleteCronCheckIn } from './vercelCronsMonitoring' ;
33- import { maybeCleanupQueueSpan } from './vercelQueuesMonitoring' ;
3424
3525export * from '@sentry/node' ;
3626
@@ -159,22 +149,7 @@ export function init(options: NodeOptions): NodeClient | undefined {
159149 ...cloudflareConfig ,
160150 } ;
161151
162- const nextjsIgnoreSpans : NonNullable < NodeOptions [ 'ignoreSpans' ] > = [
163- // Static assets (matches `_next/static` anywhere in the name to handle custom basePath)
164- / ^ G E T ( \/ .* ) ? \/ _ n e x t \/ s t a t i c \/ / ,
165- // Dev source-map fetch endpoints
166- / \/ _ _ n e x t j s _ o r i g i n a l - s t a c k - f r a m e / ,
167- // Pages router /404
168- / ^ \/ 4 0 4 $ / ,
169- // App router /404 and /_not-found segments (any HTTP method)
170- / ^ ( G E T | H E A D | P O S T | P U T | D E L E T E | C O N N E C T | O P T I O N S | T R A C E | P A T C H ) \/ ( 4 0 4 | _ n o t - f o u n d ) $ / ,
171- // Root transactions named "NextServer.getRequestHandler" containing useless tracing
172- / ^ N e x t S e r v e r \. g e t R e q u e s t H a n d l e r $ / ,
173- // Spans flagged via TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION
174- // (set in `dropMiddlewareTunnelRequests` during `spanStart`)
175- { attributes : { [ TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION ] : true } } ,
176- ] ;
177- opts . ignoreSpans = [ ...( opts . ignoreSpans || [ ] ) , ...nextjsIgnoreSpans ] ;
152+ opts . ignoreSpans = [ ...( opts . ignoreSpans || [ ] ) , ...NEXTJS_SERVER_IGNORE_SPANS ] ;
178153
179154 if ( DEBUG_BUILD && opts . debug ) {
180155 debug . enable ( ) ;
@@ -196,44 +171,9 @@ export function init(options: NodeOptions): NodeClient | undefined {
196171
197172 const client = isOwnedByCloudflare ? undefined : nodeInit ( opts ) ;
198173
199- client ?. on ( 'beforeSampling' , ( { spanAttributes } , samplingDecision ) => {
200- // There are situations where the Next.js Node.js server forwards requests for the Edge Runtime server (e.g. in
201- // middleware) and this causes spans for Sentry ingest requests to be created. These are not exempt from our tracing
202- // because we didn't get the chance to do `suppressTracing`, since this happens outside of userland.
203- // We need to drop these spans.
204- if (
205- // eslint-disable-next-line typescript/no-deprecated
206- ( typeof spanAttributes [ HTTP_TARGET ] === 'string' &&
207- // eslint-disable-next-line typescript/no-deprecated
208- spanAttributes [ HTTP_TARGET ] . includes ( 'sentry_key' ) &&
209- // eslint-disable-next-line typescript/no-deprecated
210- spanAttributes [ HTTP_TARGET ] . includes ( 'sentry_client' ) ) ||
211- ( typeof spanAttributes [ URL_QUERY ] === 'string' &&
212- spanAttributes [ URL_QUERY ] . includes ( 'sentry_key' ) &&
213- spanAttributes [ URL_QUERY ] . includes ( 'sentry_client' ) )
214- ) {
215- samplingDecision . decision = false ;
216- }
217- } ) ;
218-
219- client ?. on ( 'spanStart' , span => handleOnSpanStart ( span , client ) ) ;
220-
221- // Normalize name/op/source/status on the request root span at span end, before it is serialized into
222- // a transaction event (legacy) or streamed span JSON. Running on the live span means both lifecycles
223- // pick up the changes from one place, and the cron/queue hooks below see the finalized status.
224- client ?. on ( 'spanEnd' , span => {
225- if ( span !== getRootSpan ( span ) ) {
226- return ;
227- }
228-
229- const mutableRootSpan = createLiveRootSpanAdapter ( span ) ;
230- enhanceHandleRequestRootSpan ( mutableRootSpan ) ;
231- enhanceMiddlewareRootSpan ( mutableRootSpan ) ;
232- backfillHttpServerStatus ( span ) ;
233- } ) ;
234-
235- client ?. on ( 'spanEnd' , maybeCompleteCronCheckIn ) ;
236- client ?. on ( 'spanEnd' , maybeCleanupQueueSpan ) ;
174+ if ( client ) {
175+ addNextjsServerSpanHooks ( client ) ;
176+ }
237177
238178 // On the client, not the global scope, so a later `init()` after
239179 // `close()` does not stack another copy. In a request of `withSentry` on
@@ -282,10 +222,6 @@ export function init(options: NodeOptions): NodeClient | undefined {
282222 ) ,
283223 ) ;
284224
285- client ?. on ( 'preprocessEvent' , event => {
286- setUrlProcessingMetadata ( event ) ;
287- } ) ;
288-
289225 if ( process . env . NODE_ENV === 'development' ) {
290226 eventProcessorTarget ?. addEventProcessor ( devErrorSymbolicationEventProcessor ) ;
291227 }
0 commit comments