Skip to content

Commit cbddc47

Browse files
committed
fix: Clear activeTransaction from the scope and always start idle timers
1 parent 3456d70 commit cbddc47

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

packages/tracing/src/browser/browsertracing.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ export class BrowserTracing implements Integration {
208208
logger.log(`[Tracing] Will not send ${finalContext.op} transaction because of beforeNavigate.`);
209209
}
210210

211+
logger.log(`[Tracing] Starting ${finalContext.op} transaction on scope`);
212+
211213
const hub = this._getCurrentHub();
212214
const { location } = getGlobalObject() as WindowOrWorkerGlobalScope & { location: Location };
213215

@@ -218,7 +220,6 @@ export class BrowserTracing implements Integration {
218220
true,
219221
{ location }, // for use in the tracesSampler
220222
);
221-
logger.log(`[Tracing] Starting ${finalContext.op} transaction on scope`);
222223
idleTransaction.registerBeforeFinishCallback((transaction, endTimestamp) => {
223224
this._metrics.addPerformanceEntries(transaction);
224225
adjustTransactionDuration(secToMs(maxTransactionDuration), transaction, endTimestamp);

packages/tracing/src/idletransaction.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ export class IdleTransaction extends Transaction {
9393
logger.log(`Setting idle transaction on scope. Span ID: ${this.spanId}`);
9494
_idleHub.configureScope(scope => scope.setSpan(this));
9595
}
96+
97+
this._initTimeout = setTimeout(() => {
98+
if (!this._finished) {
99+
this.finish();
100+
}
101+
}, this._idleTimeout);
96102
}
97103

98104
/** {@inheritDoc} */
@@ -130,16 +136,16 @@ export class IdleTransaction extends Transaction {
130136
return keepSpan;
131137
});
132138

133-
// this._onScope is true if the transaction was previously on the scope.
134-
if (this._onScope) {
135-
clearActiveTransaction(this._idleHub);
136-
}
137-
138139
logger.log('[Tracing] flushing IdleTransaction');
139140
} else {
140141
logger.log('[Tracing] No active IdleTransaction');
141142
}
142143

144+
// this._onScope is true if the transaction was previously on the scope.
145+
if (this._onScope) {
146+
clearActiveTransaction(this._idleHub);
147+
}
148+
143149
return super.finish(endTimestamp);
144150
}
145151

@@ -159,12 +165,6 @@ export class IdleTransaction extends Transaction {
159165
*/
160166
public initSpanRecorder(maxlen?: number): void {
161167
if (!this.spanRecorder) {
162-
this._initTimeout = setTimeout(() => {
163-
if (!this._finished) {
164-
this.finish();
165-
}
166-
}, this._idleTimeout);
167-
168168
const pushActivity = (id: string): void => {
169169
if (this._finished) {
170170
return;

packages/tracing/test/idletransaction.test.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe('IdleTransaction', () => {
150150
const transaction = new IdleTransaction({ name: 'foo', startTimestamp: 1234 }, hub, 1000);
151151
transaction.initSpanRecorder(10);
152152

153-
jest.runTimersToTime(DEFAULT_IDLE_TIMEOUT);
153+
jest.advanceTimersByTime(DEFAULT_IDLE_TIMEOUT);
154154
expect(transaction.endTimestamp).toBeDefined();
155155
});
156156

@@ -159,28 +159,34 @@ describe('IdleTransaction', () => {
159159
transaction.initSpanRecorder(10);
160160
transaction.startChild({});
161161

162-
jest.runTimersToTime(DEFAULT_IDLE_TIMEOUT);
162+
jest.advanceTimersByTime(DEFAULT_IDLE_TIMEOUT);
163163
expect(transaction.endTimestamp).toBeUndefined();
164164
});
165165
});
166166

167167
describe('heartbeat', () => {
168168
it('does not start heartbeat if there is no span recorder', () => {
169-
const transaction = new IdleTransaction({ name: 'foo' }, hub, 1000);
169+
const HEARTBEAT_INTERVAL = 5000;
170+
// 20s to exceed 3 heartbeats
171+
const transaction = new IdleTransaction({ name: 'foo' }, hub, 20000);
170172
const mockFinish = jest.spyOn(transaction, 'finish');
171173

174+
expect(transaction.status).not.toEqual(SpanStatus.DeadlineExceeded);
172175
expect(mockFinish).toHaveBeenCalledTimes(0);
173176

174177
// Beat 1
175-
jest.runOnlyPendingTimers();
178+
jest.advanceTimersByTime(HEARTBEAT_INTERVAL);
179+
expect(transaction.status).not.toEqual(SpanStatus.DeadlineExceeded);
176180
expect(mockFinish).toHaveBeenCalledTimes(0);
177181

178182
// Beat 2
179-
jest.runOnlyPendingTimers();
183+
jest.advanceTimersByTime(HEARTBEAT_INTERVAL);
184+
expect(transaction.status).not.toEqual(SpanStatus.DeadlineExceeded);
180185
expect(mockFinish).toHaveBeenCalledTimes(0);
181186

182187
// Beat 3
183-
jest.runOnlyPendingTimers();
188+
jest.advanceTimersByTime(HEARTBEAT_INTERVAL);
189+
expect(transaction.status).not.toEqual(SpanStatus.DeadlineExceeded);
184190
expect(mockFinish).toHaveBeenCalledTimes(0);
185191
});
186192

0 commit comments

Comments
 (0)