@@ -54,6 +54,15 @@ const MOCK_LOAD_ARGS: any = {
54
54
id : '/users/[id]' ,
55
55
} ,
56
56
url : new URL ( 'http://localhost:3000/users/123' ) ,
57
+ } ;
58
+
59
+ const MOCK_LOAD_NO_ROUTE_ARGS : any = {
60
+ params : { id : '123' } ,
61
+ url : new URL ( 'http://localhost:3000/users/123' ) ,
62
+ } ;
63
+
64
+ const MOCK_SERVER_ONLY_LOAD_ARGS : any = {
65
+ ...MOCK_LOAD_ARGS ,
57
66
request : {
58
67
headers : {
59
68
get : ( key : string ) => {
@@ -75,6 +84,32 @@ const MOCK_LOAD_ARGS: any = {
75
84
} ,
76
85
} ;
77
86
87
+ const MOCK_SERVER_ONLY_NO_TRACE_LOAD_ARGS : any = {
88
+ ...MOCK_LOAD_ARGS ,
89
+ request : {
90
+ headers : {
91
+ get : ( _ : string ) => {
92
+ return null ;
93
+ } ,
94
+ } ,
95
+ } ,
96
+ } ;
97
+
98
+ const MOCK_SERVER_ONLY_NO_BAGGAGE_LOAD_ARGS : any = {
99
+ ...MOCK_LOAD_ARGS ,
100
+ request : {
101
+ headers : {
102
+ get : ( key : string ) => {
103
+ if ( key === 'sentry-trace' ) {
104
+ return '1234567890abcdef1234567890abcdef-1234567890abcdef-1' ;
105
+ }
106
+
107
+ return null ;
108
+ } ,
109
+ } ,
110
+ } ,
111
+ } ;
112
+
78
113
beforeAll ( ( ) => {
79
114
addTracingExtensions ( ) ;
80
115
} ) ;
@@ -101,44 +136,6 @@ describe('wrapLoadWithSentry', () => {
101
136
expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
102
137
} ) ;
103
138
104
- // TODO: enable this once we figured out how tracing the load function doesn't result in creating a new transaction
105
- it . skip ( 'calls trace function' , async ( ) => {
106
- async function load ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
107
- return {
108
- post : params . id ,
109
- } ;
110
- }
111
-
112
- const wrappedLoad = wrapLoadWithSentry ( load ) ;
113
- await wrappedLoad ( MOCK_LOAD_ARGS ) ;
114
-
115
- expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
116
- expect ( mockTrace ) . toHaveBeenCalledWith (
117
- {
118
- op : 'function.sveltekit.load' ,
119
- name : '/users/[id]' ,
120
- parentSampled : true ,
121
- parentSpanId : '1234567890abcdef' ,
122
- status : 'ok' ,
123
- traceId : '1234567890abcdef1234567890abcdef' ,
124
- metadata : {
125
- dynamicSamplingContext : {
126
- environment : 'production' ,
127
- public_key : 'dogsarebadatkeepingsecrets' ,
128
- release : '1.0.0' ,
129
- sample_rate : '1' ,
130
- trace_id : '1234567890abcdef1234567890abcdef' ,
131
- transaction : 'dogpark' ,
132
- user_segment : 'segmentA' ,
133
- } ,
134
- source : 'route' ,
135
- } ,
136
- } ,
137
- expect . any ( Function ) ,
138
- expect . any ( Function ) ,
139
- ) ;
140
- } ) ;
141
-
142
139
describe ( 'with error() helper' , ( ) => {
143
140
it . each ( [
144
141
// [statusCode, timesCalled]
@@ -189,4 +186,125 @@ describe('wrapLoadWithSentry', () => {
189
186
{ handled : false , type : 'sveltekit' , data : { function : 'load' } } ,
190
187
) ;
191
188
} ) ;
189
+
190
+ describe ( 'calls trace' , ( ) => {
191
+ async function load ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
192
+ return {
193
+ post : params . id ,
194
+ } ;
195
+ }
196
+
197
+ describe ( 'for server-only load' , ( ) => {
198
+ it ( 'attaches trace data if available' , async ( ) => {
199
+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
200
+ await wrappedLoad ( MOCK_SERVER_ONLY_LOAD_ARGS ) ;
201
+
202
+ expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
203
+ expect ( mockTrace ) . toHaveBeenCalledWith (
204
+ {
205
+ op : 'function.sveltekit.load' ,
206
+ name : '/users/[id]' ,
207
+ parentSampled : true ,
208
+ parentSpanId : '1234567890abcdef' ,
209
+ status : 'ok' ,
210
+ traceId : '1234567890abcdef1234567890abcdef' ,
211
+ metadata : {
212
+ dynamicSamplingContext : {
213
+ environment : 'production' ,
214
+ public_key : 'dogsarebadatkeepingsecrets' ,
215
+ release : '1.0.0' ,
216
+ sample_rate : '1' ,
217
+ trace_id : '1234567890abcdef1234567890abcdef' ,
218
+ transaction : 'dogpark' ,
219
+ user_segment : 'segmentA' ,
220
+ } ,
221
+ source : 'route' ,
222
+ } ,
223
+ } ,
224
+ expect . any ( Function ) ,
225
+ expect . any ( Function ) ,
226
+ ) ;
227
+ } ) ;
228
+
229
+ it ( "doesn't attach trace data if it's not available" , async ( ) => {
230
+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
231
+ await wrappedLoad ( MOCK_SERVER_ONLY_NO_TRACE_LOAD_ARGS ) ;
232
+
233
+ expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
234
+ expect ( mockTrace ) . toHaveBeenCalledWith (
235
+ {
236
+ op : 'function.sveltekit.load' ,
237
+ name : '/users/[id]' ,
238
+ status : 'ok' ,
239
+ metadata : {
240
+ source : 'route' ,
241
+ } ,
242
+ } ,
243
+ expect . any ( Function ) ,
244
+ expect . any ( Function ) ,
245
+ ) ;
246
+ } ) ;
247
+
248
+ it ( "doesn't attach the DSC data if the baggage header not available" , async ( ) => {
249
+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
250
+ await wrappedLoad ( MOCK_SERVER_ONLY_NO_BAGGAGE_LOAD_ARGS ) ;
251
+
252
+ expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
253
+ expect ( mockTrace ) . toHaveBeenCalledWith (
254
+ {
255
+ op : 'function.sveltekit.load' ,
256
+ name : '/users/[id]' ,
257
+ parentSampled : true ,
258
+ parentSpanId : '1234567890abcdef' ,
259
+ status : 'ok' ,
260
+ traceId : '1234567890abcdef1234567890abcdef' ,
261
+ metadata : {
262
+ dynamicSamplingContext : { } ,
263
+ source : 'route' ,
264
+ } ,
265
+ } ,
266
+ expect . any ( Function ) ,
267
+ expect . any ( Function ) ,
268
+ ) ;
269
+ } ) ;
270
+ } ) ;
271
+
272
+ it ( 'for shared load' , async ( ) => {
273
+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
274
+ await wrappedLoad ( MOCK_LOAD_ARGS ) ;
275
+
276
+ expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
277
+ expect ( mockTrace ) . toHaveBeenCalledWith (
278
+ {
279
+ op : 'function.sveltekit.load' ,
280
+ name : '/users/[id]' ,
281
+ status : 'ok' ,
282
+ metadata : {
283
+ source : 'route' ,
284
+ } ,
285
+ } ,
286
+ expect . any ( Function ) ,
287
+ expect . any ( Function ) ,
288
+ ) ;
289
+ } ) ;
290
+
291
+ it ( 'falls back to the raw url if `event.route.id` is not available' , async ( ) => {
292
+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
293
+ await wrappedLoad ( MOCK_LOAD_NO_ROUTE_ARGS ) ;
294
+
295
+ expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
296
+ expect ( mockTrace ) . toHaveBeenCalledWith (
297
+ {
298
+ op : 'function.sveltekit.load' ,
299
+ name : '/users/123' ,
300
+ status : 'ok' ,
301
+ metadata : {
302
+ source : 'url' ,
303
+ } ,
304
+ } ,
305
+ expect . any ( Function ) ,
306
+ expect . any ( Function ) ,
307
+ ) ;
308
+ } ) ;
309
+ } ) ;
192
310
} ) ;
0 commit comments