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' ;
23import type { Profile } from '@sentry/types/src/profiling' ;
34import { logger } from '@sentry/utils' ;
45
@@ -29,6 +30,7 @@ export class BrowserProfilingIntegration implements Integration {
2930
3031 public readonly name : string ;
3132
33+ /** @deprecated This is never set. */
3234 public getCurrentHub ?: ( ) => Hub ;
3335
3436 public constructor ( ) {
@@ -38,12 +40,13 @@ export class BrowserProfilingIntegration implements Integration {
3840 /**
3941 * @inheritDoc
4042 */
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+ }
4346
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 ( ) ;
4750
4851 const transaction = scope . getTransaction ( ) ;
4952
@@ -53,67 +56,68 @@ export class BrowserProfilingIntegration implements Integration {
5356 }
5457 }
5558
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 ;
6091 }
61- } ) ;
6292
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 ;
6796 }
6897
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 ;
72101 }
73102
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 ;
111107 }
112108
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+ } ) ;
118122 }
119123}
0 commit comments