@@ -13,28 +13,39 @@ import { SpanRecorder, Transaction } from './transaction';
13
13
export class IdleTransactionSpanRecorder extends SpanRecorder {
14
14
private readonly _pushActivity ?: ( id : string ) => void ;
15
15
private readonly _popActivity ?: ( id : string ) => void ;
16
-
17
- public constructor ( maxlen ?: number , pushActivity ?: ( id : string ) => void , popActivity ?: ( id : string ) => void ) {
16
+ public spanId : string = '' ;
17
+
18
+ public constructor (
19
+ maxlen ?: number ,
20
+ pushActivity ?: ( id : string ) => void ,
21
+ popActivity ?: ( id : string ) => void ,
22
+ spanId : string = '' ,
23
+ ) {
18
24
super ( maxlen ) ;
19
25
this . _pushActivity = pushActivity ;
20
26
this . _popActivity = popActivity ;
27
+ this . spanId = spanId ;
21
28
}
22
29
23
30
/**
24
31
* @inheritDoc
25
32
*/
26
33
public add ( span : Span ) : void {
27
- span . finish = ( endTimestamp ?: number ) => {
28
- span . endTimestamp = typeof endTimestamp === 'number' ? endTimestamp : timestampWithMs ( ) ;
29
- if ( this . _popActivity ) {
30
- this . _popActivity ( span . spanId ) ;
31
- }
32
- } ;
34
+ if ( span . spanId !== this . spanId ) {
35
+ span . finish = ( endTimestamp ?: number ) => {
36
+ span . endTimestamp = typeof endTimestamp === 'number' ? endTimestamp : timestampWithMs ( ) ;
37
+ if ( this . _popActivity ) {
38
+ this . _popActivity ( span . spanId ) ;
39
+ }
40
+ } ;
41
+ }
33
42
34
43
super . add ( span ) ;
35
- if ( span && ! span . endTimestamp ) {
36
- if ( this . _pushActivity ) {
37
- this . _pushActivity ( span . spanId ) ;
44
+ if ( span . spanId !== this . spanId ) {
45
+ if ( span && ! span . endTimestamp ) {
46
+ if ( this . _pushActivity ) {
47
+ this . _pushActivity ( span . spanId ) ;
48
+ }
38
49
}
39
50
}
40
51
}
@@ -109,7 +120,7 @@ export class IdleTransaction extends Transaction {
109
120
) ;
110
121
this . setStatus ( SpanStatus . DeadlineExceeded ) ;
111
122
this . setTag ( 'heartbeat' , 'failed' ) ;
112
- this . _finishIdleTransaction ( timestampWithMs ( ) ) ;
123
+ this . finishIdleTransaction ( timestampWithMs ( ) ) ;
113
124
} else {
114
125
this . _pingHeartbeat ( ) ;
115
126
}
@@ -119,6 +130,7 @@ export class IdleTransaction extends Transaction {
119
130
* Pings the heartbeat
120
131
*/
121
132
private _pingHeartbeat ( ) : void {
133
+ logger . log ( `ping Heartbeat -> current counter: ${ this . _heartbeatCounter } ` ) ;
122
134
this . _heartbeatTimer = ( setTimeout ( ( ) => {
123
135
this . _beat ( ) ;
124
136
} , 5000 ) as any ) as number ;
@@ -142,8 +154,11 @@ export class IdleTransaction extends Transaction {
142
154
/**
143
155
* Finish the current active idle transaction
144
156
*/
145
- private _finishIdleTransaction ( endTimestamp : number ) : void {
146
- if ( this . spanRecorder && ! this . _finished ) {
157
+ public finishIdleTransaction ( endTimestamp : number ) : void {
158
+ this . _finished = true ;
159
+ this . _resetActiveTransaction ( ) ;
160
+
161
+ if ( this . spanRecorder ) {
147
162
logger . log ( '[Tracing] finishing IdleTransaction' , new Date ( endTimestamp * 1000 ) . toISOString ( ) ) ;
148
163
149
164
if ( this . _finishCallback ) {
@@ -174,9 +189,7 @@ export class IdleTransaction extends Transaction {
174
189
} ) ;
175
190
176
191
logger . log ( '[Tracing] flushing IdleTransaction' ) ;
177
- this . finish ( ) ;
178
- this . _finished = true ;
179
- this . _resetActiveTransaction ( ) ;
192
+ this . finish ( endTimestamp ) ;
180
193
} else {
181
194
logger . log ( '[Tracing] No active IdleTransaction' ) ;
182
195
}
@@ -188,7 +201,7 @@ export class IdleTransaction extends Transaction {
188
201
*/
189
202
private _pushActivity ( spanId : string ) : void {
190
203
logger . log ( `[Tracing] pushActivity: ${ spanId } ` ) ;
191
- logger . log ( '[Tracing] activies count' , Object . keys ( this . activities ) . length ) ;
204
+ logger . log ( '[Tracing] activities count' , Object . keys ( this . activities ) . length ) ;
192
205
this . activities [ spanId ] = true ;
193
206
}
194
207
@@ -201,6 +214,7 @@ export class IdleTransaction extends Transaction {
201
214
logger . log ( `[Tracing] popActivity ${ spanId } ` ) ;
202
215
// tslint:disable-next-line: no-dynamic-delete
203
216
delete this . activities [ spanId ] ;
217
+ logger . log ( '[Tracing] activities count' , Object . keys ( this . activities ) . length ) ;
204
218
}
205
219
206
220
const count = Object . keys ( this . activities ) . length ;
@@ -210,7 +224,9 @@ export class IdleTransaction extends Transaction {
210
224
// Remember timestampWithMs is in seconds, timeout is in ms
211
225
const end = timestampWithMs ( ) + timeout / 1000 ;
212
226
setTimeout ( ( ) => {
213
- this . _finishIdleTransaction ( end ) ;
227
+ if ( ! this . _finished ) {
228
+ this . finishIdleTransaction ( end ) ;
229
+ }
214
230
} , timeout ) ;
215
231
}
216
232
}
@@ -239,7 +255,7 @@ export class IdleTransaction extends Transaction {
239
255
}
240
256
} ;
241
257
// tslint:disable-next-line: no-unbound-method
242
- this . spanRecorder = new IdleTransactionSpanRecorder ( maxlen , pushActivity , popActivity ) ;
258
+ this . spanRecorder = new IdleTransactionSpanRecorder ( maxlen , pushActivity , popActivity , this . spanId ) ;
243
259
}
244
260
this . spanRecorder . add ( this ) ;
245
261
}
0 commit comments