@@ -72,6 +72,8 @@ export class IdleTransaction extends Transaction {
72
72
73
73
private _finishCallback : Function | undefined = undefined ;
74
74
75
+ private _zeroActivityCounter : number = 0 ;
76
+
75
77
private readonly _idleHub : Hub | undefined ;
76
78
77
79
public constructor ( transactionContext : TransactionContext , hub ?: Hub , idleTimeout : number = 500 ) {
@@ -82,7 +84,7 @@ export class IdleTransaction extends Transaction {
82
84
if ( hub ) {
83
85
// We set the transaction here on the scope so error events pick up the trace
84
86
// context and attach it to the error.
85
- logger . log ( 'Setting transaction on scope' ) ;
87
+ logger . log ( 'Setting idle transaction on scope' ) ;
86
88
hub . configureScope ( scope => scope . setSpan ( this ) ) ;
87
89
}
88
90
@@ -140,6 +142,8 @@ export class IdleTransaction extends Transaction {
140
142
* Unsets the current active transaction + activities
141
143
*/
142
144
private _resetActiveTransaction ( ) : void {
145
+ this . _finished = true ;
146
+ this . activities = { } ;
143
147
if ( this . _idleHub ) {
144
148
const scope = this . _idleHub . getScope ( ) ;
145
149
if ( scope ) {
@@ -155,11 +159,10 @@ export class IdleTransaction extends Transaction {
155
159
* Finish the current active idle transaction
156
160
*/
157
161
public finishIdleTransaction ( endTimestamp : number ) : void {
158
- this . _finished = true ;
159
162
this . _resetActiveTransaction ( ) ;
160
163
161
164
if ( this . spanRecorder ) {
162
- logger . log ( '[Tracing] finishing IdleTransaction' , new Date ( endTimestamp * 1000 ) . toISOString ( ) ) ;
165
+ logger . log ( '[Tracing] finishing IdleTransaction' , new Date ( endTimestamp * 1000 ) . toISOString ( ) , this . op ) ;
163
166
164
167
if ( this . _finishCallback ) {
165
168
this . _finishCallback ( this ) ;
@@ -201,8 +204,8 @@ export class IdleTransaction extends Transaction {
201
204
*/
202
205
private _pushActivity ( spanId : string ) : void {
203
206
logger . log ( `[Tracing] pushActivity: ${ spanId } ` ) ;
204
- logger . log ( '[Tracing] activities count' , Object . keys ( this . activities ) . length ) ;
205
207
this . activities [ spanId ] = true ;
208
+ logger . log ( '[Tracing] new activities count' , Object . keys ( this . activities ) . length ) ;
206
209
}
207
210
208
211
/**
@@ -214,18 +217,20 @@ export class IdleTransaction extends Transaction {
214
217
logger . log ( `[Tracing] popActivity ${ spanId } ` ) ;
215
218
// tslint:disable-next-line: no-dynamic-delete
216
219
delete this . activities [ spanId ] ;
217
- logger . log ( '[Tracing] activities count' , Object . keys ( this . activities ) . length ) ;
220
+ logger . log ( '[Tracing] new activities count' , Object . keys ( this . activities ) . length ) ;
218
221
}
219
222
220
- const count = Object . keys ( this . activities ) . length ;
221
- if ( count === 0 ) {
223
+ if ( Object . keys ( this . activities ) . length === 0 ) {
224
+ this . _zeroActivityCounter += 1 ;
222
225
const timeout = this . _idleTimeout ;
223
226
// We need to add the timeout here to have the real endtimestamp of the transaction
224
227
// Remember timestampWithMs is in seconds, timeout is in ms
225
228
const end = timestampWithMs ( ) + timeout / 1000 ;
226
229
setTimeout ( ( ) => {
227
230
if ( ! this . _finished ) {
228
- this . finishIdleTransaction ( end ) ;
231
+ if ( Object . keys ( this . activities ) . length === 0 || this . _zeroActivityCounter >= 3 ) {
232
+ this . finishIdleTransaction ( end ) ;
233
+ }
229
234
}
230
235
} , timeout ) ;
231
236
}
0 commit comments