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

feat(node): Auto-select best AsyncContextStrategy for Node.js version #7804

Merged
merged 2 commits into from
Apr 11, 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
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get a sanity check test for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you mean? A test that checks that the version detection is working?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, but now that I think about it, it's maybe too complicated. We can add one any time later on.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm surprised that there was no TypeScript or lint error from using if (NODE_VERSION >= 14)!

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