Skip to content

Commit 8be6f8f

Browse files
committed
it finally works :)
1 parent da019a8 commit 8be6f8f

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

packages/tracing/src/idletransaction.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,39 @@ import { SpanRecorder, Transaction } from './transaction';
1313
export class IdleTransactionSpanRecorder extends SpanRecorder {
1414
private readonly _pushActivity?: (id: string) => void;
1515
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+
) {
1824
super(maxlen);
1925
this._pushActivity = pushActivity;
2026
this._popActivity = popActivity;
27+
this.spanId = spanId;
2128
}
2229

2330
/**
2431
* @inheritDoc
2532
*/
2633
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+
}
3342

3443
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+
}
3849
}
3950
}
4051
}
@@ -109,7 +120,7 @@ export class IdleTransaction extends Transaction {
109120
);
110121
this.setStatus(SpanStatus.DeadlineExceeded);
111122
this.setTag('heartbeat', 'failed');
112-
this._finishIdleTransaction(timestampWithMs());
123+
this.finishIdleTransaction(timestampWithMs());
113124
} else {
114125
this._pingHeartbeat();
115126
}
@@ -119,6 +130,7 @@ export class IdleTransaction extends Transaction {
119130
* Pings the heartbeat
120131
*/
121132
private _pingHeartbeat(): void {
133+
logger.log(`ping Heartbeat -> current counter: ${this._heartbeatCounter}`);
122134
this._heartbeatTimer = (setTimeout(() => {
123135
this._beat();
124136
}, 5000) as any) as number;
@@ -142,8 +154,11 @@ export class IdleTransaction extends Transaction {
142154
/**
143155
* Finish the current active idle transaction
144156
*/
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) {
147162
logger.log('[Tracing] finishing IdleTransaction', new Date(endTimestamp * 1000).toISOString());
148163

149164
if (this._finishCallback) {
@@ -174,9 +189,7 @@ export class IdleTransaction extends Transaction {
174189
});
175190

176191
logger.log('[Tracing] flushing IdleTransaction');
177-
this.finish();
178-
this._finished = true;
179-
this._resetActiveTransaction();
192+
this.finish(endTimestamp);
180193
} else {
181194
logger.log('[Tracing] No active IdleTransaction');
182195
}
@@ -188,7 +201,7 @@ export class IdleTransaction extends Transaction {
188201
*/
189202
private _pushActivity(spanId: string): void {
190203
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);
192205
this.activities[spanId] = true;
193206
}
194207

@@ -201,6 +214,7 @@ export class IdleTransaction extends Transaction {
201214
logger.log(`[Tracing] popActivity ${spanId}`);
202215
// tslint:disable-next-line: no-dynamic-delete
203216
delete this.activities[spanId];
217+
logger.log('[Tracing] activities count', Object.keys(this.activities).length);
204218
}
205219

206220
const count = Object.keys(this.activities).length;
@@ -210,7 +224,9 @@ export class IdleTransaction extends Transaction {
210224
// Remember timestampWithMs is in seconds, timeout is in ms
211225
const end = timestampWithMs() + timeout / 1000;
212226
setTimeout(() => {
213-
this._finishIdleTransaction(end);
227+
if (!this._finished) {
228+
this.finishIdleTransaction(end);
229+
}
214230
}, timeout);
215231
}
216232
}
@@ -239,7 +255,7 @@ export class IdleTransaction extends Transaction {
239255
}
240256
};
241257
// 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);
243259
}
244260
this.spanRecorder.add(this);
245261
}

packages/tracing/src/integrations/tracing/backgroundtab.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { getGlobalObject } from '@sentry/utils';
1+
import { getGlobalObject, logger, timestampWithMs, isInstanceOf } from '@sentry/utils';
22

33
import { SpanStatus } from '../../spanstatus';
44

55
import { getActiveTransaction } from './utils';
6+
import { IdleTransaction } from '../../idletransaction';
67

78
const global = getGlobalObject<Window>();
89

@@ -13,12 +14,16 @@ const global = getGlobalObject<Window>();
1314
export function registerBackgroundTabDetection(): void {
1415
if (global && global.document) {
1516
document.addEventListener('visibilitychange', () => {
16-
// TODO: THis will cancel ALL Transactions now, is this desired behaviour?
17-
const activeTransaction = getActiveTransaction();
17+
const activeTransaction = getActiveTransaction() as IdleTransaction;
1818
if (document.hidden && activeTransaction) {
19+
logger.log(`[Tracing] Transaction: ${SpanStatus.Cancelled} -> since tab moved to the background`);
1920
activeTransaction.setStatus(SpanStatus.Cancelled);
2021
activeTransaction.setTag('visibilitychange', 'document.hidden');
21-
activeTransaction.finish();
22+
if (isInstanceOf(activeTransaction, IdleTransaction)) {
23+
activeTransaction.finishIdleTransaction(timestampWithMs());
24+
} else {
25+
activeTransaction.finish();
26+
}
2227
}
2328
});
2429
}

0 commit comments

Comments
 (0)