@@ -54,46 +54,6 @@ export class Tracer implements api.Tracer {
54
54
this . instrumentationLibrary = instrumentationLibrary ;
55
55
}
56
56
57
- startActiveSpan < F extends ( span : api . Span ) => ReturnType < F > > (
58
- name : string ,
59
- arg2 : F | api . SpanOptions ,
60
- arg3 ?: F | api . Context ,
61
- arg4 ?: F
62
- ) : ReturnType < F > | undefined {
63
- let fn : F | undefined ,
64
- options : api . SpanOptions | undefined ,
65
- activeContext : api . Context | undefined ;
66
-
67
- if ( arguments . length === 2 && typeof arg2 === 'function' ) {
68
- fn = arg2 ;
69
- } else if (
70
- arguments . length === 3 &&
71
- typeof arg2 === 'object' &&
72
- typeof arg3 === 'function'
73
- ) {
74
- options = arg2 ;
75
- fn = arg3 ;
76
- } else if (
77
- arguments . length === 4 &&
78
- typeof arg2 === 'object' &&
79
- typeof arg3 === 'object' &&
80
- typeof arg4 === 'function'
81
- ) {
82
- options = arg2 ;
83
- activeContext = arg3 ;
84
- fn = arg4 ;
85
- }
86
-
87
- const parentContext = activeContext ?? api . context . active ( ) ;
88
- const span = this . startSpan ( name , options , parentContext ) ;
89
- const contextWithSpanSet = api . trace . setSpan ( parentContext , span ) ;
90
-
91
- if ( fn ) {
92
- return api . context . with ( contextWithSpanSet , fn , undefined , span ) ;
93
- }
94
- return ;
95
- }
96
-
97
57
/**
98
58
* Starts a new Span or returns the default NoopSpan based on the sampling
99
59
* decision.
@@ -163,6 +123,94 @@ export class Tracer implements api.Tracer {
163
123
return span ;
164
124
}
165
125
126
+ /**
127
+ * Starts a new {@link Span} and calls the given function passing it the
128
+ * created span as first argument.
129
+ * Additionally the new span gets set in context and this context is activated
130
+ * for the duration of the function call.
131
+ *
132
+ * @param name The name of the span
133
+ * @param [options] SpanOptions used for span creation
134
+ * @param [context] Context to use to extract parent
135
+ * @param fn function called in the context of the span and receives the newly created span as an argument
136
+ * @returns return value of fn
137
+ * @example
138
+ * const something = tracer.startActiveSpan('op', span => {
139
+ * try {
140
+ * do some work
141
+ * span.setStatus({code: SpanStatusCode.OK});
142
+ * return something;
143
+ * } catch (err) {
144
+ * span.setStatus({
145
+ * code: SpanStatusCode.ERROR,
146
+ * message: err.message,
147
+ * });
148
+ * throw err;
149
+ * } finally {
150
+ * span.end();
151
+ * }
152
+ * });
153
+ * @example
154
+ * const span = tracer.startActiveSpan('op', span => {
155
+ * try {
156
+ * do some work
157
+ * return span;
158
+ * } catch (err) {
159
+ * span.setStatus({
160
+ * code: SpanStatusCode.ERROR,
161
+ * message: err.message,
162
+ * });
163
+ * throw err;
164
+ * }
165
+ * });
166
+ * do some more work
167
+ * span.end();
168
+ */
169
+ startActiveSpan < F extends ( span : api . Span ) => ReturnType < F > > (
170
+ name : string ,
171
+ fn : F
172
+ ) : ReturnType < F > ;
173
+ startActiveSpan < F extends ( span : api . Span ) => ReturnType < F > > (
174
+ name : string ,
175
+ opts : api . SpanOptions ,
176
+ fn : F
177
+ ) : ReturnType < F > ;
178
+ startActiveSpan < F extends ( span : api . Span ) => ReturnType < F > > (
179
+ name : string ,
180
+ opts : api . SpanOptions ,
181
+ ctx : api . Context ,
182
+ fn : F
183
+ ) : ReturnType < F > ;
184
+ startActiveSpan < F extends ( span : api . Span ) => ReturnType < F > > (
185
+ name : string ,
186
+ arg2 ?: F | api . SpanOptions ,
187
+ arg3 ?: F | api . Context ,
188
+ arg4 ?: F
189
+ ) : ReturnType < F > | undefined {
190
+ let opts : api . SpanOptions | undefined ;
191
+ let ctx : api . Context | undefined ;
192
+ let fn : F ;
193
+
194
+ if ( arguments . length < 2 ) {
195
+ return ;
196
+ } else if ( arguments . length === 2 ) {
197
+ fn = arg2 as F ;
198
+ } else if ( arguments . length === 3 ) {
199
+ opts = arg2 as api . SpanOptions | undefined ;
200
+ fn = arg3 as F ;
201
+ } else {
202
+ opts = arg2 as api . SpanOptions | undefined ;
203
+ ctx = arg3 as api . Context | undefined ;
204
+ fn = arg4 as F ;
205
+ }
206
+
207
+ const parentContext = ctx ?? api . context . active ( ) ;
208
+ const span = this . startSpan ( name , opts , parentContext ) ;
209
+ const contextWithSpanSet = api . trace . setSpan ( parentContext , span ) ;
210
+
211
+ return api . context . with ( contextWithSpanSet , fn , undefined , span ) ;
212
+ }
213
+
166
214
/** Returns the active {@link SpanLimits}. */
167
215
getSpanLimits ( ) : SpanLimits {
168
216
return this . _spanLimits ;
0 commit comments