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

chore: function overloads implementation of startActiveSpan in noop t… #81

Merged
merged 9 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions src/trace/NoopTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,41 +49,47 @@ export class NoopTracer implements Tracer {

startActiveSpan<F extends (span: Span) => ReturnType<F>>(
name: string,
arg2: F | SpanOptions,
fn: F
): ReturnType<F>;
startActiveSpan<F extends (span: Span) => ReturnType<F>>(
name: string,
opts: SpanOptions | undefined,
fn: F
): ReturnType<F>;
startActiveSpan<F extends (span: Span) => ReturnType<F>>(
name: string,
opts: SpanOptions | undefined,
ctx: Context | undefined,
fn: F
): ReturnType<F>;
startActiveSpan<F extends (span: Span) => ReturnType<F>>(
name: string,
arg2?: F | SpanOptions,
arg3?: F | Context,
arg4?: F
): ReturnType<F> | 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);
}
}

Expand Down
14 changes: 7 additions & 7 deletions test/noop-implementations/noop-tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});
});