From 35224c7e3442f8f965bd543f2d1c8a8c70b47351 Mon Sep 17 00:00:00 2001 From: Naseem Date: Tue, 1 Jun 2021 16:36:51 -0400 Subject: [PATCH] =?UTF-8?q?chore:=20function=20overloads=20implementation?= =?UTF-8?q?=20of=20startActiveSpan=20in=20noop=20t=E2=80=A6=20(#81)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Dyla Co-authored-by: Valentin Marchaud --- src/trace/NoopTracer.ts | 62 ++++++++++--------- test/noop-implementations/noop-tracer.test.ts | 14 ++--- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/trace/NoopTracer.ts b/src/trace/NoopTracer.ts index b44dde20..238553e6 100644 --- a/src/trace/NoopTracer.ts +++ b/src/trace/NoopTracer.ts @@ -49,41 +49,47 @@ export class NoopTracer implements Tracer { startActiveSpan ReturnType>( name: string, - arg2: F | SpanOptions, + fn: F + ): ReturnType; + startActiveSpan ReturnType>( + name: string, + opts: SpanOptions | undefined, + fn: F + ): ReturnType; + startActiveSpan ReturnType>( + name: string, + opts: SpanOptions | undefined, + ctx: Context | undefined, + fn: F + ): ReturnType; + startActiveSpan ReturnType>( + name: string, + arg2?: F | SpanOptions, arg3?: F | Context, arg4?: F ): ReturnType | undefined { - let fn: F | undefined, - options: SpanOptions | undefined, - activeContext: Context | undefined; - if (arguments.length === 2 && typeof arg2 === 'function') { - fn = arg2; - } else if ( - arguments.length === 3 && - typeof arg2 === 'object' && - typeof arg3 === 'function' - ) { - options = arg2; - fn = arg3; - } else if ( - arguments.length === 4 && - typeof arg2 === 'object' && - typeof arg3 === 'object' && - typeof arg4 === 'function' - ) { - options = arg2; - activeContext = arg3; - fn = arg4; + let opts: SpanOptions | undefined; + let ctx: Context | undefined; + let fn: F; + + if (arguments.length < 2) { + return; + } else if (arguments.length === 2) { + fn = arg2 as F; + } else if (arguments.length === 3) { + opts = arg2 as SpanOptions | undefined; + fn = arg3 as F; + } else { + opts = arg2 as SpanOptions | undefined; + ctx = arg3 as Context | undefined; + fn = arg4 as F; } - const parentContext = activeContext ?? context.active(); - const span = this.startSpan(name, options, parentContext); + const parentContext = ctx ?? context.active(); + const span = this.startSpan(name, opts, parentContext); const contextWithSpanSet = setSpan(parentContext, span); - if (fn) { - return context.with(contextWithSpanSet, fn, undefined, span); - } - return; + return context.with(contextWithSpanSet, fn, undefined, span); } } diff --git a/test/noop-implementations/noop-tracer.test.ts b/test/noop-implementations/noop-tracer.test.ts index 7f23ff22..03701720 100644 --- a/test/noop-implementations/noop-tracer.test.ts +++ b/test/noop-implementations/noop-tracer.test.ts @@ -69,16 +69,16 @@ describe('NoopTracer', () => { } }; const opts = { attributes: { foo: 'bar' } }; - const ctx = context.active(); - const a = tracer.startActiveSpan(name, fn); - assert.strictEqual(a, 1); + assert.strictEqual((tracer as any).startActiveSpan(name), undefined); - const b = tracer.startActiveSpan(name, opts, fn); + assert.strictEqual(tracer.startActiveSpan(name, fn), 1); - assert.strictEqual(b, 1); + assert.strictEqual(tracer.startActiveSpan(name, opts, fn), 1); - const c = tracer.startActiveSpan(name, opts, ctx, fn); - assert.strictEqual(c, 1); + assert.strictEqual( + tracer.startActiveSpan(name, opts, context.active(), fn), + 1 + ); }); });