-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(profiling): Expose profiler as top level primitive #13512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
188e4c8
ref(profiling): expose continuous profiler on Sentry.profiler
JonasBa ecb8037
ref(profiling) add comment
JonasBa 79731ac
test: fix profiling tests
JonasBa 8bf0f0a
format fix
JonasBa 96dab48
use import type for client
JonasBa f05e42b
ref: runtime safe call to start/stop
JonasBa 2398c25
Add to all node-based packages
lforst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two calls are just thin wrappers around getIntegration and calling a method on that integration.