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 75e551d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 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
18 changes: 11 additions & 7 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 DefaultContextManager = semver.gte(process.version, '14.8.0')
? AsyncLocalStorageContextManager
: AsyncHooksContextManager;

describe('API registration', () => {
beforeEach(() => {
Expand All @@ -38,9 +46,7 @@ describe('API registration', () => {
const tracerProvider = new NodeTracerProvider();
tracerProvider.register();

assert.ok(
context['_getContextManager']() instanceof AsyncHooksContextManager
);
assert.ok(context['_getContextManager']() instanceof DefaultContextManager);
assert.ok(
propagation['_getGlobalPropagator']() instanceof CompositePropagator
);
Expand Down Expand Up @@ -96,9 +102,7 @@ describe('API registration', () => {
propagation['_getGlobalPropagator']() instanceof NoopTextMapPropagator
);

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

const apiTracerProvider = trace.getTracerProvider();
assert.ok(apiTracerProvider instanceof ProxyTracerProvider);
Expand Down
13 changes: 10 additions & 3 deletions packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ 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 { ConsoleMetricExporter, MeterProvider } from '@opentelemetry/metrics';
Expand Down Expand Up @@ -81,6 +84,10 @@ const mockedIdentityResponse = {
};
const mockedHostResponse = 'my-hostname';

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

describe('Node SDK', () => {
before(() => {
// Disable attempted load of default plugins
Expand Down Expand Up @@ -127,7 +134,7 @@ describe('Node SDK', () => {
assert.ok(metrics.getMeterProvider() instanceof NoopMeterProvider);

assert.ok(
context['_getContextManager']() instanceof AsyncHooksContextManager
context['_getContextManager']() instanceof DefaultContextManager
);
assert.ok(
propagation['_getGlobalPropagator']() instanceof CompositePropagator
Expand All @@ -151,7 +158,7 @@ describe('Node SDK', () => {
assert.ok(metrics.getMeterProvider() instanceof NoopMeterProvider);

assert.ok(
context['_getContextManager']() instanceof AsyncHooksContextManager
context['_getContextManager']() instanceof DefaultContextManager
);
assert.ok(
propagation['_getGlobalPropagator']() instanceof CompositePropagator
Expand Down

0 comments on commit 75e551d

Please sign in to comment.