Skip to content

feat(v8/profiling-node): Use OTEL powered node package #11033

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/profiling-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
},
"devDependencies": {
"@sentry/core": "8.0.0-alpha.2",
"@sentry/node-experimental": "8.0.0-alpha.2",
"@sentry/node": "8.0.0-alpha.2",
"@sentry/types": "8.0.0-alpha.2",
"@sentry/utils": "8.0.0-alpha.2",
"@types/node": "16.18.70",
Expand Down
3 changes: 2 additions & 1 deletion packages/profiling-node/rollup.npm.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import { makeBaseNPMConfig } from '@sentry-internal/rollup-utils';
import { makeJsonPlugin } from '@sentry-internal/rollup-utils/plugins/index.mjs';

export default makeBaseNPMConfig({
packageSpecificConfig: {
input: 'src/index.ts',
output: { file: 'lib/index.js', format: 'cjs', dir: undefined, preserveModules: false },
plugins: [resolve(), commonjs(), typescript({ tsconfig: './tsconfig.json' })],
plugins: [resolve(), makeJsonPlugin(), commonjs(), typescript({ tsconfig: './tsconfig.json' })],
},
});
2 changes: 1 addition & 1 deletion packages/profiling-node/src/integration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineIntegration, getCurrentScope, getRootSpan, spanToJSON } from '@sentry/core';
import type { NodeClient } from '@sentry/node-experimental';
import type { NodeClient } from '@sentry/node';
import type { IntegrationFn, Span } from '@sentry/types';

import { logger } from '@sentry/utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/profiling-node/src/spanProfileUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spanIsSampled, spanToJSON } from '@sentry/core';
import type { NodeClient } from '@sentry/node-experimental';
import type { NodeClient } from '@sentry/node';
import type { CustomSamplingContext, Span } from '@sentry/types';
import { logger, uuid4 } from '@sentry/utils';

Expand Down
4 changes: 2 additions & 2 deletions packages/profiling-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Context, Envelope, Event, StackFrame, StackParser } from '@sentry/
import { env, versions } from 'process';
import { isMainThread, threadId } from 'worker_threads';

import * as Sentry from '@sentry/node-experimental';
import { getCurrentHub } from '@sentry/node';
import { GLOBAL_OBJ, forEachEnvelopeItem, logger } from '@sentry/utils';

import { DEBUG_BUILD } from './debug-build';
Expand Down Expand Up @@ -318,7 +318,7 @@ export function applyDebugMetadata(resource_paths: ReadonlyArray<string>): Debug
}

// eslint-disable-next-line deprecation/deprecation
const hub = Sentry.getCurrentHub();
const hub = getCurrentHub();
// eslint-disable-next-line deprecation/deprecation
const client = hub.getClient();
const options = client && client.getOptions();
Expand Down
2 changes: 1 addition & 1 deletion packages/profiling-node/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EventEmitter } from 'events';

import type { Transport } from '@sentry/types';

import type { NodeClient } from '@sentry/node-experimental';
import type { NodeClient } from '@sentry/node';
import { _nodeProfilingIntegration } from '../src/integration';

