@@ -54,6 +54,15 @@ const MOCK_LOAD_ARGS: any = {
5454 id : '/users/[id]' ,
5555 } ,
5656 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 ,
5766 request : {
5867 headers : {
5968 get : ( key : string ) => {
@@ -75,6 +84,32 @@ const MOCK_LOAD_ARGS: any = {
7584 } ,
7685} ;
7786
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+
78113beforeAll ( ( ) => {
79114 addTracingExtensions ( ) ;
80115} ) ;
@@ -101,41 +136,125 @@ describe('wrapLoadWithSentry', () => {
101136 expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
102137 } ) ;
103138
104- it ( 'calls trace function' , async ( ) => {
139+ describe ( 'calls trace' , ( ) => {
105140 async function load ( { params } : Parameters < ServerLoad > [ 0 ] ) : Promise < ReturnType < ServerLoad > > {
106141 return {
107142 post : params . id ,
108143 } ;
109144 }
110145
111- const wrappedLoad = wrapLoadWithSentry ( load ) ;
112- await wrappedLoad ( MOCK_LOAD_ARGS ) ;
113-
114- expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
115- expect ( mockTrace ) . toHaveBeenCalledWith (
116- {
117- op : 'function.sveltekit.load' ,
118- name : '/users/[id]' ,
119- parentSampled : true ,
120- parentSpanId : '1234567890abcdef' ,
121- status : 'ok' ,
122- traceId : '1234567890abcdef1234567890abcdef' ,
123- metadata : {
124- dynamicSamplingContext : {
125- environment : 'production' ,
126- public_key : 'dogsarebadatkeepingsecrets' ,
127- release : '1.0.0' ,
128- sample_rate : '1' ,
129- trace_id : '1234567890abcdef1234567890abcdef' ,
130- transaction : 'dogpark' ,
131- user_segment : 'segmentA' ,
146+ describe ( 'for server-only load' , ( ) => {
147+ it ( 'attaches trace data if available' , async ( ) => {
148+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
149+ await wrappedLoad ( MOCK_SERVER_ONLY_LOAD_ARGS ) ;
150+
151+ expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
152+ expect ( mockTrace ) . toHaveBeenCalledWith (
153+ {
154+ op : 'function.sveltekit.load' ,
155+ name : '/users/[id]' ,
156+ parentSampled : true ,
157+ parentSpanId : '1234567890abcdef' ,
158+ status : 'ok' ,
159+ traceId : '1234567890abcdef1234567890abcdef' ,
160+ metadata : {
161+ dynamicSamplingContext : {
162+ environment : 'production' ,
163+ public_key : 'dogsarebadatkeepingsecrets' ,
164+ release : '1.0.0' ,
165+ sample_rate : '1' ,
166+ trace_id : '1234567890abcdef1234567890abcdef' ,
167+ transaction : 'dogpark' ,
168+ user_segment : 'segmentA' ,
169+ } ,
170+ source : 'route' ,
171+ } ,
172+ } ,
173+ expect . any ( Function ) ,
174+ expect . any ( Function ) ,
175+ ) ;
176+ } ) ;
177+
178+ it ( "doesn't attach trace data if it's not available" , async ( ) => {
179+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
180+ await wrappedLoad ( MOCK_SERVER_ONLY_NO_TRACE_LOAD_ARGS ) ;
181+
182+ expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
183+ expect ( mockTrace ) . toHaveBeenCalledWith (
184+ {
185+ op : 'function.sveltekit.load' ,
186+ name : '/users/[id]' ,
187+ status : 'ok' ,
188+ metadata : {
189+ source : 'route' ,
190+ } ,
191+ } ,
192+ expect . any ( Function ) ,
193+ expect . any ( Function ) ,
194+ ) ;
195+ } ) ;
196+
197+ it ( "doesn't attach the DSC data if the baggage header not available" , async ( ) => {
198+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
199+ await wrappedLoad ( MOCK_SERVER_ONLY_NO_BAGGAGE_LOAD_ARGS ) ;
200+
201+ expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
202+ expect ( mockTrace ) . toHaveBeenCalledWith (
203+ {
204+ op : 'function.sveltekit.load' ,
205+ name : '/users/[id]' ,
206+ parentSampled : true ,
207+ parentSpanId : '1234567890abcdef' ,
208+ status : 'ok' ,
209+ traceId : '1234567890abcdef1234567890abcdef' ,
210+ metadata : {
211+ dynamicSamplingContext : { } ,
212+ source : 'route' ,
213+ } ,
214+ } ,
215+ expect . any ( Function ) ,
216+ expect . any ( Function ) ,
217+ ) ;
218+ } ) ;
219+ } ) ;
220+
221+ it ( 'for shared load' , async ( ) => {
222+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
223+ await wrappedLoad ( MOCK_LOAD_ARGS ) ;
224+
225+ expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
226+ expect ( mockTrace ) . toHaveBeenCalledWith (
227+ {
228+ op : 'function.sveltekit.load' ,
229+ name : '/users/[id]' ,
230+ status : 'ok' ,
231+ metadata : {
232+ source : 'route' ,
132233 } ,
133- source : 'route' ,
134234 } ,
135- } ,
136- expect . any ( Function ) ,
137- expect . any ( Function ) ,
138- ) ;
235+ expect . any ( Function ) ,
236+ expect . any ( Function ) ,
237+ ) ;
238+ } ) ;
239+
240+ it ( 'falls back to the raw url if `event.route.id` is not available' , async ( ) => {
241+ const wrappedLoad = wrapLoadWithSentry ( load ) ;
242+ await wrappedLoad ( MOCK_LOAD_NO_ROUTE_ARGS ) ;
243+
244+ expect ( mockTrace ) . toHaveBeenCalledTimes ( 1 ) ;
245+ expect ( mockTrace ) . toHaveBeenCalledWith (
246+ {
247+ op : 'function.sveltekit.load' ,
248+ name : '/users/123' ,
249+ status : 'ok' ,
250+ metadata : {
251+ source : 'url' ,
252+ } ,
253+ } ,
254+ expect . any ( Function ) ,
255+ expect . any ( Function ) ,
256+ ) ;
257+ } ) ;
139258 } ) ;
140259
141260 describe ( 'with error() helper' , ( ) => {
0 commit comments