1- import type { EventEnvelope , Integration , IntegrationFn , Profile , Span } from '@sentry/core' ;
1+ import type { EventEnvelope , IntegrationFn , Profile , Span } from '@sentry/core' ;
22import { debug , defineIntegration , getActiveSpan , getRootSpan , hasSpansEnabled } from '@sentry/core' ;
33import type { BrowserOptions } from '../client' ;
44import { DEBUG_BUILD } from '../debug-build' ;
@@ -23,9 +23,9 @@ const INTEGRATION_NAME = 'BrowserProfiling';
2323const _browserProfilingIntegration = ( ( ) => {
2424 return {
2525 name : INTEGRATION_NAME ,
26- _profiler : new UIProfiler ( ) ,
2726 setup ( client ) {
2827 const options = client . getOptions ( ) as BrowserOptions ;
28+ const profiler = new UIProfiler ( ) ;
2929
3030 if ( ! hasLegacyProfiling ( options ) && ! options . profileLifecycle ) {
3131 // Set default lifecycle mode
@@ -52,8 +52,12 @@ const _browserProfilingIntegration = (() => {
5252 if ( ! hasLegacyProfiling ( options ) ) {
5353 const lifecycleMode = options . profileLifecycle ;
5454
55+ // Registering hooks in all lifecycle modes to be able to notify users in case they want to start/stop the profiler manually in `trace` mode
56+ client . on ( 'startUIProfiler' , ( ) => profiler . start ( ) ) ;
57+ client . on ( 'stopUIProfiler' , ( ) => profiler . stop ( ) ) ;
58+
5559 if ( lifecycleMode === 'manual' ) {
56- this . _profiler . initialize ( client ) ;
60+ profiler . initialize ( client ) ;
5761 } else if ( lifecycleMode === 'trace' ) {
5862 if ( ! hasSpansEnabled ( options ) ) {
5963 DEBUG_BUILD &&
@@ -63,19 +67,19 @@ const _browserProfilingIntegration = (() => {
6367 return ;
6468 }
6569
66- this . _profiler . initialize ( client ) ;
70+ profiler . initialize ( client ) ;
6771
6872 // If there is an active, sampled root span already, notify the profiler
6973 if ( rootSpan ) {
70- this . _profiler . notifyRootSpanActive ( rootSpan ) ;
74+ profiler . notifyRootSpanActive ( rootSpan ) ;
7175 }
7276
7377 // In case rootSpan is created slightly after setup -> schedule microtask to re-check and notify.
7478 WINDOW . setTimeout ( ( ) => {
7579 const laterActiveSpan = getActiveSpan ( ) ;
7680 const laterRootSpan = laterActiveSpan && getRootSpan ( laterActiveSpan ) ;
7781 if ( laterRootSpan ) {
78- this . _profiler . notifyRootSpanActive ( laterRootSpan ) ;
82+ profiler . notifyRootSpanActive ( laterRootSpan ) ;
7983 }
8084 } , 0 ) ;
8185 }
@@ -151,10 +155,6 @@ const _browserProfilingIntegration = (() => {
151155 return attachProfiledThreadToEvent ( event ) ;
152156 } ,
153157 } ;
154- } ) satisfies IntegrationFn < BrowserProfilingIntegration > ;
155-
156- interface BrowserProfilingIntegration extends Integration {
157- _profiler : UIProfiler ;
158- }
158+ } ) satisfies IntegrationFn ;
159159
160160export const browserProfilingIntegration = defineIntegration ( _browserProfilingIntegration ) ;
0 commit comments