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' ;
6- import {
7- applySdkMetadata ,
8- debug ,
9- getClient ,
10- getGlobalScope ,
11- getRootSpan ,
12- getVercelEnv ,
13- GLOBAL_OBJ ,
14- } from '@sentry/core' ;
5+ import { applySdkMetadata , debug , getClient , getGlobalScope , getVercelEnv , GLOBAL_OBJ } from '@sentry/core' ;
156import type { NodeClient , NodeOptions } from '@sentry/node' ;
167import { getDefaultIntegrations , httpIntegration , init as nodeInit } from '@sentry/node' ;
178import { DEBUG_BUILD } from '../common/debug-build' ;
189import { devErrorSymbolicationEventProcessor } from '../common/devErrorSymbolicationEventProcessor' ;
1910import { isPrerenderControlFlowError } from '../common/nextNavigationErrorUtils' ;
20- import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../common/span-attributes-with-logic-attached' ;
2111import { isBuild } from '../common/utils/isBuild' ;
2212import { isAsyncContextOwnedByCloudflare , isCloudflareWaitUntilAvailable } from '../common/utils/responseEnd' ;
23- import { setUrlProcessingMetadata } from '../common/utils/setUrlProcessingMetadata' ;
2413import { 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' ;
14+ import { addNextjsServerSpanHooks , NEXTJS_SERVER_IGNORE_SPANS } from './handleOnSpanStart' ;
3015import { prepareSafeIdGeneratorContext } from './prepareSafeIdGeneratorContext' ;
3116import { nextjsUseCacheIntegration } from './useCacheInstrumentation' ;
32- import { maybeCompleteCronCheckIn } from './vercelCronsMonitoring' ;
33- import { maybeCleanupQueueSpan } from './vercelQueuesMonitoring' ;
3417
3518export * from '@sentry/node' ;
3619
@@ -159,22 +142,7 @@ export function init(options: NodeOptions): NodeClient | undefined {
159142 ...cloudflareConfig ,
160143 } ;
161144
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 ] ;
145+ opts . ignoreSpans = [ ...( opts . ignoreSpans || [ ] ) , ...NEXTJS_SERVER_IGNORE_SPANS ] ;
178146
179147 if ( DEBUG_BUILD && opts . debug ) {
180148 debug . enable ( ) ;
@@ -195,44 +163,9 @@ export function init(options: NodeOptions): NodeClient | undefined {
195163
196164 const client = isOwnedByCloudflare ? undefined : nodeInit ( opts ) ;
197165
198- client ?. on ( 'beforeSampling' , ( { spanAttributes } , samplingDecision ) => {
199- // There are situations where the Next.js Node.js server forwards requests for the Edge Runtime server (e.g. in
200- // middleware) and this causes spans for Sentry ingest requests to be created. These are not exempt from our tracing
201- // because we didn't get the chance to do `suppressTracing`, since this happens outside of userland.
202- // We need to drop these spans.
203- if (
204- // eslint-disable-next-line typescript/no-deprecated
205- ( typeof spanAttributes [ HTTP_TARGET ] === 'string' &&
206- // eslint-disable-next-line typescript/no-deprecated
207- spanAttributes [ HTTP_TARGET ] . includes ( 'sentry_key' ) &&
208- // eslint-disable-next-line typescript/no-deprecated
209- spanAttributes [ HTTP_TARGET ] . includes ( 'sentry_client' ) ) ||
210- ( typeof spanAttributes [ URL_QUERY ] === 'string' &&
211- spanAttributes [ URL_QUERY ] . includes ( 'sentry_key' ) &&
212- spanAttributes [ URL_QUERY ] . includes ( 'sentry_client' ) )
213- ) {
214- samplingDecision . decision = false ;
215- }
216- } ) ;
217-
218- client ?. on ( 'spanStart' , span => handleOnSpanStart ( span , client ) ) ;
219-
220- // Normalize name/op/source/status on the request root span at span end, before it is serialized into
221- // a transaction event (legacy) or streamed span JSON. Running on the live span means both lifecycles
222- // pick up the changes from one place, and the cron/queue hooks below see the finalized status.
223- client ?. on ( 'spanEnd' , span => {
224- if ( span !== getRootSpan ( span ) ) {
225- return ;
226- }
227-
228- const mutableRootSpan = createLiveRootSpanAdapter ( span ) ;
229- enhanceHandleRequestRootSpan ( mutableRootSpan ) ;
230- enhanceMiddlewareRootSpan ( mutableRootSpan ) ;
231- backfillHttpServerStatus ( span ) ;
232- } ) ;
233-
234- client ?. on ( 'spanEnd' , maybeCompleteCronCheckIn ) ;
235- client ?. on ( 'spanEnd' , maybeCleanupQueueSpan ) ;
166+ if ( client ) {
167+ addNextjsServerSpanHooks ( client ) ;
168+ }
236169
237170 getGlobalScope ( ) . addEventProcessor (
238171 Object . assign (
@@ -276,10 +209,6 @@ export function init(options: NodeOptions): NodeClient | undefined {
276209 ) ,
277210 ) ;
278211
279- client ?. on ( 'preprocessEvent' , event => {
280- setUrlProcessingMetadata ( event ) ;
281- } ) ;
282-
283212 if ( process . env . NODE_ENV === 'development' ) {
284213 getGlobalScope ( ) . addEventProcessor ( devErrorSymbolicationEventProcessor ) ;
285214 }
0 commit comments