1717import * as assert from 'assert' ;
1818import * as sinon from 'sinon' ;
1919import {
20- ProxyTracerProvider ,
21- SpanKind ,
22- TracerProvider ,
23- ProxyTracer ,
24- Tracer ,
25- Span ,
20+ Context ,
2621 NoopTracer ,
22+ ProxyTracer ,
23+ ProxyTracerProvider ,
2724 ROOT_CONTEXT ,
25+ Span ,
26+ SpanKind ,
2827 SpanOptions ,
28+ Tracer ,
29+ TracerProvider ,
2930} from '../../src' ;
31+ import { ContextAPI } from '../../src/api/context' ;
32+ import { getSpan , setSpan } from '../../src/trace/context-utils' ;
3033import { NonRecordingSpan } from '../../src/trace/NonRecordingSpan' ;
31-
3234describe ( 'ProxyTracer' , ( ) => {
3335 let provider : ProxyTracerProvider ;
3436 const sandbox = sinon . createSandbox ( ) ;
@@ -97,8 +99,48 @@ describe('ProxyTracer', () => {
9799 return delegateSpan ;
98100 } ,
99101
100- startActiveSpan ( ) {
101- return Reflect . apply ( tracer . startActiveSpan , tracer , arguments ) ;
102+ startActiveSpan < F extends ( span : Span ) => ReturnType < F > > (
103+ name : string ,
104+ arg2 : F | SpanOptions ,
105+ arg3 ?: F | Context ,
106+ arg4 ?: F
107+ ) : ReturnType < F > | undefined {
108+ let fn : F | undefined ,
109+ options : SpanOptions | undefined ,
110+ activeContext : Context | undefined ;
111+ if ( arguments . length === 2 && typeof arg2 === 'function' ) {
112+ fn = arg2 ;
113+ } else if (
114+ arguments . length === 3 &&
115+ typeof arg2 === 'object' &&
116+ typeof arg3 === 'function'
117+ ) {
118+ options = arg2 ;
119+ fn = arg3 ;
120+ } else if (
121+ arguments . length === 4 &&
122+ typeof arg2 === 'object' &&
123+ typeof arg3 === 'object' &&
124+ typeof arg4 === 'function'
125+ ) {
126+ options = arg2 ;
127+ activeContext = arg3 ;
128+ fn = arg4 ;
129+ }
130+
131+ const contextApiSingleton = ContextAPI . getInstance ( ) ;
132+ const activeCtx = activeContext ?? contextApiSingleton . active ( ) ;
133+ const span = this . startSpan ( name , options , activeCtx ) ;
134+
135+ if ( fn ) {
136+ return contextApiSingleton . with (
137+ setSpan ( activeCtx , span ) ,
138+ fn ,
139+ undefined ,
140+ span
141+ ) ;
142+ }
143+ return ;
102144 } ,
103145 } ;
104146
@@ -118,6 +160,12 @@ describe('ProxyTracer', () => {
118160 assert . strictEqual ( span , delegateSpan ) ;
119161 } ) ;
120162
163+ it ( 'should create active spans using the delegate tracer' , ( ) => {
164+ tracer . startActiveSpan ( 'test' , span => {
165+ assert . strictEqual ( getSpan ( ContextAPI . getInstance ( ) . active ( ) ) , span ) ;
166+ } ) ;
167+ } ) ;
168+
121169 it ( 'should pass original arguments to DelegateTracer#startSpan' , ( ) => {
122170 const startSpanStub = sandbox . stub ( delegateTracer , 'startSpan' ) ;
123171
0 commit comments