Skip to content

Commit

Permalink
Pass through context in ProxyTracer.startSpan() (#1604)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Dyla <[email protected]>
  • Loading branch information
pauldraper and dyladan authored Jan 13, 2021
1 parent 73b8bd0 commit 1035ac4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/opentelemetry-api/src/trace/ProxyTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { Context } from '@opentelemetry/context-base';
import { Span, SpanOptions, Tracer } from '..';
import { NOOP_TRACER } from './NoopTracer';
import { ProxyTracerProvider } from './ProxyTracerProvider';
Expand All @@ -31,8 +32,8 @@ export class ProxyTracer implements Tracer {
public readonly version?: string
) {}

startSpan(name: string, options?: SpanOptions): Span {
return this._getTracer().startSpan(name, options);
startSpan(name: string, options?: SpanOptions, context?: Context): Span {
return this._getTracer().startSpan(name, options, context);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@ import {
Tracer,
Span,
NoopTracer,
ROOT_CONTEXT,
SpanOptions,
} from '../../src';

describe('ProxyTracer', () => {
let provider: ProxyTracerProvider;
const sandbox = sinon.createSandbox();

beforeEach(() => {
provider = new ProxyTracerProvider();
});

afterEach(() => {
sandbox.restore();
});

describe('when no delegate is set', () => {
it('should return proxy tracers', () => {
const tracer = provider.getTracer('test');
Expand All @@ -61,7 +68,6 @@ describe('ProxyTracer', () => {

describe('when delegate is set before getTracer', () => {
let delegate: TracerProvider;
const sandbox = sinon.createSandbox();
let getTracerStub: sinon.SinonStub;

beforeEach(() => {
Expand All @@ -72,10 +78,6 @@ describe('ProxyTracer', () => {
provider.setDelegate(delegate);
});

afterEach(() => {
sandbox.restore();
});

it('should return tracers directly from the delegate', () => {
const tracer = provider.getTracer('test', 'v0');

Expand Down Expand Up @@ -114,5 +116,25 @@ describe('ProxyTracer', () => {

assert.strictEqual(span, delegateSpan);
});

it('should pass original arguments to DelegateTracer#startSpan', () => {
const startSpanStub = sandbox.stub(delegateTracer, 'startSpan');

const name = 'name1';
const options: SpanOptions = {};
const ctx = ROOT_CONTEXT.setValue(Symbol('test'), 1);
tracer.startSpan(name, options, ctx);

// Assert the proxy tracer has the full API of the NoopTracer
assert.strictEqual(
NoopTracer.prototype.startSpan.length,
ProxyTracer.prototype.startSpan.length
);
assert.deepStrictEqual(Object.getOwnPropertyNames(NoopTracer.prototype), [
'constructor',
'startSpan',
]);
sandbox.assert.calledOnceWithExactly(startSpanStub, name, options, ctx);
});
});
});

0 comments on commit 1035ac4

Please sign in to comment.