Skip to content

Commit

Permalink
feat(profiling): Expose profiler as top level primitive (getsentry#13512
Browse files Browse the repository at this point in the history
)

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
JonasBa and lforst committed Aug 30, 2024
1 parent f452423 commit 01165db
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export {
withMonitor,
withScope,
zodErrorsIntegration,
profiler,
} from '@sentry/node';

export { init } from './server/sdk';
Expand Down
1 change: 1 addition & 0 deletions packages/aws-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export {
trpcMiddleware,
addOpenTelemetryInstrumentation,
zodErrorsIntegration,
profiler,
} from '@sentry/node';

export {
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export {
trpcMiddleware,
addOpenTelemetryInstrumentation,
zodErrorsIntegration,
profiler,
} from '@sentry/node';

export {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export { sessionTimingIntegration } from './integrations/sessiontiming';
export { zodErrorsIntegration } from './integrations/zoderrors';
export { thirdPartyErrorFilterIntegration } from './integrations/third-party-errors-filter';
export { metrics } from './metrics/exports';
export { profiler } from './profiling';
export type { MetricData } from '@sentry/types';
export { metricsDefault } from './metrics/exports-default';
export { BrowserMetricsAggregator } from './metrics/browser-aggregator';
Expand Down
72 changes: 72 additions & 0 deletions packages/core/src/profiling.ts
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,
};
1 change: 1 addition & 0 deletions packages/google-cloud-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export {
trpcMiddleware,
addOpenTelemetryInstrumentation,
zodErrorsIntegration,
profiler,
} from '@sentry/node';

export {
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export {
spanToBaggageHeader,
trpcMiddleware,
zodErrorsIntegration,
profiler,
} from '@sentry/core';

export type {
Expand Down
14 changes: 7 additions & 7 deletions packages/profiling-node/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
spanToJSON,
} from '@sentry/core';
import type { NodeClient } from '@sentry/node';
import type { Event, Integration, IntegrationFn, Profile, ProfileChunk, Span } from '@sentry/types';
import type { Event, IntegrationFn, Profile, ProfileChunk, ProfilingIntegration, Span } from '@sentry/types';

import { LRUMap, logger, uuid4 } from '@sentry/utils';

Expand Down Expand Up @@ -159,6 +159,7 @@ interface ChunkData {
timer: NodeJS.Timeout | undefined;
startTraceID: string;
}

class ContinuousProfiler {
private _profilerId = uuid4();
private _client: NodeClient | undefined = undefined;
Expand Down Expand Up @@ -384,12 +385,8 @@ class ContinuousProfiler {
}
}

export interface ProfilingIntegration extends Integration {
_profiler: ContinuousProfiler;
}

/** Exported only for tests. */
export const _nodeProfilingIntegration = ((): ProfilingIntegration => {
export const _nodeProfilingIntegration = ((): ProfilingIntegration<NodeClient> => {
if (DEBUG_BUILD && ![16, 18, 20, 22].includes(NODE_MAJOR)) {
logger.warn(
`[Profiling] You are using a Node.js version that does not have prebuilt binaries (${NODE_VERSION}).`,
Expand All @@ -407,7 +404,10 @@ export const _nodeProfilingIntegration = ((): ProfilingIntegration => {
const options = client.getOptions();

const mode =
(options.profilesSampleRate === undefined || options.profilesSampleRate === 0) && !options.profilesSampler
(options.profilesSampleRate === undefined ||
options.profilesSampleRate === null ||
options.profilesSampleRate === 0) &&
!options.profilesSampler
? 'continuous'
: 'span';
switch (mode) {
Expand Down
63 changes: 47 additions & 16 deletions packages/profiling-node/test/spanProfileUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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');
}
Expand All @@ -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();
Expand All @@ -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', () => {
Expand All @@ -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');
}
Expand All @@ -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');
}
Expand All @@ -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');
}
Expand All @@ -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');
}
Expand All @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
Expand All @@ -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 })],
Expand Down Expand Up @@ -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');
}
Expand All @@ -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);
});
});
5 changes: 3 additions & 2 deletions packages/profiling-node/test/spanProfileUtils.worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ jest.setTimeout(10000);

import * as Sentry from '@sentry/node';
import type { Transport } from '@sentry/types';
import { type ProfilingIntegration, _nodeProfilingIntegration } from '../src/integration';
import { type ProfilingIntegration } from '@sentry/types';
import { _nodeProfilingIntegration } from '../src/integration';

function makeContinuousProfilingClient(): [Sentry.NodeClient, Transport] {
const integration = _nodeProfilingIntegration();
Expand Down Expand Up @@ -49,7 +50,7 @@ it('worker threads context', () => {
},
});

const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration');
const integration = client.getIntegrationByName<ProfilingIntegration<any>>('ProfilingIntegration');
if (!integration) {
throw new Error('Profiling integration not found');
}
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,5 @@ export type {
Metrics,
} from './metrics';
export type { ParameterizedString } from './parameterize';
export type { ContinuousProfiler, ProfilingIntegration, Profiler } from './profiling';
export type { ViewHierarchyData, ViewHierarchyWindow } from './view-hierarchy';
Loading

0 comments on commit 01165db

Please sign in to comment.