17
17
import * as assert from 'assert' ;
18
18
import * as sinon from 'sinon' ;
19
19
import {
20
- ProxyTracerProvider ,
21
- SpanKind ,
22
- TracerProvider ,
23
- ProxyTracer ,
24
- Tracer ,
25
- Span ,
20
+ Context ,
26
21
NoopTracer ,
22
+ ProxyTracer ,
23
+ ProxyTracerProvider ,
27
24
ROOT_CONTEXT ,
25
+ Span ,
26
+ SpanKind ,
28
27
SpanOptions ,
28
+ Tracer ,
29
+ TracerProvider ,
29
30
} from '../../src' ;
31
+ import { ContextAPI } from '../../src/api/context' ;
32
+ import { getSpan , setSpan } from '../../src/trace/context-utils' ;
30
33
import { NonRecordingSpan } from '../../src/trace/NonRecordingSpan' ;
31
-
32
34
describe ( 'ProxyTracer' , ( ) => {
33
35
let provider : ProxyTracerProvider ;
34
36
const sandbox = sinon . createSandbox ( ) ;
@@ -97,8 +99,48 @@ describe('ProxyTracer', () => {
97
99
return delegateSpan ;
98
100
} ,
99
101
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 ;
102
144
} ,
103
145
} ;
104
146
@@ -118,6 +160,12 @@ describe('ProxyTracer', () => {
118
160
assert . strictEqual ( span , delegateSpan ) ;
119
161
} ) ;
120
162
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
+
121
169
it ( 'should pass original arguments to DelegateTracer#startSpan' , ( ) => {
122
170
const startSpanStub = sandbox . stub ( delegateTracer , 'startSpan' ) ;
123
171
0 commit comments