From e82a95b326ddc41547f30d18c8fd3b3edb828328 Mon Sep 17 00:00:00 2001 From: naseemkullah Date: Fri, 21 May 2021 19:15:59 -0400 Subject: [PATCH 1/6] chore: function overloads implementation of startActiveSpan in noop tracer Signed-off-by: naseemkullah --- src/trace/NoopTracer.ts | 58 +++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/src/trace/NoopTracer.ts b/src/trace/NoopTracer.ts index 7f810522..724f0ecf 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, + options: SpanOptions, + fn: F + ): ReturnType; + startActiveSpan ReturnType>( + name: string, + options: SpanOptions, + context: Context, + 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 options: SpanOptions | undefined; + let activeContext: Context | undefined; + let fn: F; + + if (arguments.length < 2) { + return; + } else if (arguments.length === 2) { + fn = arg2 as F; + } else if (arguments.length === 3) { + options = arg2 as SpanOptions | undefined; + fn = arg3 as F; + } else { + options = arg2 as SpanOptions | undefined; + activeContext = arg3 as Context | undefined; + fn = arg4 as F; } const parentContext = activeContext ?? context.active(); const span = this.startSpan(name, options, parentContext); const contextWithSpanSet = setSpan(parentContext, span); - if (fn) { - return context.with(contextWithSpanSet, fn, undefined, span); - } - return; + return context.with(contextWithSpanSet, fn, undefined, span); } } From 815cb2f1020f1ff3ee59a0cce30dfe6b71876561 Mon Sep 17 00:00:00 2001 From: naseemkullah Date: Fri, 21 May 2021 20:39:08 -0400 Subject: [PATCH 2/6] chore: change var names Signed-off-by: naseemkullah --- src/trace/NoopTracer.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/trace/NoopTracer.ts b/src/trace/NoopTracer.ts index 724f0ecf..9223ecf0 100644 --- a/src/trace/NoopTracer.ts +++ b/src/trace/NoopTracer.ts @@ -53,13 +53,13 @@ export class NoopTracer implements Tracer { ): ReturnType; startActiveSpan ReturnType>( name: string, - options: SpanOptions, + opts: SpanOptions, fn: F ): ReturnType; startActiveSpan ReturnType>( name: string, - options: SpanOptions, - context: Context, + opts: SpanOptions, + ctx: Context, fn: F ): ReturnType; startActiveSpan ReturnType>( @@ -68,8 +68,8 @@ export class NoopTracer implements Tracer { arg3?: F | Context, arg4?: F ): ReturnType | undefined { - let options: SpanOptions | undefined; - let activeContext: Context | undefined; + let opts: SpanOptions | undefined; + let ctx: Context | undefined; let fn: F; if (arguments.length < 2) { @@ -77,16 +77,16 @@ export class NoopTracer implements Tracer { } else if (arguments.length === 2) { fn = arg2 as F; } else if (arguments.length === 3) { - options = arg2 as SpanOptions | undefined; + opts = arg2 as SpanOptions | undefined; fn = arg3 as F; } else { - options = arg2 as SpanOptions | undefined; - activeContext = arg3 as Context | undefined; + 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); return context.with(contextWithSpanSet, fn, undefined, span); From 77e349043347fe099c66a9b18ee84f5297753941 Mon Sep 17 00:00:00 2001 From: naseemkullah Date: Sat, 22 May 2021 08:48:10 -0400 Subject: [PATCH 3/6] refactor: call startActiveSpan directly in assertion Signed-off-by: naseemkullah --- test/noop-implementations/noop-tracer.test.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/test/noop-implementations/noop-tracer.test.ts b/test/noop-implementations/noop-tracer.test.ts index 0d4ca1a3..295da75f 100644 --- a/test/noop-implementations/noop-tracer.test.ts +++ b/test/noop-implementations/noop-tracer.test.ts @@ -69,16 +69,14 @@ describe('NoopTracer', () => { } }; const opts = { attributes: { foo: 'bar' } }; - const ctx = context.active(); - const a = tracer.startActiveSpan(name, fn); - assert.strictEqual(a, 1); + assert.strictEqual(tracer.startActiveSpan(name, fn), 1); - const b = tracer.startActiveSpan(name, opts, fn); + assert.strictEqual(tracer.startActiveSpan(name, opts, fn), 1); - assert.strictEqual(b, 1); - - const c = tracer.startActiveSpan(name, opts, ctx, fn); - assert.strictEqual(c, 1); + assert.strictEqual( + tracer.startActiveSpan(name, opts, context.active(), fn), + 1 + ); }); }); From a0a187c9f27ebbcdd0c3f05475d5458eb5662484 Mon Sep 17 00:00:00 2001 From: naseemkullah Date: Sat, 22 May 2021 08:51:50 -0400 Subject: [PATCH 4/6] test: wrong usage to increase coverage Signed-off-by: naseemkullah --- test/noop-implementations/noop-tracer.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/noop-implementations/noop-tracer.test.ts b/test/noop-implementations/noop-tracer.test.ts index 295da75f..2e0f1777 100644 --- a/test/noop-implementations/noop-tracer.test.ts +++ b/test/noop-implementations/noop-tracer.test.ts @@ -70,6 +70,8 @@ describe('NoopTracer', () => { }; const opts = { attributes: { foo: 'bar' } }; + assert.strictEqual((tracer as any).startActiveSpan(name), undefined); + assert.strictEqual(tracer.startActiveSpan(name, fn), 1); assert.strictEqual(tracer.startActiveSpan(name, opts, fn), 1); From 9c6f23e678f5c233d5b3dcc466fbeca5bb29ae43 Mon Sep 17 00:00:00 2001 From: Naseem Date: Mon, 24 May 2021 11:59:37 -0400 Subject: [PATCH 5/6] Update src/trace/NoopTracer.ts Co-authored-by: Daniel Dyla --- src/trace/NoopTracer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/trace/NoopTracer.ts b/src/trace/NoopTracer.ts index 9223ecf0..5db4b74b 100644 --- a/src/trace/NoopTracer.ts +++ b/src/trace/NoopTracer.ts @@ -53,7 +53,7 @@ export class NoopTracer implements Tracer { ): ReturnType; startActiveSpan ReturnType>( name: string, - opts: SpanOptions, + opts: SpanOptions | undefined, fn: F ): ReturnType; startActiveSpan ReturnType>( From 1a78b422db8384dcbc15c76223a4545561f6117a Mon Sep 17 00:00:00 2001 From: Naseem Date: Mon, 24 May 2021 11:59:53 -0400 Subject: [PATCH 6/6] Update src/trace/NoopTracer.ts Co-authored-by: Daniel Dyla --- src/trace/NoopTracer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/trace/NoopTracer.ts b/src/trace/NoopTracer.ts index 5db4b74b..e172150d 100644 --- a/src/trace/NoopTracer.ts +++ b/src/trace/NoopTracer.ts @@ -58,8 +58,8 @@ export class NoopTracer implements Tracer { ): ReturnType; startActiveSpan ReturnType>( name: string, - opts: SpanOptions, - ctx: Context, + opts: SpanOptions | undefined, + ctx: Context | undefined, fn: F ): ReturnType; startActiveSpan ReturnType>(