Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
global tracer call delegation uses wrong this (#82)
Browse files Browse the repository at this point in the history
The global tracer delegate uses the delegate as the this context when
calling the configured global tracer. This will break many tracing
implementations. Most notably, this will break all tracing
implementations which subclass the opentracing provided Tracer class.
  • Loading branch information
bripkens authored and bhs committed May 19, 2017
1 parent d7c443c commit a8c0ee3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
History
-------

Unreleased
-------------------

- The `globalTracer` delegate will now correctly call `startSpan`, `inject` and `extract` with the configured global tracer as `this`.


0.14.0
-------------------

Expand Down
6 changes: 3 additions & 3 deletions src/global_tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ class GlobalTracerDelegate extends Tracer {

startSpan(): any {
const tracer = _globalTracer || noopTracer;
return tracer.startSpan.apply(this, arguments);
return tracer.startSpan.apply(tracer, arguments);
}

inject(): any {
const tracer = _globalTracer || noopTracer;
return tracer.inject.apply(this, arguments);
return tracer.inject.apply(tracer, arguments);
}

extract(): any {
const tracer = _globalTracer || noopTracer;
return tracer.extract.apply(this, arguments);
return tracer.extract.apply(tracer, arguments);
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/test/opentracing_api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

import { expect } from 'chai';
import * as opentracing from '../index';
import Span from '../span';
import { SpanOptions, Tracer } from '../tracer';

export function opentracingAPITests(): void {
describe('Opentracing API', () => {
Expand Down Expand Up @@ -38,6 +40,27 @@ export function opentracingAPITests(): void {
expect(opentracing[name]).to.be.a('function');
});
}

describe('global tracer', () => {
const dummySpan = new Span();

afterEach(() => {
opentracing.initGlobalTracer(new Tracer());
});

it('should use the global tracer', () => {
opentracing.initGlobalTracer(new TestTracer());
const tracer = opentracing.globalTracer();
const span = tracer.startSpan('test');
expect(span).to.equal(dummySpan);
});

class TestTracer extends Tracer {
protected _startSpan(name: string, fields: SpanOptions): Span {
return dummySpan;
}
}
});
});

describe('Tracer', () => {
Expand Down

0 comments on commit a8c0ee3

Please sign in to comment.