Skip to content
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

meta(changelog): Update changelog for 7.72.0 #9118

Merged
merged 7 commits into from
Sep 26, 2023
Merged
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
49 changes: 49 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,55 @@

- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott

## 7.72.0

### Important Changes

- **feat(node): App Not Responding with stack traces (#9079)**

This release introduces support for Application Not Responding (ANR) errors for Node.js applications.
These errors are triggered when the Node.js main thread event loop of an application is blocked for more than five seconds.
The Node SDK reports ANR errors as Sentry events and can optionally attach a stacktrace of the blocking code to the ANR event.

To enable ANR detection, import and use the `enableANRDetection` function from the `@sentry/node` package before you run the rest of your application code.
Any event loop blocking before calling `enableANRDetection` will not be detected by the SDK.

Example (ESM):

```ts
import * as Sentry from "@sentry/node";

Sentry.init({
dsn: "___PUBLIC_DSN___",
tracesSampleRate: 1.0,
});

await Sentry.enableANRDetection({ captureStackTrace: true });
// Function that runs your app
runApp();
```

Example (CJS):

```ts
const Sentry = require("@sentry/node");

Sentry.init({
dsn: "___PUBLIC_DSN___",
tracesSampleRate: 1.0,
});

Sentry.enableANRDetection({ captureStackTrace: true }).then(() => {
// Function that runs your app
runApp();
});
```

### Other Changes

- fix(nextjs): Filter `RequestAsyncStorage` locations by locations that webpack will resolve (#9114)
- fix(replay): Ensure `replay_id` is not captured when session is expired (#9109)

## 7.71.0

- feat(bun): Instrument Bun.serve (#9080)
Expand Down
49 changes: 29 additions & 20 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export function constructWebpackConfigFunction(
pageExtensionRegex,
excludeServerRoutes: userSentryOptions.excludeServerRoutes,
sentryConfigFilePath: getUserConfigFilePath(projectDir, runtime),
nextjsRequestAsyncStorageModulePath: getRequestAsyncLocalStorageModuleLocation(rawNewConfig.resolve?.modules),
nextjsRequestAsyncStorageModulePath: getRequestAsyncStorageModuleLocation(rawNewConfig.resolve?.modules),
};

const normalizeLoaderResourcePath = (resourcePath: string): string => {
Expand Down Expand Up @@ -977,30 +977,39 @@ function addValueInjectionLoader(
);
}

function getRequestAsyncLocalStorageModuleLocation(modules: string[] | undefined): string | undefined {
if (modules === undefined) {
function getRequestAsyncStorageModuleLocation(
webpackResolvableModuleLocations: string[] | undefined,
): string | undefined {
if (webpackResolvableModuleLocations === undefined) {
return undefined;
}

try {
// Original location of that module
// https://github.com/vercel/next.js/blob/46151dd68b417e7850146d00354f89930d10b43b/packages/next/src/client/components/request-async-storage.ts
const location = 'next/dist/client/components/request-async-storage';
require.resolve(location, { paths: modules });
return location;
} catch {
// noop
}
const absoluteWebpackResolvableModuleLocations = webpackResolvableModuleLocations.map(m => path.resolve(m));
const moduleIsWebpackResolvable = (moduleId: string): boolean => {
let requireResolveLocation: string;
try {
// This will throw if the location is not resolvable at all.
// We provide a `paths` filter in order to maximally limit the potential locations to the locations webpack would check.
requireResolveLocation = require.resolve(moduleId, { paths: webpackResolvableModuleLocations });
} catch {
return false;
}

try {
// Since the require.resolve approach still looks in "global" node_modules locations like for example "/user/lib/node"
// we further need to filter by locations that start with the locations that webpack would check for.
return absoluteWebpackResolvableModuleLocations.some(resolvableModuleLocation =>
requireResolveLocation.startsWith(resolvableModuleLocation),
);
};

const potentialRequestAsyncStorageLocations = [
// Original location of RequestAsyncStorage
// https://github.com/vercel/next.js/blob/46151dd68b417e7850146d00354f89930d10b43b/packages/next/src/client/components/request-async-storage.ts
'next/dist/client/components/request-async-storage',
// Introduced in Next.js 13.4.20
// https://github.com/vercel/next.js/blob/e1bc270830f2fc2df3542d4ef4c61b916c802df3/packages/next/src/client/components/request-async-storage.external.ts
const location = 'next/dist/client/components/request-async-storage.external';
require.resolve(location, { paths: modules });
return location;
} catch {
// noop
}
'next/dist/client/components/request-async-storage.external',
];

return undefined;
return potentialRequestAsyncStorageLocations.find(potentialLocation => moduleIsWebpackResolvable(potentialLocation));
}
1 change: 1 addition & 0 deletions packages/node-experimental/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@opentelemetry/instrumentation-mysql2": "~0.34.1",
"@opentelemetry/instrumentation-nestjs-core": "~0.33.1",
"@opentelemetry/instrumentation-pg": "~0.36.1",
"@opentelemetry/resources": "~1.17.0",
"@opentelemetry/sdk-trace-base": "~1.17.0",
"@opentelemetry/semantic-conventions": "~1.17.0",
"@prisma/instrumentation": "~5.3.1",
Expand Down
9 changes: 8 additions & 1 deletion packages/node-experimental/src/sdk/initOtel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { diag, DiagLogLevel } from '@opentelemetry/api';
import { Resource } from '@opentelemetry/resources';
import { AlwaysOnSampler, BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
import { getCurrentHub } from '@sentry/core';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { getCurrentHub, SDK_VERSION } from '@sentry/core';
import { SentryPropagator, SentrySpanProcessor } from '@sentry/opentelemetry-node';
import { logger } from '@sentry/utils';

Expand Down Expand Up @@ -28,6 +30,11 @@ export function initOtel(): () => void {
// Create and configure NodeTracerProvider
const provider = new BasicTracerProvider({
sampler: new AlwaysOnSampler(),
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'node-experimental',
[SemanticResourceAttributes.SERVICE_NAMESPACE]: 'sentry',
[SemanticResourceAttributes.SERVICE_VERSION]: SDK_VERSION,
}),
});
provider.addSpanProcessor(new SentrySpanProcessor());

Expand Down
31 changes: 31 additions & 0 deletions packages/node-integration-tests/suites/anr/scenario.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const crypto = require('crypto');

const Sentry = require('@sentry/node');

// close both processes after 5 seconds
setTimeout(() => {
process.exit();
}, 5000);

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
beforeSend: event => {
// eslint-disable-next-line no-console
console.log(JSON.stringify(event));
},
});

Sentry.enableAnrDetection({ captureStackTrace: true, anrThreshold: 200, debug: true }).then(() => {
function longWork() {
for (let i = 0; i < 100; i++) {
const salt = crypto.randomBytes(128).toString('base64');
// eslint-disable-next-line no-unused-vars
const hash = crypto.pbkdf2Sync('myPassword', salt, 10000, 512, 'sha512');
}
}

setTimeout(() => {
longWork();
}, 1000);
});
31 changes: 31 additions & 0 deletions packages/node-integration-tests/suites/anr/scenario.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as crypto from 'crypto';

import * as Sentry from '@sentry/node';

// close both processes after 5 seconds
setTimeout(() => {
process.exit();
}, 5000);

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
beforeSend: event => {
// eslint-disable-next-line no-console
console.log(JSON.stringify(event));
},
});

await Sentry.enableAnrDetection({ captureStackTrace: true, anrThreshold: 200, debug: true });

function longWork() {
for (let i = 0; i < 100; i++) {
const salt = crypto.randomBytes(128).toString('base64');
// eslint-disable-next-line no-unused-vars
const hash = crypto.pbkdf2Sync('myPassword', salt, 10000, 512, 'sha512');
}
}

setTimeout(() => {
longWork();
}, 1000);
57 changes: 57 additions & 0 deletions packages/node-integration-tests/suites/anr/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { Event } from '@sentry/node';
import { parseSemver } from '@sentry/utils';
import * as childProcess from 'child_process';
import * as path from 'path';

const NODE_VERSION = parseSemver(process.versions.node).major || 0;

describe('should report ANR when event loop blocked', () => {
test('CJS', done => {
// The stack trace is different when node < 12
const testFramesDetails = NODE_VERSION >= 12;

expect.assertions(testFramesDetails ? 6 : 4);

const testScriptPath = path.resolve(__dirname, 'scenario.js');

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (_, stdout) => {
const event = JSON.parse(stdout) as Event;

expect(event.exception?.values?.[0].mechanism).toEqual({ type: 'ANR' });
expect(event.exception?.values?.[0].type).toEqual('ApplicationNotResponding');
expect(event.exception?.values?.[0].value).toEqual('Application Not Responding for at least 200 ms');
expect(event.exception?.values?.[0].stacktrace?.frames?.length).toBeGreaterThan(4);

if (testFramesDetails) {
expect(event.exception?.values?.[0].stacktrace?.frames?.[2].function).toEqual('?');
expect(event.exception?.values?.[0].stacktrace?.frames?.[3].function).toEqual('longWork');
}

done();
});
});

test('ESM', done => {
if (NODE_VERSION < 14) {
done();
return;
}

expect.assertions(6);

const testScriptPath = path.resolve(__dirname, 'scenario.mjs');

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (_, stdout) => {
const event = JSON.parse(stdout) as Event;

expect(event.exception?.values?.[0].mechanism).toEqual({ type: 'ANR' });
expect(event.exception?.values?.[0].type).toEqual('ApplicationNotResponding');
expect(event.exception?.values?.[0].value).toEqual('Application Not Responding for at least 200 ms');
expect(event.exception?.values?.[0].stacktrace?.frames?.length).toBeGreaterThan(4);
expect(event.exception?.values?.[0].stacktrace?.frames?.[2].function).toEqual('?');
expect(event.exception?.values?.[0].stacktrace?.frames?.[3].function).toEqual('longWork');

done();
});
});
});
95 changes: 95 additions & 0 deletions packages/node/src/anr/debugger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import type { StackFrame } from '@sentry/types';
import { dropUndefinedKeys, filenameIsInApp } from '@sentry/utils';
import type { Debugger } from 'inspector';

