Skip to content

Commit

Permalink
feat(node-tracer): use AsyncLocalStorageContextManager by default sta…
Browse files Browse the repository at this point in the history
…rting Node 14.8
  • Loading branch information
vmarchaud committed Sep 13, 2020
1 parent f824195 commit dd701fc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
11 changes: 9 additions & 2 deletions packages/opentelemetry-node/src/NodeTracerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
* limitations under the License.
*/

import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import {
AsyncHooksContextManager,
AsyncLocalStorageContextManager,
} from '@opentelemetry/context-async-hooks';
import {
BasicTracerProvider,
SDKRegistrationConfig,
} from '@opentelemetry/tracing';
import { DEFAULT_INSTRUMENTATION_PLUGINS, NodeTracerConfig } from './config';
import { PluginLoader, Plugins } from './instrumentation/PluginLoader';
import * as semver from 'semver';

/**
* Register this TracerProvider for use with the OpenTelemetry API.
Expand Down Expand Up @@ -53,7 +57,10 @@ export class NodeTracerProvider extends BasicTracerProvider {

register(config: SDKRegistrationConfig = {}) {
if (config.contextManager === undefined) {
config.contextManager = new AsyncHooksContextManager();
const ContextManager = semver.gte(process.version, '14.8.0')
? AsyncLocalStorageContextManager
: AsyncHooksContextManager;
config.contextManager = new ContextManager();
config.contextManager.enable();
}

Expand Down
14 changes: 11 additions & 3 deletions packages/opentelemetry-node/test/registration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ import {
trace,
ProxyTracerProvider,
} from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import {
AsyncHooksContextManager,
AsyncLocalStorageContextManager,
} from '@opentelemetry/context-async-hooks';
import { NoopContextManager } from '@opentelemetry/context-base';
import { CompositePropagator } from '@opentelemetry/core';
import * as assert from 'assert';
import { NodeTracerProvider } from '../src';
import * as semver from 'semver';

const DdefaultContextManager = semver.gte(process.version, '14.8.0')
? AsyncLocalStorageContextManager
: AsyncHooksContextManager;

describe('API registration', () => {
beforeEach(() => {
Expand All @@ -39,7 +47,7 @@ describe('API registration', () => {
tracerProvider.register();

assert.ok(
context['_getContextManager']() instanceof AsyncHooksContextManager
context['_getContextManager']() instanceof DdefaultContextManager
);
assert.ok(
propagation['_getGlobalPropagator']() instanceof CompositePropagator
Expand Down Expand Up @@ -97,7 +105,7 @@ describe('API registration', () => {
);

assert.ok(
context['_getContextManager']() instanceof AsyncHooksContextManager
context['_getContextManager']() instanceof DdefaultContextManager
);

const apiTracerProvider = trace.getTracerProvider();
Expand Down

0 comments on commit dd701fc

Please sign in to comment.