@@ -6,18 +6,23 @@ import {
66 SENTRY_SEGMENT_NAME_SOURCE ,
77} from '@sentry/conventions/attributes' ;
88import { MIDDLEWARE } from '@sentry/conventions/op' ;
9- import type { Client } from '@sentry/core' ;
9+ import type { Client , SpanAttributes } from '@sentry/core' ;
1010import {
1111 _INTERNAL_setIncomingRequestForScope ,
1212 addChildSpanToSpan ,
1313 GLOBAL_OBJ ,
1414 SentrySpan ,
15+ shouldIgnoreSpan ,
1516 spanToStaticSpanJSON ,
1617 withIsolationScope ,
1718} from '@sentry/core' ;
1819import { afterEach , describe , expect , it } from 'vitest' ;
1920import { ATTR_NEXT_SPAN_NAME , ATTR_NEXT_SPAN_TYPE } from '../../src/common/nextSpanAttributes' ;
20- import { addNextjsServerSpanHooks , handleOnSpanStart } from '../../src/server/serverSpanHooks' ;
21+ import {
22+ addNextjsServerSpanHooks ,
23+ handleOnSpanStart ,
24+ NEXTJS_SERVER_IGNORE_SPANS ,
25+ } from '../../src/server/serverSpanHooks' ;
2126
2227const client = { getOptions : ( ) => ( { } ) } as unknown as Client ;
2328
@@ -179,51 +184,51 @@ describe('handleOnSpanStart', () => {
179184 } ) ;
180185} ) ;
181186
182- describe ( 'addNextjsServerSpanHooks beforeSampling ' , ( ) => {
187+ describe ( 'addNextjsServerSpanHooks beforeSpanStart ' , ( ) => {
183188 const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & { _sentryNextJsVersion ?: string } ;
184189
185190 afterEach ( ( ) => {
186191 delete globalWithInjectedValues . _sentryNextJsVersion ;
187192 } ) ;
188193
189- function getSamplingDecision ( nextVersion : string | undefined , middlewareInvoke : boolean | undefined ) : boolean {
194+ function isRequestSpanIgnored ( nextVersion : string | undefined , middlewareInvoke : boolean | undefined ) : boolean {
190195 globalWithInjectedValues . _sentryNextJsVersion = nextVersion ;
191196
192- let beforeSampling : ( ( data : unknown , decision : { decision : boolean } ) => void ) | undefined ;
197+ let beforeSpanStart : ( ( data : { name : string | undefined ; attributes : SpanAttributes } ) => void ) | undefined ;
193198 addNextjsServerSpanHooks ( {
194- on : ( hook : string , callback : typeof beforeSampling ) => {
195- if ( hook === 'beforeSampling ' ) {
196- beforeSampling = callback ;
199+ on : ( hook : string , callback : typeof beforeSpanStart ) => {
200+ if ( hook === 'beforeSpanStart ' ) {
201+ beforeSpanStart = callback ;
197202 }
198203 } ,
199204 getOptions : ( ) => ( { } ) ,
200205 } as unknown as Client ) ;
201206
202207 const request = { [ Symbol . for ( 'NextInternalRequestMeta' ) ] : { middlewareInvoke } } ;
203- const samplingDecision = { decision : true } ;
208+ const spanStartData = { name : 'GET' , attributes : { [ ATTR_NEXT_SPAN_TYPE ] : 'BaseServer.handleRequest' } } ;
204209 withIsolationScope ( isolationScope => {
205210 _INTERNAL_setIncomingRequestForScope ( isolationScope , request ) ;
206- beforeSampling ?.(
207- { spanAttributes : { [ ATTR_NEXT_SPAN_TYPE ] : 'BaseServer.handleRequest' } , spanName : 'GET' } ,
208- samplingDecision ,
209- ) ;
211+ beforeSpanStart ?.( spanStartData ) ;
210212 } ) ;
211- return samplingDecision . decision ;
213+ return shouldIgnoreSpan (
214+ { description : spanStartData . name , attributes : spanStartData . attributes } ,
215+ NEXTJS_SERVER_IGNORE_SPANS ,
216+ ) ;
212217 }
213218
214- it ( 'drops the request span that Next.js only starts to run the proxy' , ( ) => {
215- expect ( getSamplingDecision ( '16.3.0' , true ) ) . toBe ( false ) ;
219+ it ( 'ignores the request span that Next.js only starts to run the proxy' , ( ) => {
220+ expect ( isRequestSpanIgnored ( '16.3.0' , true ) ) . toBe ( true ) ;
216221 } ) ;
217222
218223 it ( 'keeps the request span that renders the route' , ( ) => {
219- expect ( getSamplingDecision ( '16.3.0' , false ) ) . toBe ( true ) ;
224+ expect ( isRequestSpanIgnored ( '16.3.0' , false ) ) . toBe ( false ) ;
220225 } ) ;
221226
222227 it ( 'keeps the request span that runs the proxy before Next.js 16.3, since the proxy span is its child there' , ( ) => {
223- expect ( getSamplingDecision ( '16.2.4' , true ) ) . toBe ( true ) ;
228+ expect ( isRequestSpanIgnored ( '16.2.4' , true ) ) . toBe ( false ) ;
224229 } ) ;
225230
226231 it ( 'keeps the request span that runs the proxy if the Next.js version is unknown' , ( ) => {
227- expect ( getSamplingDecision ( undefined , true ) ) . toBe ( true ) ;
232+ expect ( isRequestSpanIgnored ( undefined , true ) ) . toBe ( false ) ;
228233 } ) ;
229234} ) ;
0 commit comments