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

Commit

Permalink
chore: function overloads implementation of startActiveSpan in noop t… (
Browse files Browse the repository at this point in the history
#81)

Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr>
  • Loading branch information
3 people committed Jun 1, 2021
1 parent 1a35772 commit 35224c7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
62 changes: 34 additions & 28 deletions src/trace/NoopTracer.ts
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
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
);
});
});

0 comments on commit 35224c7

Please sign in to comment.