Skip to content

Commit ff7b4bb

Browse files
committed
fix(profiling): Link root spans that start before native init to the app start profile
With startOnAppStart, the native SDK starts the profile before JS runs. Root spans that started and ended before native init had no session, so their transactions got no profiler id. Also, the afterInit handler then saw nothing to run and stopped the app start profile at once, without the delay that trace mode uses. The integration now creates the app start session in setup. Root spans that start before native init map to it, and its id resolves when native init completes. After that, the normal logic decides when the profiler stops. The session resolves without an id if the client closes before native init, so no transaction waits forever.
1 parent 3204b4b commit ff7b4bb

2 files changed

Lines changed: 67 additions & 4 deletions

File tree

‎packages/core/src/js/profiling/uiProfilingIntegration.ts‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const uiProfilingIntegration = ({
4848
let isManualRequested = lifecycle === 'manual' && startOnAppStart;
4949
let isAppStartPending = lifecycle === 'trace' && startOnAppStart;
5050
let jsThreadId: string | null = null;
51+
let resolveAppStartSession: ((profilerId: string | null) => void) | undefined;
5152

5253
const shouldRun = (): boolean =>
5354
lifecycle === 'trace' ? isAppStartPending || activeRootSpanIds.size > 0 : isManualRequested;
@@ -126,7 +127,8 @@ export const uiProfilingIntegration = ({
126127
return {
127128
name: INTEGRATION_NAME,
128129
setup(client: ReactNativeClient): void {
129-
if (hasSpansEnabled(client.getOptions())) {
130+
const tracingEnabled = hasSpansEnabled(client.getOptions());
131+
if (tracingEnabled) {
130132
jsThreadId = NATIVE.getCurrentThreadId();
131133
client.on('spanStart', onSpanStart);
132134
client.on('spanEnd', onSpanEnd);
@@ -135,17 +137,34 @@ export const uiProfilingIntegration = ({
135137
isAppStartPending = false;
136138
}
137139

140+
// The native SDK starts the app start profile before JS runs, so root spans that start before
141+
// native init belong to it. Its id is only available after native init.
142+
if (startOnAppStart && (lifecycle === 'manual' || tracingEnabled)) {
143+
session = {
144+
profilerId: new Promise(resolve => {
145+
resolveAppStartSession = resolve;
146+
}),
147+
};
148+
}
149+
138150
client.on('afterInit', () => {
139151
isNativeReady = true;
140-
if (startOnAppStart && !shouldRun()) {
141-
// The native SDK started the app start profile before JS ran.
152+
const resolve = resolveAppStartSession;
153+
resolveAppStartSession = undefined;
154+
if (resolve) {
155+
// The profiler already runs, so this call only returns its id.
156+
NATIVE.startProfiler().then(resolve, () => resolve(null));
157+
} else if (startOnAppStart) {
142158
stopNativeProfiler();
143159
return;
144160
}
145161
syncNativeProfiler();
146162
});
147163

148-
client.on('close', clearStopTimeout);
164+
client.on('close', () => {
165+
clearStopTimeout();
166+
resolveAppStartSession?.(null);
167+
});
149168
},
150169
processEvent(event: Event): Event | PromiseLike<Event> {
151170
const rootSpanId = event.type === 'transaction' ? event.contexts?.trace?.span_id : undefined;

‎packages/core/test/profiling/uiProfilingIntegration.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,50 @@ describe('uiProfilingIntegration', () => {
124124
expect(NATIVE.stopProfiler).toHaveBeenCalledTimes(1);
125125
});
126126

127+
it('links a root span that ends before native init to the app start profile', async () => {
128+
NATIVE.startProfiler.mockResolvedValueOnce('app-start-profiler-id');
129+
const client = new TestClient(
130+
getDefaultTestClientOptions({
131+
tracesSampleRate: 1.0,
132+
integrations: [uiProfilingIntegration({ lifecycle: 'trace', startOnAppStart: true })],
133+
}),
134+
);
135+
setCurrentClient(client);
136+
client.init();
137+
138+
startInactiveSpan({ name: 'early' }).end();
139+
// @ts-expect-error Only available on ReactNativeClient
140+
client.emit('afterInit');
141+
await jest.advanceTimersByTimeAsync(0);
142+
143+
expect(getTransaction(client, 'early')?.contexts?.profile).toEqual({ profiler_id: 'app-start-profiler-id' });
144+
expect(NATIVE.stopProfiler).not.toHaveBeenCalled();
145+
146+
await jest.advanceTimersByTimeAsync(TRACE_STOP_DELAY_MS);
147+
expect(NATIVE.stopProfiler).toHaveBeenCalledTimes(1);
148+
});
149+
150+
it('stops the app start profile after the profiler id call when manual stop comes before native init', async () => {
151+
const client = new TestClient(
152+
getDefaultTestClientOptions({
153+
integrations: [uiProfilingIntegration({ lifecycle: 'manual', startOnAppStart: true })],
154+
}),
155+
);
156+
setCurrentClient(client);
157+
client.init();
158+
159+
profiler.stopProfiler();
160+
// @ts-expect-error Only available on ReactNativeClient
161+
client.emit('afterInit');
162+
await jest.advanceTimersByTimeAsync(0);
163+
164+
expect(NATIVE.startProfiler).toHaveBeenCalledTimes(1);
165+
expect(NATIVE.stopProfiler).toHaveBeenCalledTimes(1);
166+
expect(NATIVE.startProfiler.mock.invocationCallOrder[0]).toBeLessThan(
167+
NATIVE.stopProfiler.mock.invocationCallOrder[0] ?? 0,
168+
);
169+
});
170+
127171
it('stops the app start profile when tracing is disabled', () => {
128172
const warn = jest.spyOn(debug, 'warn');
129173

0 commit comments

Comments
 (0)