Skip to content

feat(node): Add disableAnrDetectionForCallback function #14359

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 8 commits into from
Dec 4, 2024
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
addRequestDataToEvent,
amqplibIntegration,
anrIntegration,
disableAnrDetectionForCallback,
captureCheckIn,
captureConsoleIntegration,
captureEvent,
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 @@ -49,6 +49,7 @@ export {
extractRequestData,
createGetModuleFromFilename,
anrIntegration,
disableAnrDetectionForCallback,
consoleIntegration,
httpIntegration,
nativeNodeFetchIntegration,
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 @@ -71,6 +71,7 @@ export {
extractRequestData,
createGetModuleFromFilename,
anrIntegration,
disableAnrDetectionForCallback,
consoleIntegration,
httpIntegration,
nativeNodeFetchIntegration,
Expand Down
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 @@ -49,6 +49,7 @@ export {
extractRequestData,
createGetModuleFromFilename,
anrIntegration,
disableAnrDetectionForCallback,
consoleIntegration,
httpIntegration,
nativeNodeFetchIntegration,
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export { localVariablesIntegration } from './integrations/local-variables';
export { modulesIntegration } from './integrations/modules';
export { onUncaughtExceptionIntegration } from './integrations/onuncaughtexception';
export { onUnhandledRejectionIntegration } from './integrations/onunhandledrejection';
export { anrIntegration } from './integrations/anr';
export { anrIntegration, disableAnrDetectionForCallback } from './integrations/anr';

export { expressIntegration, expressErrorHandler, setupExpressErrorHandler } from './integrations/tracing/express';
export { fastifyIntegration, setupFastifyErrorHandler } from './integrations/tracing/fastify';
Expand Down
27 changes: 27 additions & 0 deletions packages/node/src/integrations/anr/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { types } from 'node:util';
import { Worker } from 'node:worker_threads';
import type { Contexts, Event, EventHint, Integration, IntegrationFn, ScopeData } from '@sentry/core';
import {
GLOBAL_OBJ,
defineIntegration,
getClient,
getCurrentScope,
getFilenameToDebugIdMap,
getGlobalScope,
Expand All @@ -14,6 +16,8 @@ import { NODE_VERSION } from '../../nodeVersion';
import type { NodeClient } from '../../sdk/client';
import type { AnrIntegrationOptions, WorkerStartData } from './common';

const { isPromise } = types;

// This string is a placeholder that gets overwritten with the worker code.
export const base64WorkerScript = '###AnrWorkerScript###';

Expand Down Expand Up @@ -213,3 +217,26 @@ async function _startWorker(
clearInterval(timer);
};
}

export function disableAnrDetectionForCallback<T>(callback: () => T): T;
export function disableAnrDetectionForCallback<T>(callback: () => Promise<T>): Promise<T>;
/**
* Disables ANR detection for the duration of the callback
*/
export function disableAnrDetectionForCallback<T>(callback: () => T | Promise<T>): T | Promise<T> {
const integration = getClient()?.getIntegrationByName(INTEGRATION_NAME) as AnrInternal | undefined;

if (!integration) {
return callback();
}

integration.stopWorker();

const result = callback();
if (isPromise(result)) {
return result.finally(() => integration.startWorker());
}

integration.startWorker();
return result;
}
1 change: 1 addition & 0 deletions packages/remix/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
addRequestDataToEvent,
amqplibIntegration,
anrIntegration,
disableAnrDetectionForCallback,
captureCheckIn,
captureConsoleIntegration,
captureEvent,
Expand Down
1 change: 1 addition & 0 deletions packages/solidstart/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
addRequestDataToEvent,
amqplibIntegration,
anrIntegration,
disableAnrDetectionForCallback,
captureCheckIn,
captureConsoleIntegration,
captureEvent,
Expand Down
1 change: 1 addition & 0 deletions packages/sveltekit/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
addRequestDataToEvent,
amqplibIntegration,
anrIntegration,
disableAnrDetectionForCallback,
captureCheckIn,
captureConsoleIntegration,
captureEvent,
Expand Down
Loading