diff --git a/packages/profiling-node/src/integration.ts b/packages/profiling-node/src/integration.ts index 0766de3ba065..a158019ee036 100644 --- a/packages/profiling-node/src/integration.ts +++ b/packages/profiling-node/src/integration.ts @@ -275,18 +275,12 @@ class ContinuousProfiler { return; } - const transport = this._client.getTransport(); - if (!transport) { - DEBUG_BUILD && debug.log('[Profiling] No transport available to send profile chunk.'); - return; - } - const dsn = this._client.getDsn(); const metadata = this._client.getSdkMetadata(); const tunnel = this._client.getOptions().tunnel; const envelope = makeProfileChunkEnvelope('node', chunk, metadata?.sdk, tunnel, dsn); - transport.send(envelope).then(null, reason => { + this._client.sendEnvelope(envelope).then(null, reason => { DEBUG_BUILD && debug.error('Error while sending profile chunk envelope:', reason); }); } diff --git a/packages/profiling-node/test/integration.test.ts b/packages/profiling-node/test/integration.test.ts index 80a47ec05212..b7c24525419d 100644 --- a/packages/profiling-node/test/integration.test.ts +++ b/packages/profiling-node/test/integration.test.ts @@ -145,6 +145,32 @@ describe('ProfilingIntegration', () => { }), }); }); + + it('emits the `beforeEnvelope` hook for profile chunks', async () => { + const [client, transport] = makeSpanProfilingClient({ + profileLifecycle: 'manual', + profileSessionSampleRate: 1, + }); + + Sentry.setCurrentClient(client); + client.init(); + + vi.spyOn(transport, 'send').mockReturnValue(Promise.resolve({})); + + const beforeEnvelope = vi.fn(); + client.on('beforeEnvelope', beforeEnvelope); + + Sentry.profiler.startProfiler(); + await wait(1000); + Sentry.profiler.stopProfiler(); + + await Sentry.flush(1000); + + expect(beforeEnvelope).toHaveBeenCalledTimes(1); + expect(beforeEnvelope.mock.calls[0]?.[0]?.[1]?.[0]?.[0]).toMatchObject({ + type: 'profile_chunk', + }); + }); }); });