Skip to content

Commit

Permalink
fix(node-tracer): set scope manager as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Sep 4, 2019
1 parent dbcbfeb commit 059ad39
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 35 deletions.
49 changes: 45 additions & 4 deletions packages/opentelemetry-node-tracer/src/NodeTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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).
}
Expand Down
39 changes: 8 additions & 31 deletions packages/opentelemetry-node-tracer/test/NodeTracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
Expand All @@ -80,7 +75,6 @@ describe('NodeTracer', () => {
region: 'eu-west',
asg: 'my-asg',
},
scopeManager: new AsyncHooksScopeManager(),
});
assert.ok(tracer instanceof NodeTracer);
});
Expand All @@ -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');
Expand All @@ -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', {});
Expand All @@ -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');
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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++) {
Expand All @@ -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);
Expand All @@ -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);
});
});
Expand Down

0 comments on commit 059ad39

Please sign in to comment.