1
- import * as Sentry from '@sentry/node-experimental ' ;
1
+ import { NodeClient , defaultStackParser , flush , makeNodeTransport , setCurrentClient , startInactiveSpan } from '@sentry/node' ;
2
2
3
- import { getMainCarrier } from '@sentry/core' ;
3
+ import { getMainCarrier , } from '@sentry/core' ;
4
4
import type { Transport } from '@sentry/types' ;
5
5
import { GLOBAL_OBJ , createEnvelope , logger } from '@sentry/utils' ;
6
6
import { CpuProfilerBindings } from '../src/cpu_profiler' ;
7
7
import { _nodeProfilingIntegration } from '../src/integration' ;
8
8
9
- function makeClientWithHooks ( ) : [ Sentry . NodeClient , Transport ] {
9
+ function makeClientWithHooks ( ) : [ NodeClient , Transport ] {
10
10
const integration = _nodeProfilingIntegration ( ) ;
11
- const client = new Sentry . NodeClient ( {
12
- stackParser : Sentry . defaultStackParser ,
11
+ const client = new NodeClient ( {
12
+ stackParser : defaultStackParser ,
13
13
tracesSampleRate : 1 ,
14
14
profilesSampleRate : 1 ,
15
15
debug : true ,
16
16
environment : 'test-environment' ,
17
17
dsn :
'https://[email protected] /6625302' ,
18
18
integrations : [ integration ] ,
19
19
transport : _opts =>
20
- Sentry . makeNodeTransport ( {
20
+ makeNodeTransport ( {
21
21
url :
'https://[email protected] /6625302' ,
22
22
recordDroppedEvent : ( ) => {
23
23
return undefined ;
@@ -45,24 +45,24 @@ describe('spanProfileUtils', () => {
45
45
46
46
it ( 'pulls environment from sdk init' , async ( ) => {
47
47
const [ client , transport ] = makeClientWithHooks ( ) ;
48
- Sentry . setCurrentClient ( client ) ;
48
+ setCurrentClient ( client ) ;
49
49
client . init ( ) ;
50
50
51
51
const transportSpy = jest . spyOn ( transport , 'send' ) . mockReturnValue ( Promise . resolve ( { } ) ) ;
52
52
53
- const transaction = Sentry . startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
53
+ const transaction = startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
54
54
await wait ( 500 ) ;
55
55
transaction . end ( ) ;
56
56
57
- await Sentry . flush ( 1000 ) ;
57
+ await flush ( 1000 ) ;
58
58
expect ( transportSpy . mock . calls ?. [ 0 ] ?. [ 0 ] ?. [ 1 ] ?. [ 0 ] ?. [ 1 ] ) . toMatchObject ( { environment : 'test-environment' } ) ;
59
59
} ) ;
60
60
61
61
it ( 'logger warns user if there are insufficient samples and discards the profile' , async ( ) => {
62
62
const logSpy = jest . spyOn ( logger , 'log' ) ;
63
63
64
64
const [ client , transport ] = makeClientWithHooks ( ) ;
65
- Sentry . setCurrentClient ( client ) ;
65
+ setCurrentClient ( client ) ;
66
66
client . init ( ) ;
67
67
68
68
jest . spyOn ( CpuProfilerBindings , 'stopProfiling' ) . mockImplementation ( ( ) => {
@@ -84,10 +84,10 @@ describe('spanProfileUtils', () => {
84
84
85
85
jest . spyOn ( transport , 'send' ) . mockReturnValue ( Promise . resolve ( { } ) ) ;
86
86
87
- const transaction = Sentry . startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
87
+ const transaction = startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
88
88
transaction . end ( ) ;
89
89
90
- await Sentry . flush ( 1000 ) ;
90
+ await flush ( 1000 ) ;
91
91
92
92
expect ( logSpy ) . toHaveBeenCalledWith ( '[Profiling] Discarding profile because it contains less than 2 samples' ) ;
93
93
@@ -100,7 +100,7 @@ describe('spanProfileUtils', () => {
100
100
const logSpy = jest . spyOn ( logger , 'log' ) ;
101
101
102
102
const [ client , transport ] = makeClientWithHooks ( ) ;
103
- Sentry . setCurrentClient ( client ) ;
103
+ setCurrentClient ( client ) ;
104
104
client . init ( ) ;
105
105
106
106
jest . spyOn ( CpuProfilerBindings , 'stopProfiling' ) . mockImplementation ( ( ) => {
@@ -127,48 +127,48 @@ describe('spanProfileUtils', () => {
127
127
128
128
jest . spyOn ( transport , 'send' ) . mockReturnValue ( Promise . resolve ( { } ) ) ;
129
129
130
- const transaction = Sentry . startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' , traceId : 'boop' } ) ;
130
+ const transaction = startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' , traceId : 'boop' } ) ;
131
131
await wait ( 500 ) ;
132
132
transaction . end ( ) ;
133
133
134
- await Sentry . flush ( 1000 ) ;
134
+ await flush ( 1000 ) ;
135
135
136
136
expect ( logSpy ) . toHaveBeenCalledWith ( '[Profiling] Invalid traceId: ' + 'boop' + ' on profiled event' ) ;
137
137
} ) ;
138
138
139
139
describe ( 'with hooks' , ( ) => {
140
140
it ( 'calls profiler when transaction is started/stopped' , async ( ) => {
141
141
const [ client , transport ] = makeClientWithHooks ( ) ;
142
- Sentry . setCurrentClient ( client ) ;
142
+ setCurrentClient ( client ) ;
143
143
client . init ( ) ;
144
144
145
145
const startProfilingSpy = jest . spyOn ( CpuProfilerBindings , 'startProfiling' ) ;
146
146
const stopProfilingSpy = jest . spyOn ( CpuProfilerBindings , 'stopProfiling' ) ;
147
147
148
148
jest . spyOn ( transport , 'send' ) . mockReturnValue ( Promise . resolve ( { } ) ) ;
149
149
150
- const transaction = Sentry . startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
150
+ const transaction = startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
151
151
await wait ( 500 ) ;
152
152
transaction . end ( ) ;
153
153
154
- await Sentry . flush ( 1000 ) ;
154
+ await flush ( 1000 ) ;
155
155
156
156
expect ( startProfilingSpy ) . toHaveBeenCalledTimes ( 1 ) ;
157
157
expect ( ( stopProfilingSpy . mock . calls [ stopProfilingSpy . mock . calls . length - 1 ] ?. [ 0 ] as string ) . length ) . toBe ( 32 ) ;
158
158
} ) ;
159
159
160
160
it ( 'sends profile in the same envelope as transaction' , async ( ) => {
161
161
const [ client , transport ] = makeClientWithHooks ( ) ;
162
- Sentry . setCurrentClient ( client ) ;
162
+ setCurrentClient ( client ) ;
163
163
client . init ( ) ;
164
164
165
165
const transportSpy = jest . spyOn ( transport , 'send' ) . mockReturnValue ( Promise . resolve ( { } ) ) ;
166
166
167
- const transaction = Sentry . startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
167
+ const transaction = startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
168
168
await wait ( 500 ) ;
169
169
transaction . end ( ) ;
170
170
171
- await Sentry . flush ( 1000 ) ;
171
+ await flush ( 1000 ) ;
172
172
173
173
// One for profile, the other for transaction
174
174
expect ( transportSpy ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -177,7 +177,7 @@ describe('spanProfileUtils', () => {
177
177
178
178
it ( 'does not crash if transaction has no profile context or it is invalid' , async ( ) => {
179
179
const [ client ] = makeClientWithHooks ( ) ;
180
- Sentry . setCurrentClient ( client ) ;
180
+ setCurrentClient ( client ) ;
181
181
client . init ( ) ;
182
182
183
183
// @ts -expect-error transaction is partial
@@ -201,7 +201,7 @@ describe('spanProfileUtils', () => {
201
201
202
202
it ( 'if transaction was profiled, but profiler returned null' , async ( ) => {
203
203
const [ client , transport ] = makeClientWithHooks ( ) ;
204
- Sentry . setCurrentClient ( client ) ;
204
+ setCurrentClient ( client ) ;
205
205
client . init ( ) ;
206
206
207
207
jest . spyOn ( CpuProfilerBindings , 'stopProfiling' ) . mockReturnValue ( null ) ;
@@ -211,11 +211,11 @@ describe('spanProfileUtils', () => {
211
211
return Promise . resolve ( { } ) ;
212
212
} ) ;
213
213
214
- const transaction = Sentry . startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
214
+ const transaction = startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
215
215
await wait ( 500 ) ;
216
216
transaction . end ( ) ;
217
217
218
- await Sentry . flush ( 1000 ) ;
218
+ await flush ( 1000 ) ;
219
219
220
220
// Only transaction is sent
221
221
expect ( transportSpy . mock . calls ?. [ 0 ] ?. [ 0 ] ?. [ 1 ] ?. [ 0 ] ?. [ 0 ] ) . toMatchObject ( { type : 'transaction' } ) ;
@@ -224,18 +224,18 @@ describe('spanProfileUtils', () => {
224
224
225
225
it ( 'emits preprocessEvent for profile' , async ( ) => {
226
226
const [ client ] = makeClientWithHooks ( ) ;
227
- Sentry . setCurrentClient ( client ) ;
227
+ setCurrentClient ( client ) ;
228
228
client . init ( ) ;
229
229
230
230
const onPreprocessEvent = jest . fn ( ) ;
231
231
232
232
client . on ( 'preprocessEvent' , onPreprocessEvent ) ;
233
233
234
- const transaction = Sentry . startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
234
+ const transaction = startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
235
235
await wait ( 500 ) ;
236
236
transaction . end ( ) ;
237
237
238
- await Sentry . flush ( 1000 ) ;
238
+ await flush ( 1000 ) ;
239
239
240
240
expect ( onPreprocessEvent . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( {
241
241
profile : {
@@ -250,10 +250,10 @@ describe('spanProfileUtils', () => {
250
250
const stopProfilingSpy = jest . spyOn ( CpuProfilerBindings , 'stopProfiling' ) ;
251
251
252
252
const [ client ] = makeClientWithHooks ( ) ;
253
- Sentry . setCurrentClient ( client ) ;
253
+ setCurrentClient ( client ) ;
254
254
client . init ( ) ;
255
255
256
- const transaction = Sentry . startInactiveSpan ( { forceTransaction : true , name : 'txn' } ) ;
256
+ const transaction = startInactiveSpan ( { forceTransaction : true , name : 'txn' } ) ;
257
257
transaction . end ( ) ;
258
258
transaction . end ( ) ;
259
259
expect ( stopProfilingSpy ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -289,16 +289,16 @@ describe('spanProfileUtils', () => {
289
289
} ) ;
290
290
291
291
const [ client , transport ] = makeClientWithHooks ( ) ;
292
- Sentry . setCurrentClient ( client ) ;
292
+ setCurrentClient ( client ) ;
293
293
client . init ( ) ;
294
294
295
295
const transportSpy = jest . spyOn ( transport , 'send' ) . mockReturnValue ( Promise . resolve ( { } ) ) ;
296
296
297
- const transaction = Sentry . startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
297
+ const transaction = startInactiveSpan ( { forceTransaction : true , name : 'profile_hub' } ) ;
298
298
await wait ( 500 ) ;
299
299
transaction . end ( ) ;
300
300
301
- await Sentry . flush ( 1000 ) ;
301
+ await flush ( 1000 ) ;
302
302
303
303
expect ( transportSpy . mock . calls ?. [ 0 ] ?. [ 0 ] ?. [ 1 ] ?. [ 1 ] ?. [ 1 ] ) . toMatchObject ( {
304
304
debug_meta : {
0 commit comments