Skip to content

Commit 4856685

Browse files
timfishAbhiPrasad
andauthored
feat(node): Auto-select best AsyncContextStrategy for Node.js version (#7804)
Co-authored-by: Abhijeet Prasad <[email protected]>
1 parent 53ae9ae commit 4856685

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

packages/node/src/async/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { NODE_VERSION } from '../nodeVersion';
2+
import { setDomainAsyncContextStrategy } from './domain';
3+
import { setHooksAsyncContextStrategy } from './hooks';
4+
5+
/**
6+
* Sets the correct async context strategy for Node.js
7+
*
8+
* Node.js >= 14 uses AsyncLocalStorage
9+
* Node.js < 14 uses domains
10+
*/
11+
export function setNodeAsyncContextStrategy(): void {
12+
if (NODE_VERSION.major && NODE_VERSION.major >= 14) {
13+
setHooksAsyncContextStrategy();
14+
} else {
15+
setDomainAsyncContextStrategy();
16+
}
17+
}

packages/node/src/sdk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
stackParserFromStackParserOptions,
1616
} from '@sentry/utils';
1717

18-
import { setDomainAsyncContextStrategy } from './async/domain';
18+
import { setNodeAsyncContextStrategy } from './async';
1919
import { NodeClient } from './client';
2020
import {
2121
Console,
@@ -111,7 +111,7 @@ export const defaultIntegrations = [
111111
export function init(options: NodeOptions = {}): void {
112112
const carrier = getMainCarrier();
113113

114-
setDomainAsyncContextStrategy();
114+
setNodeAsyncContextStrategy();
115115

116116
const autoloadedIntegrations = carrier.__SENTRY__?.integrations || [];
117117

packages/node/test/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
init,
1313
NodeClient,
1414
} from '../src';
15-
import { setDomainAsyncContextStrategy } from '../src/async/domain';
15+
import { setNodeAsyncContextStrategy } from '../src/async';
1616
import { ContextLines, LinkedErrors } from '../src/integrations';
1717
import { defaultStackParser } from '../src/sdk';
1818
import type { NodeClientOptions } from '../src/types';
@@ -288,7 +288,7 @@ describe('SentryNode', () => {
288288
},
289289
dsn,
290290
});
291-
setDomainAsyncContextStrategy();
291+
setNodeAsyncContextStrategy();
292292
const client = new NodeClient(options);
293293

294294
runWithAsyncContext(hub => {

0 commit comments

Comments
 (0)