From 1035ac4de3002038b30b2c8e8e04d6d6fafca71a Mon Sep 17 00:00:00 2001 From: Paul Draper Date: Wed, 13 Jan 2021 09:26:13 -0700 Subject: [PATCH] Pass through context in ProxyTracer.startSpan() (#1604) Co-authored-by: Daniel Dyla --- .../src/trace/ProxyTracer.ts | 5 +-- .../proxy-tracer.test.ts | 32 ++++++++++++++++--- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/packages/opentelemetry-api/src/trace/ProxyTracer.ts b/packages/opentelemetry-api/src/trace/ProxyTracer.ts index c6e22433e7..591610c3ae 100644 --- a/packages/opentelemetry-api/src/trace/ProxyTracer.ts +++ b/packages/opentelemetry-api/src/trace/ProxyTracer.ts @@ -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'; @@ -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); } /** diff --git a/packages/opentelemetry-api/test/proxy-implementations/proxy-tracer.test.ts b/packages/opentelemetry-api/test/proxy-implementations/proxy-tracer.test.ts index 41db382f3d..9170f3e44a 100644 --- a/packages/opentelemetry-api/test/proxy-implementations/proxy-tracer.test.ts +++ b/packages/opentelemetry-api/test/proxy-implementations/proxy-tracer.test.ts @@ -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'); @@ -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(() => { @@ -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'); @@ -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); + }); }); });