-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(profiling): Expose profiler as top level primitive (#13512)
We are about to enter a public beta for continuous profiling, which means it is time to expose this from under the wraps of the integration and align it with how the profiler is exposed in python and iOS SDKs --------- Co-authored-by: Luca Forstner <[email protected]>
- Loading branch information
Showing
12 changed files
with
153 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { Profiler, ProfilingIntegration } from '@sentry/types'; | ||
import { logger } from '@sentry/utils'; | ||
|
||
import { getClient } from './currentScopes'; | ||
import { DEBUG_BUILD } from './debug-build'; | ||
|
||
function isProfilingIntegrationWithProfiler( | ||
integration: ProfilingIntegration<any> | undefined, | ||
): integration is ProfilingIntegration<any> { | ||
return ( | ||
!!integration && | ||
typeof integration['_profiler'] !== 'undefined' && | ||
typeof integration['_profiler']['start'] === 'function' && | ||
typeof integration['_profiler']['stop'] === 'function' | ||
); | ||
} | ||
/** | ||
* Starts the Sentry continuous profiler. | ||
* This mode is exclusive with the transaction profiler and will only work if the profilesSampleRate is set to a falsy value. | ||
* In continuous profiling mode, the profiler will keep reporting profile chunks to Sentry until it is stopped, which allows for continuous profiling of the application. | ||
*/ | ||
function startProfiler(): void { | ||
const client = getClient(); | ||
if (!client) { | ||
DEBUG_BUILD && logger.warn('No Sentry client available, profiling is not started'); | ||
return; | ||
} | ||
|
||
const integration = client.getIntegrationByName<ProfilingIntegration<any>>('ProfilingIntegration'); | ||
|
||
if (!integration) { | ||
DEBUG_BUILD && logger.warn('ProfilingIntegration is not available'); | ||
return; | ||
} | ||
|
||
if (!isProfilingIntegrationWithProfiler(integration)) { | ||
DEBUG_BUILD && logger.warn('Profiler is not available on profiling integration.'); | ||
return; | ||
} | ||
|
||
integration._profiler.start(); | ||
} | ||
|
||
/** | ||
* Stops the Sentry continuous profiler. | ||
* Calls to stop will stop the profiler and flush the currently collected profile data to Sentry. | ||
*/ | ||
function stopProfiler(): void { | ||
const client = getClient(); | ||
if (!client) { | ||
DEBUG_BUILD && logger.warn('No Sentry client available, profiling is not started'); | ||
return; | ||
} | ||
|
||
const integration = client.getIntegrationByName<ProfilingIntegration<any>>('ProfilingIntegration'); | ||
if (!integration) { | ||
DEBUG_BUILD && logger.warn('ProfilingIntegration is not available'); | ||
return; | ||
} | ||
|
||
if (!isProfilingIntegrationWithProfiler(integration)) { | ||
DEBUG_BUILD && logger.warn('Profiler is not available on profiling integration.'); | ||
return; | ||
} | ||
|
||
integration._profiler.stop(); | ||
} | ||
|
||
export const profiler: Profiler = { | ||
startProfiler, | ||
stopProfiler, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,11 @@ import * as Sentry from '@sentry/node'; | |
|
||
import { getMainCarrier } from '@sentry/core'; | ||
import type { NodeClientOptions } from '@sentry/node/build/types/types'; | ||
import type { ProfilingIntegration } from '@sentry/types'; | ||
import type { ProfileChunk, Transport } from '@sentry/types'; | ||
import { GLOBAL_OBJ, createEnvelope, logger } from '@sentry/utils'; | ||
import { CpuProfilerBindings } from '../src/cpu_profiler'; | ||
import { type ProfilingIntegration, _nodeProfilingIntegration } from '../src/integration'; | ||
import { _nodeProfilingIntegration } from '../src/integration'; | ||
|
||
function makeClientWithHooks(): [Sentry.NodeClient, Transport] { | ||
const integration = _nodeProfilingIntegration(); | ||
|
@@ -299,7 +300,7 @@ describe('automated span instrumentation', () => { | |
Sentry.setCurrentClient(client); | ||
client.init(); | ||
|
||
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration'); | ||
const integration = client.getIntegrationByName<ProfilingIntegration<Sentry.NodeClient>>('ProfilingIntegration'); | ||
if (!integration) { | ||
throw new Error('Profiling integration not found'); | ||
} | ||
|
@@ -390,7 +391,7 @@ describe('continuous profiling', () => { | |
}); | ||
afterEach(() => { | ||
const client = Sentry.getClient(); | ||
const integration = client?.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration'); | ||
const integration = client?.getIntegrationByName<ProfilingIntegration<Sentry.NodeClient>>('ProfilingIntegration'); | ||
|
||
if (integration) { | ||
integration._profiler.stop(); | ||
|
@@ -432,7 +433,7 @@ describe('continuous profiling', () => { | |
|
||
const transportSpy = jest.spyOn(transport, 'send').mockReturnValue(Promise.resolve({})); | ||
|
||
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration'); | ||
const integration = client.getIntegrationByName<ProfilingIntegration<Sentry.NodeClient>>('ProfilingIntegration'); | ||
if (!integration) { | ||
throw new Error('Profiling integration not found'); | ||
} | ||
|
@@ -446,7 +447,7 @@ describe('continuous profiling', () => { | |
expect(profile.client_sdk.version).toEqual(expect.stringMatching(/\d+\.\d+\.\d+/)); | ||
}); | ||
|
||
it('initializes the continuous profiler and binds the sentry client', () => { | ||
it('initializes the continuous profiler', () => { | ||
const startProfilingSpy = jest.spyOn(CpuProfilerBindings, 'startProfiling'); | ||
|
||
const [client] = makeContinuousProfilingClient(); | ||
|
@@ -455,14 +456,13 @@ describe('continuous profiling', () => { | |
|
||
expect(startProfilingSpy).not.toHaveBeenCalledTimes(1); | ||
|
||
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration'); | ||
const integration = client.getIntegrationByName<ProfilingIntegration<Sentry.NodeClient>>('ProfilingIntegration'); | ||
if (!integration) { | ||
throw new Error('Profiling integration not found'); | ||
} | ||
integration._profiler.start(); | ||
|
||
expect(integration._profiler).toBeDefined(); | ||
expect(integration._profiler['_client']).toBe(client); | ||
}); | ||
|
||
it('starts a continuous profile', () => { | ||
|
@@ -473,7 +473,7 @@ describe('continuous profiling', () => { | |
client.init(); | ||
|
||
expect(startProfilingSpy).not.toHaveBeenCalledTimes(1); | ||
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration'); | ||
const integration = client.getIntegrationByName<ProfilingIntegration<Sentry.NodeClient>>('ProfilingIntegration'); | ||
if (!integration) { | ||
throw new Error('Profiling integration not found'); | ||
} | ||
|
@@ -490,7 +490,7 @@ describe('continuous profiling', () => { | |
client.init(); | ||
|
||
expect(startProfilingSpy).not.toHaveBeenCalledTimes(1); | ||
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration'); | ||
const integration = client.getIntegrationByName<ProfilingIntegration<Sentry.NodeClient>>('ProfilingIntegration'); | ||
if (!integration) { | ||
throw new Error('Profiling integration not found'); | ||
} | ||
|
@@ -509,7 +509,7 @@ describe('continuous profiling', () => { | |
client.init(); | ||
|
||
expect(startProfilingSpy).not.toHaveBeenCalledTimes(1); | ||
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration'); | ||
const integration = client.getIntegrationByName<ProfilingIntegration<Sentry.NodeClient>>('ProfilingIntegration'); | ||
if (!integration) { | ||
throw new Error('Profiling integration not found'); | ||
} | ||
|
@@ -529,7 +529,7 @@ describe('continuous profiling', () => { | |
client.init(); | ||
|
||
expect(startProfilingSpy).not.toHaveBeenCalledTimes(1); | ||
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration'); | ||
const integration = client.getIntegrationByName<ProfilingIntegration<Sentry.NodeClient>>('ProfilingIntegration'); | ||
if (!integration) { | ||
throw new Error('Profiling integration not found'); | ||
} | ||
|
@@ -548,7 +548,7 @@ describe('continuous profiling', () => { | |
client.init(); | ||
|
||
expect(startProfilingSpy).not.toHaveBeenCalledTimes(1); | ||
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration'); | ||
const integration = client.getIntegrationByName<ProfilingIntegration<Sentry.NodeClient>>('ProfilingIntegration'); | ||
if (!integration) { | ||
throw new Error('Profiling integration not found'); | ||
} | ||
|
@@ -604,7 +604,7 @@ describe('continuous profiling', () => { | |
|
||
const transportSpy = jest.spyOn(transport, 'send').mockReturnValue(Promise.resolve({})); | ||
|
||
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration'); | ||
const integration = client.getIntegrationByName<ProfilingIntegration<Sentry.NodeClient>>('ProfilingIntegration'); | ||
if (!integration) { | ||
throw new Error('Profiling integration not found'); | ||
} | ||
|
@@ -632,7 +632,7 @@ describe('continuous profiling', () => { | |
}, | ||
}); | ||
|
||
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration'); | ||
const integration = client.getIntegrationByName<ProfilingIntegration<Sentry.NodeClient>>('ProfilingIntegration'); | ||
if (!integration) { | ||
throw new Error('Profiling integration not found'); | ||
} | ||
|
@@ -692,7 +692,7 @@ describe('span profiling mode', () => { | |
Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' }); | ||
|
||
expect(startProfilingSpy).toHaveBeenCalled(); | ||
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration'); | ||
const integration = client.getIntegrationByName<ProfilingIntegration<Sentry.NodeClient>>('ProfilingIntegration'); | ||
|
||
if (!integration) { | ||
throw new Error('Profiling integration not found'); | ||
|
@@ -703,6 +703,10 @@ describe('span profiling mode', () => { | |
}); | ||
}); | ||
describe('continuous profiling mode', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it.each([ | ||
['profilesSampleRate=0', makeClientOptions({ profilesSampleRate: 0 })], | ||
['profilesSampleRate=undefined', makeClientOptions({ profilesSampleRate: undefined })], | ||
|
@@ -739,7 +743,7 @@ describe('continuous profiling mode', () => { | |
|
||
jest.spyOn(transport, 'send').mockReturnValue(Promise.resolve({})); | ||
|
||
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration'); | ||
const integration = client.getIntegrationByName<ProfilingIntegration<Sentry.NodeClient>>('ProfilingIntegration'); | ||
if (!integration) { | ||
throw new Error('Profiling integration not found'); | ||
} | ||
|
@@ -750,4 +754,31 @@ describe('continuous profiling mode', () => { | |
Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' }); | ||
expect(startProfilingSpy).toHaveBeenCalledTimes(callCount); | ||
}); | ||
|
||
it('top level methods proxy to integration', () => { | ||
const client = new Sentry.NodeClient({ | ||
...makeClientOptions({ profilesSampleRate: undefined }), | ||
dsn: 'https://[email protected]/6625302', | ||
tracesSampleRate: 1, | ||
transport: _opts => | ||
Sentry.makeNodeTransport({ | ||
url: 'https://[email protected]/6625302', | ||
recordDroppedEvent: () => { | ||
return undefined; | ||
}, | ||
}), | ||
integrations: [_nodeProfilingIntegration()], | ||
}); | ||
|
||
Sentry.setCurrentClient(client); | ||
client.init(); | ||
|
||
const startProfilingSpy = jest.spyOn(CpuProfilerBindings, 'startProfiling'); | ||
const stopProfilingSpy = jest.spyOn(CpuProfilerBindings, 'stopProfiling'); | ||
|
||
Sentry.profiler.startProfiler(); | ||
expect(startProfilingSpy).toHaveBeenCalledTimes(1); | ||
Sentry.profiler.stopProfiler(); | ||
expect(stopProfilingSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.