1
- import type { EventEnvelope , EventProcessor , Hub , Integration , Transaction } from '@sentry/types' ;
1
+ import { getCurrentScope } from '@sentry/core' ;
2
+ import type { Client , EventEnvelope , EventProcessor , Hub , Integration , Transaction } from '@sentry/types' ;
2
3
import type { Profile } from '@sentry/types/src/profiling' ;
3
4
import { logger } from '@sentry/utils' ;
4
5
@@ -29,6 +30,7 @@ export class BrowserProfilingIntegration implements Integration {
29
30
30
31
public readonly name : string ;
31
32
33
+ /** @deprecated This is never set. */
32
34
public getCurrentHub ?: ( ) => Hub ;
33
35
34
36
public constructor ( ) {
@@ -38,12 +40,13 @@ export class BrowserProfilingIntegration implements Integration {
38
40
/**
39
41
* @inheritDoc
40
42
*/
41
- public setupOnce ( _addGlobalEventProcessor : ( callback : EventProcessor ) => void , getCurrentHub : ( ) => Hub ) : void {
42
- this . getCurrentHub = getCurrentHub ;
43
+ public setupOnce ( _addGlobalEventProcessor : ( callback : EventProcessor ) => void , _getCurrentHub : ( ) => Hub ) : void {
44
+ // noop
45
+ }
43
46
44
- const hub = this . getCurrentHub ( ) ;
45
- const client = hub . getClient ( ) ;
46
- const scope = hub . getScope ( ) ;
47
+ /** @inheritdoc */
48
+ public setup ( client : Client ) : void {
49
+ const scope = getCurrentScope ( ) ;
47
50
48
51
const transaction = scope . getTransaction ( ) ;
49
52
@@ -53,67 +56,68 @@ export class BrowserProfilingIntegration implements Integration {
53
56
}
54
57
}
55
58
56
- if ( client && typeof client . on === 'function' ) {
57
- client . on ( 'startTransaction' , ( transaction : Transaction ) => {
58
- if ( shouldProfileTransaction ( transaction ) ) {
59
- startProfileForTransaction ( transaction ) ;
59
+ if ( typeof client . on !== 'function' ) {
60
+ logger . warn ( '[Profiling] Client does not support hooks, profiling will be disabled' ) ;
61
+ return ;
62
+ }
63
+
64
+ client . on ( 'startTransaction' , ( transaction : Transaction ) => {
65
+ if ( shouldProfileTransaction ( transaction ) ) {
66
+ startProfileForTransaction ( transaction ) ;
67
+ }
68
+ } ) ;
69
+
70
+ client . on ( 'beforeEnvelope' , ( envelope ) : void => {
71
+ // if not profiles are in queue, there is nothing to add to the envelope.
72
+ if ( ! getActiveProfilesCount ( ) ) {
73
+ return ;
74
+ }
75
+
76
+ const profiledTransactionEvents = findProfiledTransactionsFromEnvelope ( envelope ) ;
77
+ if ( ! profiledTransactionEvents . length ) {
78
+ return ;
79
+ }
80
+
81
+ const profilesToAddToEnvelope : Profile [ ] = [ ] ;
82
+
83
+ for ( const profiledTransaction of profiledTransactionEvents ) {
84
+ const context = profiledTransaction && profiledTransaction . contexts ;
85
+ const profile_id = context && context [ 'profile' ] && context [ 'profile' ] [ 'profile_id' ] ;
86
+ const start_timestamp = context && context [ 'profile' ] && context [ 'profile' ] [ 'start_timestamp' ] ;
87
+
88
+ if ( typeof profile_id !== 'string' ) {
89
+ DEBUG_BUILD && logger . log ( '[Profiling] cannot find profile for a transaction without a profile context' ) ;
90
+ continue ;
60
91
}
61
- } ) ;
62
92
63
- client . on ( 'beforeEnvelope' , ( envelope ) : void => {
64
- // if not profiles are in queue, there is nothing to add to the envelope.
65
- if ( ! getActiveProfilesCount ( ) ) {
66
- return ;
93
+ if ( ! profile_id ) {
94
+ DEBUG_BUILD && logger . log ( '[Profiling] cannot find profile for a transaction without a profile context' ) ;
95
+ continue ;
67
96
}
68
97
69
- const profiledTransactionEvents = findProfiledTransactionsFromEnvelope ( envelope ) ;
70
- if ( ! profiledTransactionEvents . length ) {
71
- return ;
98
+ // Remove the profile from the transaction context before sending, relay will take care of the rest.
99
+ if ( context && context [ 'profile' ] ) {
100
+ delete context . profile ;
72
101
}
73
102
74
- const profilesToAddToEnvelope : Profile [ ] = [ ] ;
75
-
76
- for ( const profiledTransaction of profiledTransactionEvents ) {
77
- const context = profiledTransaction && profiledTransaction . contexts ;
78
- const profile_id = context && context [ 'profile' ] && context [ 'profile' ] [ 'profile_id' ] ;
79
- const start_timestamp = context && context [ 'profile' ] && context [ 'profile' ] [ 'start_timestamp' ] ;
80
-
81
- if ( typeof profile_id !== 'string' ) {
82
- DEBUG_BUILD && logger . log ( '[Profiling] cannot find profile for a transaction without a profile context' ) ;
83
- continue ;
84
- }
85
-
86
- if ( ! profile_id ) {
87
- DEBUG_BUILD && logger . log ( '[Profiling] cannot find profile for a transaction without a profile context' ) ;
88
- continue ;
89
- }
90
-
91
- // Remove the profile from the transaction context before sending, relay will take care of the rest.
92
- if ( context && context [ 'profile' ] ) {
93
- delete context . profile ;
94
- }
95
-
96
- const profile = takeProfileFromGlobalCache ( profile_id ) ;
97
- if ( ! profile ) {
98
- DEBUG_BUILD && logger . log ( `[Profiling] Could not retrieve profile for transaction: ${ profile_id } ` ) ;
99
- continue ;
100
- }
101
-
102
- const profileEvent = createProfilingEvent (
103
- profile_id ,
104
- start_timestamp as number | undefined ,
105
- profile ,
106
- profiledTransaction as ProfiledEvent ,
107
- ) ;
108
- if ( profileEvent ) {
109
- profilesToAddToEnvelope . push ( profileEvent ) ;
110
- }
103
+ const profile = takeProfileFromGlobalCache ( profile_id ) ;
104
+ if ( ! profile ) {
105
+ DEBUG_BUILD && logger . log ( `[Profiling] Could not retrieve profile for transaction: ${ profile_id } ` ) ;
106
+ continue ;
111
107
}
112
108
113
- addProfilesToEnvelope ( envelope as EventEnvelope , profilesToAddToEnvelope ) ;
114
- } ) ;
115
- } else {
116
- logger . warn ( '[Profiling] Client does not support hooks, profiling will be disabled' ) ;
117
- }
109
+ const profileEvent = createProfilingEvent (
110
+ profile_id ,
111
+ start_timestamp as number | undefined ,
112
+ profile ,
113
+ profiledTransaction as ProfiledEvent ,
114
+ ) ;
115
+ if ( profileEvent ) {
116
+ profilesToAddToEnvelope . push ( profileEvent ) ;
117
+ }
118
+ }
119
+
120
+ addProfilesToEnvelope ( envelope as EventEnvelope , profilesToAddToEnvelope ) ;
121
+ } ) ;
118
122
}
119
123
}
0 commit comments