1
- import { captureException , configureScope , getCurrentHub , startTransaction } from '@sentry/node' ;
1
+ import { captureException , getCurrentHub , startTransaction } from '@sentry/node' ;
2
2
import { getActiveTransaction } from '@sentry/tracing' ;
3
3
import { addExceptionMechanism , fill , loadModule , logger , stripUrlQueryAndFragment } from '@sentry/utils' ;
4
4
@@ -69,6 +69,65 @@ interface DataFunction {
69
69
( args : DataFunctionArgs ) : Promise < Response > | Response | Promise < AppData > | AppData ;
70
70
}
71
71
72
+ function captureRemixServerException ( err : Error , name : string ) : void {
73
+ captureException ( err , scope => {
74
+ scope . addEventProcessor ( event => {
75
+ addExceptionMechanism ( event , {
76
+ type : 'instrument' ,
77
+ handled : true ,
78
+ data : {
79
+ function : name ,
80
+ } ,
81
+ } ) ;
82
+
83
+ return event ;
84
+ } ) ;
85
+
86
+ return scope ;
87
+ } ) ;
88
+ }
89
+
90
+ function makeWrappedDocumentRequestFunction (
91
+ origDocumentRequestFunction : HandleDocumentRequestFunction ,
92
+ ) : HandleDocumentRequestFunction {
93
+ return async function (
94
+ this : unknown ,
95
+ request : Request ,
96
+ responseStatusCode : number ,
97
+ responseHeaders : Headers ,
98
+ context : Record < symbol , unknown > ,
99
+ ) : Promise < Response > {
100
+ let res : Response ;
101
+
102
+ const activeTransaction = getActiveTransaction ( ) ;
103
+ const currentScope = getCurrentHub ( ) . getScope ( ) ;
104
+
105
+ if ( ! activeTransaction || ! currentScope ) {
106
+ return origDocumentRequestFunction . call ( this , request , responseStatusCode , responseHeaders , context ) ;
107
+ }
108
+
109
+ try {
110
+ const span = activeTransaction . startChild ( {
111
+ op : 'remix.server.documentRequest' ,
112
+ description : activeTransaction . name ,
113
+ tags : {
114
+ method : request . method ,
115
+ url : request . url ,
116
+ } ,
117
+ } ) ;
118
+
119
+ res = await origDocumentRequestFunction . call ( this , request , responseStatusCode , responseHeaders , context ) ;
120
+
121
+ span . finish ( ) ;
122
+ } catch ( err ) {
123
+ captureRemixServerException ( err , name ) ;
124
+ throw err ;
125
+ }
126
+
127
+ return res ;
128
+ } ;
129
+ }
130
+
72
131
function makeWrappedDataFunction ( origFn : DataFunction , name : 'action' | 'loader' ) : DataFunction {
73
132
return async function ( this : unknown , args : DataFunctionArgs ) : Promise < Response | AppData > {
74
133
let res : Response | AppData ;
@@ -98,23 +157,7 @@ function makeWrappedDataFunction(origFn: DataFunction, name: 'action' | 'loader'
98
157
currentScope . setSpan ( activeTransaction ) ;
99
158
span . finish ( ) ;
100
159
} catch ( err ) {
101
- configureScope ( scope => {
102
- scope . addEventProcessor ( event => {
103
- addExceptionMechanism ( event , {
104
- type : 'instrument' ,
105
- handled : true ,
106
- data : {
107
- function : name ,
108
- } ,
109
- } ) ;
110
-
111
- return event ;
112
- } ) ;
113
- } ) ;
114
-
115
- captureException ( err ) ;
116
-
117
- // Rethrow for other handlers
160
+ captureRemixServerException ( err , name ) ;
118
161
throw err ;
119
162
}
120
163
@@ -160,6 +203,10 @@ function makeWrappedCreateRequestHandler(
160
203
return function ( this : unknown , build : ServerBuild , mode : string | undefined ) : RequestHandler {
161
204
const routes : ServerRouteManifest = { } ;
162
205
206
+ const wrappedEntry = { ...build . entry , module : { ...build . entry . module } } ;
207
+
208
+ fill ( wrappedEntry . module , 'default' , makeWrappedDocumentRequestFunction ) ;
209
+
163
210
for ( const [ id , route ] of Object . entries ( build . routes ) ) {
164
211
const wrappedRoute = { ...route , module : { ...route . module } } ;
165
212
@@ -174,7 +221,7 @@ function makeWrappedCreateRequestHandler(
174
221
routes [ id ] = wrappedRoute ;
175
222
}
176
223
177
- const requestHandler = origCreateRequestHandler . call ( this , { ...build , routes } , mode ) ;
224
+ const requestHandler = origCreateRequestHandler . call ( this , { ...build , routes, entry : wrappedEntry } , mode ) ;
178
225
179
226
return wrapRequestHandler ( requestHandler ) ;
180
227
} ;
0 commit comments