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

Commit 81151c8

Browse files
author
naseemkullah
committed
feat: configure overloads
the user can call startActiveTracer with 2, 3 or 4 args, with the last being the function Signed-off-by: naseemkullah <[email protected]>
1 parent 17ad5a7 commit 81151c8

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

src/trace/NoopTracer.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { getSpanContext } from '../trace/context-utils';
17+
import { ContextAPI } from '../api/context';
1818
import { Context } from '../context/types';
19+
import { getSpanContext } from '../trace/context-utils';
1920
import { NonRecordingSpan } from './NonRecordingSpan';
2021
import { Span } from './span';
2122
import { isSpanContextValid } from './spancontext-utils';
@@ -48,12 +49,28 @@ export class NoopTracer implements Tracer {
4849

4950
startActiveSpan<F extends (span: Span) => ReturnType<F>>(
5051
name: string,
51-
options: SpanOptions,
52-
context: Context,
53-
fn: F
54-
) {
55-
const span = this.startSpan(name, options, context);
56-
return fn(span);
52+
optionsOrFn: SpanOptions | F,
53+
contextOrFn?: Context | F,
54+
fn?: F
55+
): ReturnType<F> {
56+
if (typeof optionsOrFn === 'function') {
57+
const span = this.startSpan(
58+
name,
59+
undefined,
60+
ContextAPI.getInstance().active()
61+
);
62+
return optionsOrFn(span);
63+
} else if (typeof contextOrFn === 'function') {
64+
const span = this.startSpan(
65+
name,
66+
optionsOrFn,
67+
ContextAPI.getInstance().active()
68+
);
69+
return contextOrFn(span);
70+
} else {
71+
const span = this.startSpan(name, optionsOrFn, contextOrFn);
72+
return fn!(span);
73+
}
5774
}
5875
}
5976

src/trace/ProxyTracer.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

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

4142
startActiveSpan<F extends (span: Span) => ReturnType<F>>(
4243
name: string,
43-
options: SpanOptions,
44-
context: Context,
45-
fn: F
46-
) {
47-
const span = this.startSpan(name, options, context);
48-
return fn(span);
44+
optionsOrFn: SpanOptions | F,
45+
contextOrFn?: Context | F,
46+
fn?: F
47+
): ReturnType<F> {
48+
if (typeof optionsOrFn === 'function') {
49+
const span = this.startSpan(
50+
name,
51+
undefined,
52+
ContextAPI.getInstance().active()
53+
);
54+
return optionsOrFn(span);
55+
} else if (typeof contextOrFn === 'function') {
56+
const span = this.startSpan(
57+
name,
58+
optionsOrFn,
59+
ContextAPI.getInstance().active()
60+
);
61+
return contextOrFn(span);
62+
} else {
63+
const span = this.startSpan(name, optionsOrFn, contextOrFn);
64+
return fn!(span);
65+
}
4966
}
5067

5168
/**

src/trace/tracer.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,15 @@ export interface Tracer {
5252
* span.end();
5353
* });
5454
*/
55-
// todo: figure out overloading
56-
// startActiveSpan?<F extends (span: Span) => ReturnType<F>>(
57-
// name: string,
58-
// fn: F
59-
// ): ReturnType<F>;
60-
// startActiveSpan?<F extends (span: Span) => ReturnType<F>>(
61-
// name: string,
62-
// options: SpanOptions,
63-
// fn: F
64-
// ): ReturnType<F>;
55+
startActiveSpan?<F extends (span: Span) => ReturnType<F>>(
56+
name: string,
57+
fn: F
58+
): ReturnType<F>;
59+
startActiveSpan?<F extends (span: Span) => ReturnType<F>>(
60+
name: string,
61+
options: SpanOptions,
62+
fn: F
63+
): ReturnType<F>;
6564
startActiveSpan?<F extends (span: Span) => ReturnType<F>>(
6665
name: string,
6766
options: SpanOptions,

0 commit comments

Comments
 (0)