Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(node): Add disableAnrDetectionForCallback function #14359

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 @@ -15,6 +15,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 @@ -47,6 +47,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 @@ -69,6 +69,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 @@ -47,6 +47,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
35 changes: 34 additions & 1 deletion packages/node/src/integrations/anr/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';
import { types } from 'node:util';
import { Worker } from 'node:worker_threads';
import { defineIntegration, getCurrentScope, getGlobalScope, getIsolationScope, mergeScopeData } from '@sentry/core';
import {
defineIntegration,
getClient,
getCurrentScope,
getGlobalScope,
getIsolationScope,
mergeScopeData,
} from '@sentry/core';
import type { Contexts, Event, EventHint, Integration, IntegrationFn, ScopeData } from '@sentry/types';
import { GLOBAL_OBJ, getFilenameToDebugIdMap, logger } from '@sentry/utils';
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 @@ -219,3 +229,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 @@ -20,6 +20,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 @@ -11,6 +11,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 @@ -11,6 +11,7 @@ export {
addRequestDataToEvent,
amqplibIntegration,
anrIntegration,
disableAnrDetectionForCallback,
captureCheckIn,
captureConsoleIntegration,
captureEvent,
Expand Down
Loading