Skip to content

Commit abb8660

Browse files
committed
feat(browser): Add afterStartPageloadSpan hook to improve spanId assignment on web vital spans
1 parent c7a1e9a commit abb8660

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

packages/browser-utils/src/metrics/utils.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,9 @@ export function listenForWebVitalReportEvents(
237237
}
238238
});
239239

240-
const activeSpan = getActiveSpan();
241-
if (activeSpan) {
242-
const rootSpan = getRootSpan(activeSpan);
243-
const spanJSON = spanToJSON(rootSpan);
244-
if (spanJSON.op === 'pageload') {
245-
pageloadSpanId = rootSpan.spanContext().spanId;
246-
}
247-
}
240+
const unsubscribeAfterStartPageLoadSpan = client.on('afterStartPageLoadSpan', span => {
241+
pageloadSpanId = span.spanContext().spanId;
242+
unsubscribeAfterStartPageLoadSpan?.();
243+
});
248244
}, 0);
249245
}

packages/browser/src/tracing/browserTracingIntegration.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,13 @@ export function startBrowserTracingPageLoadSpan(
644644
client.emit('startPageLoadSpan', spanOptions, traceOptions);
645645
getCurrentScope().setTransactionName(spanOptions.name);
646646

647-
return getActiveIdleSpan(client);
647+
const pageloadSpan = getActiveIdleSpan(client);
648+
649+
if (pageloadSpan) {
650+
client.emit('afterStartPageLoadSpan', pageloadSpan);
651+
}
652+
653+
return pageloadSpan;
648654
}
649655

650656
/**

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 the browser tracing integrations to trigger after the pageload span was started.
608+
* @returns {() => void} A function that, when executed, removes the registered callback.
609+
*/
610+
public on(hook: 'afterStartPageLoadSpan', callback: (span: Span) => void): () => void;
611+
606612
/**
607613
* A hook for triggering right before a navigation span is started.
608614
* @returns {() => void} A function that, when executed, removes the registered callback.
@@ -791,6 +797,11 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
791797
traceOptions?: { sentryTrace?: string | undefined; baggage?: string | undefined },
792798
): void;
793799

800+
/**
801+
* Emit a hook event for browser tracing integrations to trigger aafter the pageload span was started.
802+
*/
803+
public emit(hook: 'afterStartPageLoadSpan', span: Span): void;
804+
794805
/**
795806
* Emit a hook event for triggering right before a navigation span is started.
796807
*/

0 commit comments

Comments
 (0)