Skip to content

Commit

Permalink
feat(node): Auto-select best AsyncContextStrategy for Node.js versi…
Browse files Browse the repository at this point in the history
…on (#7804)

Co-authored-by: Abhijeet Prasad <devabhiprasad@gmail.com>
  • Loading branch information
timfish and AbhiPrasad authored Apr 11, 2023
1 parent 53ae9ae commit 4856685
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
17 changes: 17 additions & 0 deletions packages/node/src/async/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NODE_VERSION } from '../nodeVersion';
import { setDomainAsyncContextStrategy } from './domain';
import { setHooksAsyncContextStrategy } from './hooks';

/**
* Sets the correct async context strategy for Node.js
*
* Node.js >= 14 uses AsyncLocalStorage
* Node.js < 14 uses domains
*/
export function setNodeAsyncContextStrategy(): void {
if (NODE_VERSION.major && NODE_VERSION.major >= 14) {
setHooksAsyncContextStrategy();
} else {
setDomainAsyncContextStrategy();
}
}
4 changes: 2 additions & 2 deletions packages/node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
stackParserFromStackParserOptions,
} from '@sentry/utils';

import { setDomainAsyncContextStrategy } from './async/domain';
import { setNodeAsyncContextStrategy } from './async';
import { NodeClient } from './client';
import {
Console,
Expand Down Expand Up @@ -111,7 +111,7 @@ export const defaultIntegrations = [
export function init(options: NodeOptions = {}): void {
const carrier = getMainCarrier();

setDomainAsyncContextStrategy();
setNodeAsyncContextStrategy();

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

Expand Down
4 changes: 2 additions & 2 deletions packages/node/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
init,
NodeClient,
} from '../src';
import { setDomainAsyncContextStrategy } from '../src/async/domain';
import { setNodeAsyncContextStrategy } from '../src/async';
import { ContextLines, LinkedErrors } from '../src/integrations';
import { defaultStackParser } from '../src/sdk';
import type { NodeClientOptions } from '../src/types';
Expand Down Expand Up @@ -288,7 +288,7 @@ describe('SentryNode', () => {
},
dsn,
});
setDomainAsyncContextStrategy();
setNodeAsyncContextStrategy();
const client = new NodeClient(options);

runWithAsyncContext(hub => {
Expand Down

0 comments on commit 4856685

Please sign in to comment.