Skip to content

Commit b75aa9e

Browse files
authored
fix(profiling-node): Send profile chunks with client.sendEnvelope (#24896)
The continuous profiler sent `profile_chunk` envelopes straight to the transport, so the `beforeEnvelope` hook never ran for them. It now uses `client.sendEnvelope()`, as the browser `UIProfiler` does. - Ref. getsentry/sentry-electron#1445
1 parent df2b796 commit b75aa9e

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

‎packages/profiling-node/src/integration.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,18 +275,12 @@ class ContinuousProfiler {
275275
return;
276276
}
277277

278-
const transport = this._client.getTransport();
279-
if (!transport) {
280-
DEBUG_BUILD && debug.log('[Profiling] No transport available to send profile chunk.');
281-
return;
282-
}
283-
284278
const dsn = this._client.getDsn();
285279
const metadata = this._client.getSdkMetadata();
286280
const tunnel = this._client.getOptions().tunnel;
287281

288282
const envelope = makeProfileChunkEnvelope('node', chunk, metadata?.sdk, tunnel, dsn);
289-
transport.send(envelope).then(null, reason => {
283+
this._client.sendEnvelope(envelope).then(null, reason => {
290284
DEBUG_BUILD && debug.error('Error while sending profile chunk envelope:', reason);
291285
});
292286
}

‎packages/profiling-node/test/integration.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,32 @@ describe('ProfilingIntegration', () => {
145145
}),
146146
});
147147
});
148+
149+
it('emits the `beforeEnvelope` hook for profile chunks', async () => {
150+
const [client, transport] = makeSpanProfilingClient({
151+
profileLifecycle: 'manual',
152+
profileSessionSampleRate: 1,
153+
});
154+
155+
Sentry.setCurrentClient(client);
156+
client.init();
157+
158+
vi.spyOn(transport, 'send').mockReturnValue(Promise.resolve({}));
159+
160+
const beforeEnvelope = vi.fn();
161+
client.on('beforeEnvelope', beforeEnvelope);
162+
163+
Sentry.profiler.startProfiler();
164+
await wait(1000);
165+
Sentry.profiler.stopProfiler();
166+
167+
await Sentry.flush(1000);
168+
169+
expect(beforeEnvelope).toHaveBeenCalledTimes(1);
170+
expect(beforeEnvelope.mock.calls[0]?.[0]?.[1]?.[0]?.[0]).toMatchObject({
171+
type: 'profile_chunk',
172+
});
173+
});
148174
});
149175
});
150176

0 commit comments

Comments
 (0)