Skip to content
This repository was archived by the owner on Nov 10, 2022. It is now read-only.

Commit 35224c7

Browse files
Naseemdyladanvmarchaud
authored
chore: function overloads implementation of startActiveSpan in noop t… (#81)
Co-authored-by: Daniel Dyla <[email protected]> Co-authored-by: Valentin Marchaud <[email protected]>
1 parent 1a35772 commit 35224c7

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

src/trace/NoopTracer.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,41 +49,47 @@ export class NoopTracer implements Tracer {
4949

5050
startActiveSpan<F extends (span: Span) => ReturnType<F>>(
5151
name: string,
52-
arg2: F | SpanOptions,
52+
fn: F
53+
): ReturnType<F>;
54+
startActiveSpan<F extends (span: Span) => ReturnType<F>>(
55+
name: string,
56+
opts: SpanOptions | undefined,
57+
fn: F
58+
): ReturnType<F>;
59+
startActiveSpan<F extends (span: Span) => ReturnType<F>>(
60+
name: string,
61+
opts: SpanOptions | undefined,
62+
ctx: Context | undefined,
63+
fn: F
64+
): ReturnType<F>;
65+
startActiveSpan<F extends (span: Span) => ReturnType<F>>(
66+
name: string,
67+
arg2?: F | SpanOptions,
5368
arg3?: F | Context,
5469
arg4?: F
5570
): ReturnType<F> | undefined {
56-
let fn: F | undefined,
57-
options: SpanOptions | undefined,
58-
activeContext: Context | undefined;
59-
if (arguments.length === 2 && typeof arg2 === 'function') {
60-
fn = arg2;
61-
} else if (
62-
arguments.length === 3 &&
63-
typeof arg2 === 'object' &&
64-
typeof arg3 === 'function'
65-
) {
66-
options = arg2;
67-
fn = arg3;
68-
} else if (
69-
arguments.length === 4 &&
70-
typeof arg2 === 'object' &&
71-
typeof arg3 === 'object' &&
72-
typeof arg4 === 'function'
73-
) {
74-
options = arg2;
75-
activeContext = arg3;
76-
fn = arg4;
71+
let opts: SpanOptions | undefined;
72+
let ctx: Context | undefined;
73+
let fn: F;
74+
75+
if (arguments.length < 2) {
76+
return;
77+
} else if (arguments.length === 2) {
78+
fn = arg2 as F;
79+
} else if (arguments.length === 3) {
80+
opts = arg2 as SpanOptions | undefined;
81+
fn = arg3 as F;
82+
} else {
83+
opts = arg2 as SpanOptions | undefined;
84+
ctx = arg3 as Context | undefined;
85+
fn = arg4 as F;
7786
}
7887

79-
const parentContext = activeContext ?? context.active();
80-
const span = this.startSpan(name, options, parentContext);
88+
const parentContext = ctx ?? context.active();
89+
const span = this.startSpan(name, opts, parentContext);
8190
const contextWithSpanSet = setSpan(parentContext, span);
8291

83-
if (fn) {
84-
return context.with(contextWithSpanSet, fn, undefined, span);
85-
}
86-
return;
92+
return context.with(contextWithSpanSet, fn, undefined, span);
8793
}
8894
}
8995

test/noop-implementations/noop-tracer.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ describe('NoopTracer', () => {
6969
}
7070
};
7171
const opts = { attributes: { foo: 'bar' } };
72-
const ctx = context.active();
7372

74-
const a = tracer.startActiveSpan(name, fn);
75-
assert.strictEqual(a, 1);
73+
assert.strictEqual((tracer as any).startActiveSpan(name), undefined);
7674

77-
const b = tracer.startActiveSpan(name, opts, fn);
75+
assert.strictEqual(tracer.startActiveSpan(name, fn), 1);
7876

79-
assert.strictEqual(b, 1);
77+
assert.strictEqual(tracer.startActiveSpan(name, opts, fn), 1);
8078

81-
const c = tracer.startActiveSpan(name, opts, ctx, fn);
82-
assert.strictEqual(c, 1);
79+
assert.strictEqual(
80+
tracer.startActiveSpan(name, opts, context.active(), fn),
81+
1
82+
);
8383
});
8484
});

0 commit comments

Comments
 (0)