Skip to content

Commit 1cbd267

Browse files
committed
feat(core): Add beforeStartNavigationSpan lifecycle hook
1 parent 29bf0d2 commit 1cbd267

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

packages/browser/src/tracing/browserTracingIntegration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ export function startBrowserTracingPageLoadSpan(
609609
* This will only do something if a browser tracing integration has been setup.
610610
*/
611611
export function startBrowserTracingNavigationSpan(client: Client, spanOptions: StartSpanOptions): Span | undefined {
612+
client.emit('beforeStartNavigationSpan', spanOptions);
612613
client.emit('startNavigationSpan', spanOptions);
613614

614615
getCurrentScope().setTransactionName(spanOptions.name);

packages/browser/test/tracing/browserTracingIntegration.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,24 @@ describe('browserTracingIntegration', () => {
788788
},
789789
});
790790
});
791+
792+
it('triggers beforeStartNavigationSpan hook listeners', () => {
793+
const client = new BrowserClient(
794+
getDefaultBrowserClientOptions({
795+
tracesSampleRate: 1,
796+
integrations: [browserTracingIntegration()],
797+
}),
798+
);
799+
setCurrentClient(client);
800+
801+
const mockBeforeStartNavigationSpanCallback = vi.fn((options: StartSpanOptions) => options);
802+
803+
client.on('beforeStartNavigationSpan', mockBeforeStartNavigationSpanCallback);
804+
805+
startBrowserTracingNavigationSpan(client, { name: 'test span', op: 'navigation' });
806+
807+
expect(mockBeforeStartNavigationSpanCallback).toHaveBeenCalledWith({ name: 'test span', op: 'navigation' });
808+
});
791809
});
792810

793811
describe('using the <meta> tag data', () => {

packages/core/src/client.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,12 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
603603
) => void,
604604
): () => void;
605605

606+
/**
607+
* A hook for triggering right before a navigation span is started.
608+
* @returns {() => void} A function that, when executed, removes the registered callback.
609+
*/
610+
public on(hook: 'beforeStartNavigationSpan', callback: (options: StartSpanOptions) => void): () => void;
611+
606612
/**
607613
* A hook for browser tracing integrations to trigger a span for a navigation.
608614
* @returns {() => void} A function that, when executed, removes the registered callback.
@@ -779,6 +785,11 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
779785
traceOptions?: { sentryTrace?: string | undefined; baggage?: string | undefined },
780786
): void;
781787

788+
/**
789+
* Emit a hook event for triggering right before a navigation span is started.
790+
*/
791+
public emit(hook: 'beforeStartNavigationSpan', options: StartSpanOptions): void;
792+
782793
/**
783794
* Emit a hook event for browser tracing integrations to trigger a span for a navigation.
784795
*/

0 commit comments

Comments
 (0)