Skip to content

Commit 3828e5c

Browse files
committed
zero counter
1 parent 8be6f8f commit 3828e5c

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

packages/tracing/src/idletransaction.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ export class IdleTransaction extends Transaction {
7272

7373
private _finishCallback: Function | undefined = undefined;
7474

75+
private _zeroActivityCounter: number = 0;
76+
7577
private readonly _idleHub: Hub | undefined;
7678

7779
public constructor(transactionContext: TransactionContext, hub?: Hub, idleTimeout: number = 500) {
@@ -82,7 +84,7 @@ export class IdleTransaction extends Transaction {
8284
if (hub) {
8385
// We set the transaction here on the scope so error events pick up the trace
8486
// context and attach it to the error.
85-
logger.log('Setting transaction on scope');
87+
logger.log('Setting idle transaction on scope');
8688
hub.configureScope(scope => scope.setSpan(this));
8789
}
8890

@@ -140,6 +142,8 @@ export class IdleTransaction extends Transaction {
140142
* Unsets the current active transaction + activities
141143
*/
142144
private _resetActiveTransaction(): void {
145+
this._finished = true;
146+
this.activities = {};
143147
if (this._idleHub) {
144148
const scope = this._idleHub.getScope();
145149
if (scope) {
@@ -155,11 +159,10 @@ export class IdleTransaction extends Transaction {
155159
* Finish the current active idle transaction
156160
*/
157161
public finishIdleTransaction(endTimestamp: number): void {
158-
this._finished = true;
159162
this._resetActiveTransaction();
160163

161164
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);
163166

164167
if (this._finishCallback) {
165168
this._finishCallback(this);
@@ -201,8 +204,8 @@ export class IdleTransaction extends Transaction {
201204
*/
202205
private _pushActivity(spanId: string): void {
203206
logger.log(`[Tracing] pushActivity: ${spanId}`);
204-
logger.log('[Tracing] activities count', Object.keys(this.activities).length);
205207
this.activities[spanId] = true;
208+
logger.log('[Tracing] new activities count', Object.keys(this.activities).length);
206209
}
207210

208211
/**
@@ -214,18 +217,20 @@ export class IdleTransaction extends Transaction {
214217
logger.log(`[Tracing] popActivity ${spanId}`);
215218
// tslint:disable-next-line: no-dynamic-delete
216219
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);
218221
}
219222

220-
const count = Object.keys(this.activities).length;
221-
if (count === 0) {
223+
if (Object.keys(this.activities).length === 0) {
224+
this._zeroActivityCounter += 1;
222225
const timeout = this._idleTimeout;
223226
// We need to add the timeout here to have the real endtimestamp of the transaction
224227
// Remember timestampWithMs is in seconds, timeout is in ms
225228
const end = timestampWithMs() + timeout / 1000;
226229
setTimeout(() => {
227230
if (!this._finished) {
228-
this.finishIdleTransaction(end);
231+
if (Object.keys(this.activities).length === 0 || this._zeroActivityCounter >= 3) {
232+
this.finishIdleTransaction(end);
233+
}
229234
}
230235
}, timeout);
231236
}

packages/tracing/src/integrations/tracing/router.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export class RouterTracing implements RoutingInstrumentation {
175175
): void {
176176
if (this._activeTransaction) {
177177
logger.log(`[Tracing] force ending previous transaction`);
178-
this._activeTransaction.finish(timestampWithMs());
178+
this._activeTransaction.finishIdleTransaction(timestampWithMs());
179179
}
180180
logger.log(`[Tracing] starting navigation transaction`);
181181
this._activeTransaction = this.startIdleTransaction(hub, 'navigation', transactionContext);
@@ -194,12 +194,14 @@ export class RouterTracing implements RoutingInstrumentation {
194194
): void {
195195
const { startTransactionOnPageLoad, startTransactionOnLocationChange } = this.options;
196196
if (startTransactionOnPageLoad) {
197+
logger.log('START PAGELOAD!!');
197198
this.startPageloadTransaction(hub, beforeFinish, transactionContext);
198199
}
199200

200201
addInstrumentationHandler({
201202
callback: () => {
202203
if (startTransactionOnLocationChange) {
204+
logger.log('START NAVIGATION!!');
203205
this.startNavigationTransaction(hub, beforeFinish, transactionContext);
204206
}
205207
},

0 commit comments

Comments
 (0)