describe('ProfilingIntegration', () => {
Expand Down
71 changes: 39 additions & 32 deletions packages/profiling-node/test/spanProfileUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import * as Sentry from '@sentry/node-experimental';
import {
NodeClient,
defaultStackParser,
flush,
makeNodeTransport,
setCurrentClient,
startInactiveSpan,
} from '@sentry/node';

import { getMainCarrier } from '@sentry/core';
import type { Transport } from '@sentry/types';
import { GLOBAL_OBJ, createEnvelope, logger } from '@sentry/utils';
import { CpuProfilerBindings } from '../src/cpu_profiler';
import { _nodeProfilingIntegration } from '../src/integration';

function makeClientWithHooks(): [Sentry.NodeClient, Transport] {
function makeClientWithHooks(): [NodeClient, Transport] {
const integration = _nodeProfilingIntegration();
const client = new Sentry.NodeClient({
stackParser: Sentry.defaultStackParser,
const client = new NodeClient({
stackParser: defaultStackParser,
tracesSampleRate: 1,
profilesSampleRate: 1,
debug: true,
environment: 'test-environment',
dsn: 'https://7fa19397baaf433f919fbe02228d5470@o1137848.ingest.sentry.io/6625302',
integrations: [integration],
transport: _opts =>
Sentry.makeNodeTransport({
makeNodeTransport({
url: 'https://7fa19397baaf433f919fbe02228d5470@o1137848.ingest.sentry.io/6625302',
recordDroppedEvent: () => {
return undefined;
Expand Down Expand Up @@ -45,24 +52,24 @@ describe('spanProfileUtils', () => {

it('pulls environment from sdk init', async () => {
const [client, transport] = makeClientWithHooks();
Sentry.setCurrentClient(client);
setCurrentClient(client);
client.init();

const transportSpy = jest.spyOn(transport, 'send').mockReturnValue(Promise.resolve({}));

const transaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
const transaction = startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
await wait(500);
transaction.end();

await Sentry.flush(1000);
await flush(1000);
expect(transportSpy.mock.calls?.[0]?.[0]?.[1]?.[0]?.[1]).toMatchObject({ environment: 'test-environment' });
});

it('logger warns user if there are insufficient samples and discards the profile', async () => {
const logSpy = jest.spyOn(logger, 'log');

const [client, transport] = makeClientWithHooks();
Sentry.setCurrentClient(client);
setCurrentClient(client);
client.init();

jest.spyOn(CpuProfilerBindings, 'stopProfiling').mockImplementation(() => {
Expand All @@ -84,10 +91,10 @@ describe('spanProfileUtils', () => {

jest.spyOn(transport, 'send').mockReturnValue(Promise.resolve({}));

const transaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
const transaction = startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
transaction.end();

await Sentry.flush(1000);
await flush(1000);

expect(logSpy).toHaveBeenCalledWith('[Profiling] Discarding profile because it contains less than 2 samples');

Expand All @@ -100,7 +107,7 @@ describe('spanProfileUtils', () => {
const logSpy = jest.spyOn(logger, 'log');

const [client, transport] = makeClientWithHooks();
Sentry.setCurrentClient(client);
setCurrentClient(client);
client.init();

jest.spyOn(CpuProfilerBindings, 'stopProfiling').mockImplementation(() => {
Expand All @@ -127,48 +134,48 @@ describe('spanProfileUtils', () => {

jest.spyOn(transport, 'send').mockReturnValue(Promise.resolve({}));

const transaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub', traceId: 'boop' });
const transaction = startInactiveSpan({ forceTransaction: true, name: 'profile_hub', traceId: 'boop' });
await wait(500);
transaction.end();

await Sentry.flush(1000);
await flush(1000);

expect(logSpy).toHaveBeenCalledWith('[Profiling] Invalid traceId: ' + 'boop' + ' on profiled event');
});

describe('with hooks', () => {
it('calls profiler when transaction is started/stopped', async () => {
const [client, transport] = makeClientWithHooks();
Sentry.setCurrentClient(client);
setCurrentClient(client);
client.init();

const startProfilingSpy = jest.spyOn(CpuProfilerBindings, 'startProfiling');
const stopProfilingSpy = jest.spyOn(CpuProfilerBindings, 'stopProfiling');

jest.spyOn(transport, 'send').mockReturnValue(Promise.resolve({}));

const transaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
const transaction = startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
await wait(500);
transaction.end();

await Sentry.flush(1000);
await flush(1000);

expect(startProfilingSpy).toHaveBeenCalledTimes(1);
expect((stopProfilingSpy.mock.calls[stopProfilingSpy.mock.calls.length - 1]?.[0] as string).length).toBe(32);
});

it('sends profile in the same envelope as transaction', async () => {
const [client, transport] = makeClientWithHooks();
Sentry.setCurrentClient(client);
setCurrentClient(client);
client.init();

const transportSpy = jest.spyOn(transport, 'send').mockReturnValue(Promise.resolve({}));

const transaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
const transaction = startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
await wait(500);
transaction.end();

await Sentry.flush(1000);
await flush(1000);

// One for profile, the other for transaction
expect(transportSpy).toHaveBeenCalledTimes(1);
Expand All @@ -177,7 +184,7 @@ describe('spanProfileUtils', () => {

it('does not crash if transaction has no profile context or it is invalid', async () => {
const [client] = makeClientWithHooks();
Sentry.setCurrentClient(client);
setCurrentClient(client);
client.init();

// @ts-expect-error transaction is partial
Expand All @@ -201,7 +208,7 @@ describe('spanProfileUtils', () => {

it('if transaction was profiled, but profiler returned null', async () => {
const [client, transport] = makeClientWithHooks();
Sentry.setCurrentClient(client);
setCurrentClient(client);
client.init();

jest.spyOn(CpuProfilerBindings, 'stopProfiling').mockReturnValue(null);
Expand All @@ -211,11 +218,11 @@ describe('spanProfileUtils', () => {
return Promise.resolve({});
});

const transaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
const transaction = startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
await wait(500);
transaction.end();

await Sentry.flush(1000);
await flush(1000);

// Only transaction is sent
expect(transportSpy.mock.calls?.[0]?.[0]?.[1]?.[0]?.[0]).toMatchObject({ type: 'transaction' });
Expand All @@ -224,18 +231,18 @@ describe('spanProfileUtils', () => {

it('emits preprocessEvent for profile', async () => {
const [client] = makeClientWithHooks();
Sentry.setCurrentClient(client);
setCurrentClient(client);
client.init();

const onPreprocessEvent = jest.fn();

client.on('preprocessEvent', onPreprocessEvent);

const transaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
const transaction = startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
await wait(500);
transaction.end();

await Sentry.flush(1000);
await flush(1000);

expect(onPreprocessEvent.mock.calls[1][0]).toMatchObject({
profile: {
Expand All @@ -250,10 +257,10 @@ describe('spanProfileUtils', () => {
const stopProfilingSpy = jest.spyOn(CpuProfilerBindings, 'stopProfiling');

const [client] = makeClientWithHooks();
Sentry.setCurrentClient(client);
setCurrentClient(client);
client.init();

const transaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'txn' });
const transaction = startInactiveSpan({ forceTransaction: true, name: 'txn' });
transaction.end();
transaction.end();
expect(stopProfilingSpy).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -289,16 +296,16 @@ describe('spanProfileUtils', () => {
});

const [client, transport] = makeClientWithHooks();
Sentry.setCurrentClient(client);
setCurrentClient(client);
client.init();

const transportSpy = jest.spyOn(transport, 'send').mockReturnValue(Promise.resolve({}));

const transaction = Sentry.startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
const transaction = startInactiveSpan({ forceTransaction: true, name: 'profile_hub' });
await wait(500);
transaction.end();

await Sentry.flush(1000);
await flush(1000);

expect(transportSpy.mock.calls?.[0]?.[0]?.[1]?.[1]?.[1]).toMatchObject({
debug_meta: {
Expand Down