@@ -143,7 +143,16 @@ export function startActiveSpan<T>(context: TransactionContext, callback: (span:
143
143
144
144
/**
145
145
* Similar to `Sentry.startActiveSpan`. Wraps a function with a transaction/span, but does not finish the span
146
- * after the function is done automatically.
146
+ * after the function is done automatically. Use the returned `finish` function to finish the span.
147
+ *
148
+ * ```js
149
+ * Sentry.startActiveSpan({ name: 'foo' }, (span, finish) => {
150
+ * // do work here
151
+ * res.on('finish', () => {
152
+ * finish();
153
+ * });
154
+ * });
155
+ * ```
147
156
*
148
157
* The created span is the active span and will be used as parent by other spans created inside the function
149
158
* and can be accessed via `Sentry.getSpan()`, as long as the function is executed while the scope is active.
@@ -152,7 +161,10 @@ export function startActiveSpan<T>(context: TransactionContext, callback: (span:
152
161
* or you didn't set `tracesSampleRate`, this function will not generate spans
153
162
* and the `span` returned from the callback will be undefined.
154
163
*/
155
- export function startActiveSpanManual < T > ( context : TransactionContext , callback : ( span : Span | undefined ) => T ) : T {
164
+ export function startActiveSpanManual < T > (
165
+ context : TransactionContext ,
166
+ callback : ( span : Span | undefined , finish : ( ) => void ) => T ,
167
+ ) : T {
156
168
const ctx = { ...context } ;
157
169
// If a name is set and a description is not, set the description to the name.
158
170
if ( ctx . name !== undefined && ctx . description === undefined ) {
@@ -174,9 +186,14 @@ export function startActiveSpanManual<T>(context: TransactionContext, callback:
174
186
const activeSpan = getChildSpanOrTransaction ( ) ;
175
187
scope . setSpan ( activeSpan ) ;
176
188
189
+ function finishAndSetSpan ( ) : void {
190
+ activeSpan && activeSpan . finish ( ) ;
191
+ hub . getScope ( ) . setSpan ( parentSpan ) ;
192
+ }
193
+
177
194
let maybePromiseResult : T ;
178
195
try {
179
- maybePromiseResult = callback ( activeSpan ) ;
196
+ maybePromiseResult = callback ( activeSpan , finishAndSetSpan ) ;
180
197
} catch ( e ) {
181
198
activeSpan && activeSpan . setStatus ( 'internal_error' ) ;
182
199
throw e ;
0 commit comments