@@ -35,8 +35,8 @@ export function getSpanFromRequest(req: IncomingMessage): Span | undefined {
3535 return req . _sentrySpan ;
3636}
3737
38- function setSpanOnRequest ( transaction : Span , req : IncomingMessage ) : void {
39- req . _sentrySpan = transaction ;
38+ function setSpanOnRequest ( span : Span , req : IncomingMessage ) : void {
39+ req . _sentrySpan = span ;
4040}
4141
4242/**
@@ -99,25 +99,9 @@ export function withTracedServerSideDataFetcher<F extends (...args: any[]) => Pr
9999 const baggage = req . headers ?. baggage ;
100100
101101 return continueTrace ( { sentryTrace, baggage } , ( ) => {
102- let requestSpan : Span | undefined = getSpanFromRequest ( req ) ;
103- if ( ! requestSpan ) {
104- // TODO(v8): Simplify these checks when startInactiveSpan always returns a span
105- requestSpan = startInactiveSpan ( {
106- name : options . requestedRouteName ,
107- op : 'http.server' ,
108- attributes : {
109- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.function.nextjs' ,
110- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
111- } ,
112- } ) ;
113- if ( requestSpan ) {
114- requestSpan . setStatus ( { code : SPAN_STATUS_OK } ) ;
115- setSpanOnRequest ( requestSpan , req ) ;
116- autoEndSpanOnResponseEnd ( requestSpan , res ) ;
117- }
118- }
102+ const requestSpan = getOrStartRequestSpan ( req , res , options . requestedRouteName ) ;
119103
120- const withActiveSpanCallback = ( ) : Promise < ReturnType < F > > => {
104+ return withActiveSpan ( requestSpan , ( ) => {
121105 return startSpanManual (
122106 {
123107 op : 'function.nextjs' ,
@@ -143,18 +127,34 @@ export function withTracedServerSideDataFetcher<F extends (...args: any[]) => Pr
143127 }
144128 } ,
145129 ) ;
146- } ;
147-
148- if ( requestSpan ) {
149- return withActiveSpan ( requestSpan , withActiveSpanCallback ) ;
150- } else {
151- return withActiveSpanCallback ( ) ;
152- }
130+ } ) ;
153131 } ) ;
154132 } ) ;
155133 } ;
156134}
157135
136+ function getOrStartRequestSpan ( req : IncomingMessage , res : ServerResponse , name : string ) : Span {
137+ const existingSpan = getSpanFromRequest ( req ) ;
138+ if ( existingSpan ) {
139+ return existingSpan ;
140+ }
141+
142+ const requestSpan = startInactiveSpan ( {
143+ name,
144+ op : 'http.server' ,
145+ attributes : {
146+ [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.function.nextjs' ,
147+ [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
148+ } ,
149+ } ) ;
150+
151+ requestSpan . setStatus ( { code : SPAN_STATUS_OK } ) ;
152+ setSpanOnRequest ( requestSpan , req ) ;
153+ autoEndSpanOnResponseEnd ( requestSpan , res ) ;
154+
155+ return requestSpan ;
156+ }
157+
158158/**
159159 * Call a data fetcher and trace it. Only traces the function if there is an active transaction on the scope.
160160 *
0 commit comments