1
1
/* eslint-disable @typescript-eslint/unbound-method */
2
2
import { BrowserClient } from '@sentry/browser' ;
3
- import { Hub } from '@sentry/hub' ;
4
- import * as hubModule from '@sentry/hub' ;
3
+ import { Hub , makeMain } from '@sentry/hub' ;
5
4
import { TransactionSamplingMethod } from '@sentry/types' ;
6
5
import * as utilsModule from '@sentry/utils' ; // for mocking
7
6
import { logger } from '@sentry/utils' ;
@@ -32,6 +31,7 @@ describe('Hub', () => {
32
31
describe ( 'getTransaction()' , ( ) => {
33
32
it ( 'should find a transaction which has been set on the scope if sampled = true' , ( ) => {
34
33
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
34
+ makeMain ( hub ) ;
35
35
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
36
36
transaction . sampled = true ;
37
37
@@ -44,6 +44,7 @@ describe('Hub', () => {
44
44
45
45
it ( 'should find a transaction which has been set on the scope if sampled = false' , ( ) => {
46
46
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
47
+ makeMain ( hub ) ;
47
48
const transaction = hub . startTransaction ( { name : 'dogpark' , sampled : false } ) ;
48
49
49
50
hub . configureScope ( scope => {
@@ -55,6 +56,7 @@ describe('Hub', () => {
55
56
56
57
it ( "should not find an open transaction if it's not on the scope" , ( ) => {
57
58
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
59
+ makeMain ( hub ) ;
58
60
hub . startTransaction ( { name : 'dogpark' } ) ;
59
61
60
62
expect ( hub . getScope ( ) ?. getTransaction ( ) ) . toBeUndefined ( ) ;
@@ -66,6 +68,8 @@ describe('Hub', () => {
66
68
it ( 'should add transaction context data to default sample context' , ( ) => {
67
69
const tracesSampler = jest . fn ( ) ;
68
70
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
71
+ makeMain ( hub ) ;
72
+
69
73
const transactionContext = {
70
74
name : 'dogpark' ,
71
75
parentSpanId : '12312012' ,
@@ -80,6 +84,7 @@ describe('Hub', () => {
80
84
it ( "should add parent's sampling decision to default sample context" , ( ) => {
81
85
const tracesSampler = jest . fn ( ) ;
82
86
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
87
+ makeMain ( hub ) ;
83
88
const parentSamplingDecsion = false ;
84
89
85
90
hub . startTransaction ( {
@@ -98,20 +103,23 @@ describe('Hub', () => {
98
103
it ( 'should set sampled = false when tracing is disabled' , ( ) => {
99
104
// neither tracesSampleRate nor tracesSampler is defined -> tracing disabled
100
105
const hub = new Hub ( new BrowserClient ( { } ) ) ;
106
+ makeMain ( hub ) ;
101
107
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
102
108
103
109
expect ( transaction . sampled ) . toBe ( false ) ;
104
110
} ) ;
105
111
106
112
it ( 'should set sampled = false if tracesSampleRate is 0' , ( ) => {
107
113
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 0 } ) ) ;
114
+ makeMain ( hub ) ;
108
115
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
109
116
110
117
expect ( transaction . sampled ) . toBe ( false ) ;
111
118
} ) ;
112
119
113
120
it ( 'should set sampled = true if tracesSampleRate is 1' , ( ) => {
114
121
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
122
+ makeMain ( hub ) ;
115
123
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
116
124
117
125
expect ( transaction . sampled ) . toBe ( true ) ;
@@ -120,6 +128,7 @@ describe('Hub', () => {
120
128
it ( "should call tracesSampler if it's defined" , ( ) => {
121
129
const tracesSampler = jest . fn ( ) ;
122
130
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
131
+ makeMain ( hub ) ;
123
132
hub . startTransaction ( { name : 'dogpark' } ) ;
124
133
125
134
expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
@@ -128,6 +137,7 @@ describe('Hub', () => {
128
137
it ( 'should set sampled = false if tracesSampler returns 0' , ( ) => {
129
138
const tracesSampler = jest . fn ( ) . mockReturnValue ( 0 ) ;
130
139
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
140
+ makeMain ( hub ) ;
131
141
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
132
142
133
143
expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
@@ -137,6 +147,7 @@ describe('Hub', () => {
137
147
it ( 'should set sampled = true if tracesSampler returns 1' , ( ) => {
138
148
const tracesSampler = jest . fn ( ) . mockReturnValue ( 1 ) ;
139
149
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
150
+ makeMain ( hub ) ;
140
151
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
141
152
142
153
expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
@@ -147,6 +158,7 @@ describe('Hub', () => {
147
158
// so that the decision otherwise would be false
148
159
const tracesSampler = jest . fn ( ) . mockReturnValue ( 0 ) ;
149
160
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
161
+ makeMain ( hub ) ;
150
162
const transaction = hub . startTransaction ( { name : 'dogpark' , sampled : true } ) ;
151
163
152
164
expect ( transaction . sampled ) . toBe ( true ) ;
@@ -156,6 +168,7 @@ describe('Hub', () => {
156
168
// so that the decision otherwise would be true
157
169
const tracesSampler = jest . fn ( ) . mockReturnValue ( 1 ) ;
158
170
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
171
+ makeMain ( hub ) ;
159
172
const transaction = hub . startTransaction ( { name : 'dogpark' , sampled : false } ) ;
160
173
161
174
expect ( transaction . sampled ) . toBe ( false ) ;
@@ -165,6 +178,7 @@ describe('Hub', () => {
165
178
// make the two options do opposite things to prove precedence
166
179
const tracesSampler = jest . fn ( ) . mockReturnValue ( true ) ;
167
180
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 0 , tracesSampler } ) ) ;
181
+ makeMain ( hub ) ;
168
182
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
169
183
170
184
expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
@@ -174,6 +188,7 @@ describe('Hub', () => {
174
188
it ( 'should tolerate tracesSampler returning a boolean' , ( ) => {
175
189
const tracesSampler = jest . fn ( ) . mockReturnValue ( true ) ;
176
190
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
191
+ makeMain ( hub ) ;
177
192
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
178
193
179
194
expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
@@ -183,6 +198,7 @@ describe('Hub', () => {
183
198
it ( 'should record sampling method when sampling decision is explicitly set' , ( ) => {
184
199
const tracesSampler = jest . fn ( ) . mockReturnValue ( 0.1121 ) ;
185
200
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
201
+ makeMain ( hub ) ;
186
202
hub . startTransaction ( { name : 'dogpark' , sampled : true } ) ;
187
203
188
204
expect ( Transaction . prototype . setMetadata ) . toHaveBeenCalledWith ( {
@@ -193,6 +209,7 @@ describe('Hub', () => {
193
209
it ( 'should record sampling method and rate when sampling decision comes from tracesSampler' , ( ) => {
194
210
const tracesSampler = jest . fn ( ) . mockReturnValue ( 0.1121 ) ;
195
211
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
212
+ makeMain ( hub ) ;
196
213
hub . startTransaction ( { name : 'dogpark' } ) ;
197
214
198
215
expect ( Transaction . prototype . setMetadata ) . toHaveBeenCalledWith ( {
@@ -202,6 +219,7 @@ describe('Hub', () => {
202
219
203
220
it ( 'should record sampling method when sampling decision is inherited' , ( ) => {
204
221
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 0.1121 } ) ) ;
222
+ makeMain ( hub ) ;
205
223
hub . startTransaction ( { name : 'dogpark' , parentSampled : true } ) ;
206
224
207
225
expect ( Transaction . prototype . setMetadata ) . toHaveBeenCalledWith ( {
@@ -211,6 +229,7 @@ describe('Hub', () => {
211
229
212
230
it ( 'should record sampling method and rate when sampling decision comes from traceSampleRate' , ( ) => {
213
231
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 0.1121 } ) ) ;
232
+ makeMain ( hub ) ;
214
233
hub . startTransaction ( { name : 'dogpark' } ) ;
215
234
216
235
expect ( Transaction . prototype . setMetadata ) . toHaveBeenCalledWith ( {
@@ -222,13 +241,15 @@ describe('Hub', () => {
222
241
describe ( 'isValidSampleRate()' , ( ) => {
223
242
it ( "should reject tracesSampleRates which aren't numbers or booleans" , ( ) => {
224
243
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 'dogs!' as any } ) ) ;
244
+ makeMain ( hub ) ;
225
245
hub . startTransaction ( { name : 'dogpark' } ) ;
226
246
227
247
expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be a boolean or a number' ) ) ;
228
248
} ) ;
229
249
230
250
it ( 'should reject tracesSampleRates which are NaN' , ( ) => {
231
251
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 'dogs!' as any } ) ) ;
252
+ makeMain ( hub ) ;
232
253
hub . startTransaction ( { name : 'dogpark' } ) ;
233
254
234
255
expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be a boolean or a number' ) ) ;
@@ -237,6 +258,7 @@ describe('Hub', () => {
237
258
// the rate might be a boolean, but for our purposes, false is equivalent to 0 and true is equivalent to 1
238
259
it ( 'should reject tracesSampleRates less than 0' , ( ) => {
239
260
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : - 26 } ) ) ;
261
+ makeMain ( hub ) ;
240
262
hub . startTransaction ( { name : 'dogpark' } ) ;
241
263
242
264
expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be between 0 and 1' ) ) ;
@@ -245,6 +267,7 @@ describe('Hub', () => {
245
267
// the rate might be a boolean, but for our purposes, false is equivalent to 0 and true is equivalent to 1
246
268
it ( 'should reject tracesSampleRates greater than 1' , ( ) => {
247
269
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 26 } ) ) ;
270
+ makeMain ( hub ) ;
248
271
hub . startTransaction ( { name : 'dogpark' } ) ;
249
272
250
273
expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be between 0 and 1' ) ) ;
@@ -253,6 +276,7 @@ describe('Hub', () => {
253
276
it ( "should reject tracesSampler return values which aren't numbers or booleans" , ( ) => {
254
277
const tracesSampler = jest . fn ( ) . mockReturnValue ( 'dogs!' ) ;
255
278
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
279
+ makeMain ( hub ) ;
256
280
hub . startTransaction ( { name : 'dogpark' } ) ;
257
281
258
282
expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be a boolean or a number' ) ) ;
@@ -261,6 +285,7 @@ describe('Hub', () => {
261
285
it ( 'should reject tracesSampler return values which are NaN' , ( ) => {
262
286
const tracesSampler = jest . fn ( ) . mockReturnValue ( NaN ) ;
263
287
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
288
+ makeMain ( hub ) ;
264
289
hub . startTransaction ( { name : 'dogpark' } ) ;
265
290
266
291
expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be a boolean or a number' ) ) ;
@@ -270,6 +295,7 @@ describe('Hub', () => {
270
295
it ( 'should reject tracesSampler return values less than 0' , ( ) => {
271
296
const tracesSampler = jest . fn ( ) . mockReturnValue ( - 12 ) ;
272
297
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
298
+ makeMain ( hub ) ;
273
299
hub . startTransaction ( { name : 'dogpark' } ) ;
274
300
275
301
expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be between 0 and 1' ) ) ;
@@ -279,6 +305,7 @@ describe('Hub', () => {
279
305
it ( 'should reject tracesSampler return values greater than 1' , ( ) => {
280
306
const tracesSampler = jest . fn ( ) . mockReturnValue ( 31 ) ;
281
307
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
308
+ makeMain ( hub ) ;
282
309
hub . startTransaction ( { name : 'dogpark' } ) ;
283
310
284
311
expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be between 0 and 1' ) ) ;
@@ -290,6 +317,7 @@ describe('Hub', () => {
290
317
jest . spyOn ( client , 'captureEvent' ) ;
291
318
292
319
const hub = new Hub ( client ) ;
320
+ makeMain ( hub ) ;
293
321
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
294
322
295
323
jest . spyOn ( transaction , 'finish' ) ;
@@ -303,6 +331,7 @@ describe('Hub', () => {
303
331
describe ( 'sampling inheritance' , ( ) => {
304
332
it ( 'should propagate sampling decision to child spans' , ( ) => {
305
333
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : Math . random ( ) } ) ) ;
334
+ makeMain ( hub ) ;
306
335
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
307
336
const child = transaction . startChild ( { op : 'ball.chase' } ) ;
308
337
@@ -320,7 +349,7 @@ describe('Hub', () => {
320
349
integrations : [ new BrowserTracing ( ) ] ,
321
350
} ) ,
322
351
) ;
323
- jest . spyOn ( hubModule , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
352
+ makeMain ( hub ) ;
324
353
325
354
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
326
355
hub . configureScope ( scope => {
@@ -362,7 +391,7 @@ describe('Hub', () => {
362
391
integrations : [ new BrowserTracing ( ) ] ,
363
392
} ) ,
364
393
) ;
365
- jest . spyOn ( hubModule , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
394
+ makeMain ( hub ) ;
366
395
367
396
const transaction = hub . startTransaction ( { name : 'dogpark' , sampled : false } ) ;
368
397
hub . configureScope ( scope => {
@@ -407,6 +436,7 @@ describe('Hub', () => {
407
436
// tracesSampleRate
408
437
mathRandom . mockReturnValueOnce ( 1 ) ;
409
438
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 0.5 } ) ) ;
439
+ makeMain ( hub ) ;
410
440
const parentSamplingDecsion = true ;
411
441
412
442
const transaction = hub . startTransaction ( {
@@ -422,6 +452,7 @@ describe('Hub', () => {
422
452
// tracesSampleRate = 1 means every transaction should end up with sampled = true, so make parent's decision the
423
453
// opposite to prove that inheritance takes precedence over tracesSampleRate
424
454
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
455
+ makeMain ( hub ) ;
425
456
const parentSamplingDecsion = false ;
426
457
427
458
const transaction = hub . startTransaction ( {
@@ -440,6 +471,7 @@ describe('Hub', () => {
440
471
const parentSamplingDecsion = false ;
441
472
442
473
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
474
+ makeMain ( hub ) ;
443
475
444
476
const transaction = hub . startTransaction ( {
445
477
name : 'dogpark' ,
@@ -457,6 +489,7 @@ describe('Hub', () => {
457
489
const parentSamplingDecsion = true ;
458
490
459
491
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
492
+ makeMain ( hub ) ;
460
493
461
494
const transaction = hub . startTransaction ( {
462
495
name : 'dogpark' ,
0 commit comments