import { getModuleFromFilename } from '../module';
import { createWebSocketClient } from './websocket';

/**
* Converts Debugger.CallFrame to Sentry StackFrame
*/
function callFrameToStackFrame(
frame: Debugger.CallFrame,
filenameFromScriptId: (id: string) => string | undefined,
): StackFrame {
const filename = filenameFromScriptId(frame.location.scriptId)?.replace(/^file:\/\//, '');

// CallFrame row/col are 0 based, whereas StackFrame are 1 based
const colno = frame.location.columnNumber ? frame.location.columnNumber + 1 : undefined;
const lineno = frame.location.lineNumber ? frame.location.lineNumber + 1 : undefined;

return dropUndefinedKeys({
filename,
module: getModuleFromFilename(filename),
function: frame.functionName || '?',
colno,
lineno,
in_app: filename ? filenameIsInApp(filename) : undefined,
});
}

// The only messages we care about
type DebugMessage =
| {
method: 'Debugger.scriptParsed';
params: Debugger.ScriptParsedEventDataType;
}
| { method: 'Debugger.paused'; params: Debugger.PausedEventDataType };

/**
* Wraps a websocket connection with the basic logic of the Node debugger protocol.
* @param url The URL to connect to
* @param onMessage A callback that will be called with each return message from the debugger
* @returns A function that can be used to send commands to the debugger
*/
async function webSocketDebugger(
url: string,
onMessage: (message: DebugMessage) => void,
): Promise<(method: string, params?: unknown) => void> {
let id = 0;
const webSocket = await createWebSocketClient(url);

webSocket.on('message', (data: Buffer) => {
const message = JSON.parse(data.toString()) as DebugMessage;
onMessage(message);
});

return (method: string, params?: unknown) => {
webSocket.send(JSON.stringify({ id: id++, method, params }));
};
}

/**
* Captures stack traces from the Node debugger.
* @param url The URL to connect to
* @param callback A callback that will be called with the stack frames
* @returns A function that triggers the debugger to pause and capture a stack trace
*/
export async function captureStackTrace(url: string, callback: (frames: StackFrame[]) => void): Promise<() => void> {
// Collect scriptId -> url map so we can look up the filenames later
const scripts = new Map<string, string>();

const sendCommand = await webSocketDebugger(url, message => {
if (message.method === 'Debugger.scriptParsed') {
scripts.set(message.params.scriptId, message.params.url);
} else if (message.method === 'Debugger.paused') {
// copy the frames
const callFrames = [...message.params.callFrames];
// and resume immediately!
sendCommand('Debugger.resume');
sendCommand('Debugger.disable');

const frames = callFrames
.map(frame => callFrameToStackFrame(frame, id => scripts.get(id)))
// Sentry expects the frames to be in the opposite order
.reverse();

callback(frames);
}
});

return () => {
sendCommand('Debugger.enable');
sendCommand('Debugger.pause');
};
}
Loading
Loading