Skip to content

Commit bbfa3b2

Browse files
committed
feat(core): Add beforeStartNavigationSpan lifecycle hook
1 parent 5534ba9 commit bbfa3b2

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

packages/browser/src/tracing/browserTracingIntegration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ export function startBrowserTracingNavigationSpan(
657657
options?: { url?: string; isRedirect?: boolean },
658658
): Span | undefined {
659659
const { url, isRedirect } = options || {};
660-
660+
client.emit('beforeStartNavigationSpan', spanOptions);
661661
client.emit('startNavigationSpan', spanOptions, { isRedirect });
662662

663663
const scope = getCurrentScope();

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.
@@ -782,6 +788,11 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
782788
traceOptions?: { sentryTrace?: string | undefined; baggage?: string | undefined },
783789
): void;
784790

791+
/**
792+
* Emit a hook event for triggering right before a navigation span is started.
793+
*/
794+
public emit(hook: 'beforeStartNavigationSpan', options: StartSpanOptions): void;
795+
785796
/**
786797
* Emit a hook event for browser tracing integrations to trigger a span for a navigation.
787798
*/

0 commit comments

Comments
 (0)