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

Commit

Permalink
feat: configure overloads
Browse files Browse the repository at this point in the history
the user can call startActiveTracer with 2, 3 or 4 args, with the last being the function

Signed-off-by: naseemkullah <[email protected]>
  • Loading branch information
naseemkullah committed May 3, 2021
1 parent 17ad5a7 commit 81151c8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
31 changes: 24 additions & 7 deletions src/trace/NoopTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* limitations under the License.
*/

import { getSpanContext } from '../trace/context-utils';
import { ContextAPI } from '../api/context';
import { Context } from '../context/types';
import { getSpanContext } from '../trace/context-utils';
import { NonRecordingSpan } from './NonRecordingSpan';
import { Span } from './span';
import { isSpanContextValid } from './spancontext-utils';
Expand Down Expand Up @@ -48,12 +49,28 @@ export class NoopTracer implements Tracer {

startActiveSpan<F extends (span: Span) => ReturnType<F>>(
name: string,
options: SpanOptions,
context: Context,
fn: F
) {
const span = this.startSpan(name, options, context);
return fn(span);
optionsOrFn: SpanOptions | F,
contextOrFn?: Context | F,
fn?: F
): ReturnType<F> {
if (typeof optionsOrFn === 'function') {
const span = this.startSpan(
name,
undefined,
ContextAPI.getInstance().active()
);
return optionsOrFn(span);
} else if (typeof contextOrFn === 'function') {
const span = this.startSpan(
name,
optionsOrFn,
ContextAPI.getInstance().active()
);
return contextOrFn(span);
} else {
const span = this.startSpan(name, optionsOrFn, contextOrFn);
return fn!(span);
}
}
}

Expand Down
29 changes: 23 additions & 6 deletions src/trace/ProxyTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { ContextAPI } from '../api/context';
import { Context } from '../context/types';
import { NOOP_TRACER } from './NoopTracer';
import { ProxyTracerProvider } from './ProxyTracerProvider';
Expand All @@ -40,12 +41,28 @@ export class ProxyTracer implements Tracer {

startActiveSpan<F extends (span: Span) => ReturnType<F>>(
name: string,
options: SpanOptions,
context: Context,
fn: F
) {
const span = this.startSpan(name, options, context);
return fn(span);
optionsOrFn: SpanOptions | F,
contextOrFn?: Context | F,
fn?: F
): ReturnType<F> {
if (typeof optionsOrFn === 'function') {
const span = this.startSpan(
name,
undefined,
ContextAPI.getInstance().active()
);
return optionsOrFn(span);
} else if (typeof contextOrFn === 'function') {
const span = this.startSpan(
name,
optionsOrFn,
ContextAPI.getInstance().active()
);
return contextOrFn(span);
} else {
const span = this.startSpan(name, optionsOrFn, contextOrFn);
return fn!(span);
}
}

/**
Expand Down
19 changes: 9 additions & 10 deletions src/trace/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,15 @@ export interface Tracer {
* span.end();
* });
*/
// todo: figure out overloading
// startActiveSpan?<F extends (span: Span) => ReturnType<F>>(
// name: string,
// fn: F
// ): ReturnType<F>;
// startActiveSpan?<F extends (span: Span) => ReturnType<F>>(
// name: string,
// options: SpanOptions,
// fn: F
// ): ReturnType<F>;
startActiveSpan?<F extends (span: Span) => ReturnType<F>>(
name: string,
fn: F
): ReturnType<F>;
startActiveSpan?<F extends (span: Span) => ReturnType<F>>(
name: string,
options: SpanOptions,
fn: F
): ReturnType<F>;
startActiveSpan?<F extends (span: Span) => ReturnType<F>>(
name: string,
options: SpanOptions,
Expand Down

0 comments on commit 81151c8

Please sign in to comment.