Skip to content

Commit

Permalink
fix: enable async hooks manager in node tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Aug 31, 2019
1 parent 3f90bf9 commit e4b4fe9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/opentelemetry-node-tracer/src/NodeTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export class NodeTracer extends BasicTracer {
* Constructs a new Tracer instance.
*/
constructor(config: BasicTracerConfig) {
super(
Object.assign({}, { scopeManager: new AsyncHooksScopeManager() }, config)
);
const scopeManager = new AsyncHooksScopeManager();
super(Object.assign({}, { scopeManager }, config));
scopeManager.enable();

// @todo: Integrate Plugin Loader (pull/126).
}
Expand Down
21 changes: 21 additions & 0 deletions packages/opentelemetry-node-tracer/test/NodeTracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import {
import { AsyncHooksScopeManager } from '@opentelemetry/scope-async-hooks';
import { NodeTracer } from '../src/NodeTracer';

const sleep = (time: number) =>
new Promise(resolve => {
return setTimeout(resolve, time);
});

describe('NodeTracer', () => {
describe('constructor', () => {
it('should construct an instance with required only options', () => {
Expand Down Expand Up @@ -156,6 +161,22 @@ describe('NodeTracer', () => {
// @todo: below check is not running.
assert.deepStrictEqual(tracer.getCurrentSpan(), null);
});

it('should find correct scope with promises', done => {
const tracer = new NodeTracer({
scopeManager: new AsyncHooksScopeManager(),
});
const span = tracer.startSpan('my-span');
tracer.withSpan(span, async () => {
for (let i = 0; i < 3; i++) {
await sleep(5).then(() => {
assert.deepStrictEqual(tracer.getCurrentSpan(), span);
});
}
return done();
});
assert.deepStrictEqual(tracer.getCurrentSpan(), null);
});
});

describe('.bind()', () => {
Expand Down

0 comments on commit e4b4fe9

Please sign in to comment.