@@ -69,6 +69,9 @@ export class IdleTransaction extends Transaction {
69
69
// We should not use heartbeat if we finished a transaction
70
70
private _finished : boolean = false ;
71
71
72
+ // Idle timeout was canceled and we should finish the transaction with the last span end.
73
+ private _idleTimeoutCanceledPermanently : boolean = false ;
74
+
72
75
private readonly _beforeFinishCallbacks : BeforeFinishCallback [ ] = [ ] ;
73
76
74
77
/**
@@ -104,7 +107,7 @@ export class IdleTransaction extends Transaction {
104
107
_idleHub . configureScope ( scope => scope . setSpan ( this ) ) ;
105
108
}
106
109
107
- this . _startIdleTimeout ( ) ;
110
+ this . _restartIdleTimeout ( ) ;
108
111
setTimeout ( ( ) => {
109
112
if ( ! this . _finished ) {
110
113
this . setStatus ( 'deadline_exceeded' ) ;
@@ -203,20 +206,37 @@ export class IdleTransaction extends Transaction {
203
206
}
204
207
205
208
/**
206
- * Cancels the existing idletimeout, if there is one
209
+ * Cancels the existing idle timeout, if there is one.
210
+ * @param restartOnChildSpanChange Default is `true`.
211
+ * If set to false the transaction will end
212
+ * with the last child span.
207
213
*/
208
- private _cancelIdleTimeout ( ) : void {
214
+ public cancelIdleTimeout (
215
+ endTimestamp ?: Parameters < IdleTransaction [ 'finish' ] > [ 0 ] ,
216
+ {
217
+ restartOnChildSpanChange,
218
+ } : {
219
+ restartOnChildSpanChange ?: boolean ;
220
+ } = {
221
+ restartOnChildSpanChange : true ,
222
+ } ,
223
+ ) : void {
209
224
if ( this . _idleTimeoutID ) {
210
225
clearTimeout ( this . _idleTimeoutID ) ;
211
226
this . _idleTimeoutID = undefined ;
227
+ this . _idleTimeoutCanceledPermanently = restartOnChildSpanChange === false ;
228
+
229
+ if ( Object . keys ( this . activities ) . length === 0 && this . _idleTimeoutCanceledPermanently ) {
230
+ this . finish ( endTimestamp ) ;
231
+ }
212
232
}
213
233
}
214
234
215
235
/**
216
- * Creates an idletimeout
236
+ * Restarts idle timeout, if there is no running idle timeout it will start one.
217
237
*/
218
- private _startIdleTimeout ( endTimestamp ?: Parameters < IdleTransaction [ 'finish' ] > [ 0 ] ) : void {
219
- this . _cancelIdleTimeout ( ) ;
238
+ private _restartIdleTimeout ( endTimestamp ?: Parameters < IdleTransaction [ 'finish' ] > [ 0 ] ) : void {
239
+ this . cancelIdleTimeout ( ) ;
220
240
this . _idleTimeoutID = setTimeout ( ( ) => {
221
241
if ( ! this . _finished && Object . keys ( this . activities ) . length === 0 ) {
222
242
this . finish ( endTimestamp ) ;
@@ -229,7 +249,7 @@ export class IdleTransaction extends Transaction {
229
249
* @param spanId The span id that represents the activity
230
250
*/
231
251
private _pushActivity ( spanId : string ) : void {
232
- this . _cancelIdleTimeout ( ) ;
252
+ this . cancelIdleTimeout ( ) ;
233
253
__DEBUG_BUILD__ && logger . log ( `[Tracing] pushActivity: ${ spanId } ` ) ;
234
254
this . activities [ spanId ] = true ;
235
255
__DEBUG_BUILD__ && logger . log ( '[Tracing] new activities count' , Object . keys ( this . activities ) . length ) ;
@@ -248,10 +268,14 @@ export class IdleTransaction extends Transaction {
248
268
}
249
269
250
270
if ( Object . keys ( this . activities ) . length === 0 ) {
251
- // We need to add the timeout here to have the real endtimestamp of the transaction
252
- // Remember timestampWithMs is in seconds, timeout is in ms
253
- const endTimestamp = timestampWithMs ( ) + this . _idleTimeout / 1000 ;
254
- this . _startIdleTimeout ( endTimestamp ) ;
271
+ const endTimestamp = timestampWithMs ( ) ;
272
+ if ( this . _idleTimeoutCanceledPermanently ) {
273
+ this . finish ( endTimestamp ) ;
274
+ } else {
275
+ // We need to add the timeout here to have the real endtimestamp of the transaction
276
+ // Remember timestampWithMs is in seconds, timeout is in ms
277
+ this . _restartIdleTimeout ( endTimestamp + this . _idleTimeout / 1000 ) ;
278
+ }
255
279
}
256
280
}
257
281
0 commit comments