From 059ad3905b460db729521d831fa128552968f676 Mon Sep 17 00:00:00 2001 From: vmarchaud Date: Wed, 4 Sep 2019 09:27:38 +0200 Subject: [PATCH] fix(node-tracer): set scope manager as optional --- .../src/NodeTracer.ts | 49 +++++++++++++++++-- .../test/NodeTracer.test.ts | 39 +++------------ 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/packages/opentelemetry-node-tracer/src/NodeTracer.ts b/packages/opentelemetry-node-tracer/src/NodeTracer.ts index 765e1dca9b2..e14ec88dfde 100644 --- a/packages/opentelemetry-node-tracer/src/NodeTracer.ts +++ b/packages/opentelemetry-node-tracer/src/NodeTracer.ts @@ -16,6 +16,45 @@ import { BasicTracer, BasicTracerConfig } from '@opentelemetry/basic-tracer'; import { AsyncHooksScopeManager } from '@opentelemetry/scope-async-hooks'; +import { ScopeManager } from '@opentelemetry/scope-base'; +import { + Attributes, + BinaryFormat, + HttpTextFormat, + Logger, + Sampler, +} from '@opentelemetry/types'; + +/** + * NodeTracerConfig provides an interface for configuring a Node Tracer. + */ +export interface NodeTracerConfig { + /** + * Binary formatter which can serialize/deserialize Spans. + */ + binaryFormat?: BinaryFormat; + /** + * Attributed that will be applied on every span created by Tracer. + * Useful to add infrastructure and environment information to your spans. + */ + defaultAttributes?: Attributes; + /** + * HTTP text formatter which can inject/extract Spans. + */ + httpTextFormat?: HttpTextFormat; + /** + * User provided logger. + */ + logger?: Logger; + /** + * Sampler determines if a span should be recorded or should be a NoopSpan. + */ + sampler?: Sampler; + /** + * Scope manager keeps context across in-process operations. + */ + scopeManager?: ScopeManager; +} /** * This class represents a node tracer with `async_hooks` module. @@ -24,10 +63,12 @@ export class NodeTracer extends BasicTracer { /** * Constructs a new Tracer instance. */ - constructor(config: BasicTracerConfig) { - const scopeManager = new AsyncHooksScopeManager(); - super(Object.assign({}, { scopeManager }, config)); - scopeManager.enable(); + constructor(config: NodeTracerConfig) { + if (config.scopeManager === undefined) { + config.scopeManager = new AsyncHooksScopeManager(); + config.scopeManager.enable(); + } + super(Object.assign({}, { scopeManager: config.scopeManager }, config)); // @todo: Integrate Plugin Loader (pull/126). } diff --git a/packages/opentelemetry-node-tracer/test/NodeTracer.test.ts b/packages/opentelemetry-node-tracer/test/NodeTracer.test.ts index 337cf19a491..2ac863d1f99 100644 --- a/packages/opentelemetry-node-tracer/test/NodeTracer.test.ts +++ b/packages/opentelemetry-node-tracer/test/NodeTracer.test.ts @@ -36,16 +36,13 @@ const sleep = (time: number) => describe('NodeTracer', () => { describe('constructor', () => { it('should construct an instance with required only options', () => { - const tracer = new NodeTracer({ - scopeManager: new AsyncHooksScopeManager(), - }); + const tracer = new NodeTracer({}); assert.ok(tracer instanceof NodeTracer); }); it('should construct an instance with binary format', () => { const tracer = new NodeTracer({ binaryFormat: new BinaryTraceContext(), - scopeManager: new AsyncHooksScopeManager(), }); assert.ok(tracer instanceof NodeTracer); }); @@ -61,14 +58,12 @@ describe('NodeTracer', () => { it('should construct an instance with logger', () => { const tracer = new NodeTracer({ logger: new NoopLogger(), - scopeManager: new AsyncHooksScopeManager(), }); assert.ok(tracer instanceof NodeTracer); }); it('should construct an instance with sampler', () => { const tracer = new NodeTracer({ - scopeManager: new AsyncHooksScopeManager(), sampler: ALWAYS_SAMPLER, }); assert.ok(tracer instanceof NodeTracer); @@ -80,7 +75,6 @@ describe('NodeTracer', () => { region: 'eu-west', asg: 'my-asg', }, - scopeManager: new AsyncHooksScopeManager(), }); assert.ok(tracer instanceof NodeTracer); }); @@ -89,7 +83,6 @@ describe('NodeTracer', () => { describe('.startSpan()', () => { it('should start a span with name only', () => { const tracer = new NodeTracer({ - scopeManager: new AsyncHooksScopeManager(), logger: new NoopLogger(), }); const span = tracer.startSpan('my-span'); @@ -98,7 +91,6 @@ describe('NodeTracer', () => { it('should start a span with name and options', () => { const tracer = new NodeTracer({ - scopeManager: new AsyncHooksScopeManager(), logger: new NoopLogger(), }); const span = tracer.startSpan('my-span', {}); @@ -108,7 +100,6 @@ describe('NodeTracer', () => { it('should return a default span with no sampling', () => { const tracer = new NodeTracer({ sampler: NEVER_SAMPLER, - scopeManager: new AsyncHooksScopeManager(), logger: new NoopLogger(), }); const span = tracer.startSpan('my-span'); @@ -137,18 +128,14 @@ describe('NodeTracer', () => { describe('.getCurrentSpan()', () => { it('should return null with AsyncHooksScopeManager when no span started', () => { - const tracer = new NodeTracer({ - scopeManager: new AsyncHooksScopeManager(), - }); + const tracer = new NodeTracer({}); assert.deepStrictEqual(tracer.getCurrentSpan(), null); }); }); describe('.withSpan()', () => { it('should run scope with AsyncHooksScopeManager scope manager', done => { - const tracer = new NodeTracer({ - scopeManager: new AsyncHooksScopeManager(), - }); + const tracer = new NodeTracer({}); const span = tracer.startSpan('my-span'); tracer.withSpan(span, () => { assert.deepStrictEqual(tracer.getCurrentSpan(), span); @@ -158,9 +145,7 @@ describe('NodeTracer', () => { }); it('should run scope with AsyncHooksScopeManager scope manager with multiple spans', done => { - const tracer = new NodeTracer({ - scopeManager: new AsyncHooksScopeManager(), - }); + const tracer = new NodeTracer({}); const span = tracer.startSpan('my-span'); tracer.withSpan(span, () => { assert.deepStrictEqual(tracer.getCurrentSpan(), span); @@ -181,9 +166,7 @@ describe('NodeTracer', () => { }); it('should find correct scope with promises', done => { - const tracer = new NodeTracer({ - scopeManager: new AsyncHooksScopeManager(), - }); + const tracer = new NodeTracer({}); const span = tracer.startSpan('my-span'); tracer.withSpan(span, async () => { for (let i = 0; i < 3; i++) { @@ -199,9 +182,7 @@ describe('NodeTracer', () => { describe('.bind()', () => { it('should bind scope with AsyncHooksScopeManager scope manager', done => { - const tracer = new NodeTracer({ - scopeManager: new AsyncHooksScopeManager(), - }); + const tracer = new NodeTracer({}); const span = tracer.startSpan('my-span'); const fn = () => { assert.deepStrictEqual(tracer.getCurrentSpan(), span); @@ -219,18 +200,14 @@ describe('NodeTracer', () => { describe('.getBinaryFormat()', () => { it('should get default binary formatter', () => { - const tracer = new NodeTracer({ - scopeManager: new AsyncHooksScopeManager(), - }); + const tracer = new NodeTracer({}); assert.ok(tracer.getBinaryFormat() instanceof BinaryTraceContext); }); }); describe('.getHttpTextFormat()', () => { it('should get default HTTP text formatter', () => { - const tracer = new NodeTracer({ - scopeManager: new AsyncHooksScopeManager(), - }); + const tracer = new NodeTracer({}); assert.ok(tracer.getHttpTextFormat() instanceof HttpTraceContext); }); });