Skip to content

Commit 95e51a2

Browse files
authored
fix(tracing): Make sure idle transaction does not override other transactions (#7725)
1 parent 5cab9e1 commit 95e51a2

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

packages/core/src/tracing/idletransaction.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ export class IdleTransaction extends Transaction {
111111
super(transactionContext, _idleHub);
112112

113113
if (_onScope) {
114-
// There should only be one active transaction on the scope
115-
clearActiveTransaction(_idleHub);
116-
117114
// We set the transaction here on the scope so error events pick up the trace
118115
// context and attach it to the error.
119116
__DEBUG_BUILD__ && logger.log(`Setting idle transaction on scope. Span ID: ${this.spanId}`);
@@ -179,7 +176,10 @@ export class IdleTransaction extends Transaction {
179176

180177
// if `this._onScope` is `true`, the transaction put itself on the scope when it started
181178
if (this._onScope) {
182-
clearActiveTransaction(this._idleHub);
179+
const scope = this._idleHub.getScope();
180+
if (scope.getTransaction() === this) {
181+
scope.setSpan(undefined);
182+
}
183183
}
184184

185185
return super.finish(endTimestamp);
@@ -353,13 +353,3 @@ export class IdleTransaction extends Transaction {
353353
}, this._heartbeatInterval);
354354
}
355355
}
356-
357-
/**
358-
* Reset transaction on scope to `undefined`
359-
*/
360-
function clearActiveTransaction(hub: Hub): void {
361-
const scope = hub.getScope();
362-
if (scope.getTransaction()) {
363-
scope.setSpan(undefined);
364-
}
365-
}

packages/tracing/test/idletransaction.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BrowserClient } from '@sentry/browser';
2-
import { TRACING_DEFAULTS } from '@sentry/core';
2+
import { TRACING_DEFAULTS, Transaction } from '@sentry/core';
33

44
import { Hub, IdleTransaction, Span } from '../../core/src';
55
import { IdleTransactionSpanRecorder } from '../../core/src/tracing/idletransaction';
@@ -75,6 +75,29 @@ describe('IdleTransaction', () => {
7575
expect(s.getTransaction()).toBe(undefined);
7676
});
7777
});
78+
79+
it('does not remove transaction from scope on finish if another transaction was set there', () => {
80+
const transaction = new IdleTransaction(
81+
{ name: 'foo' },
82+
hub,
83+
TRACING_DEFAULTS.idleTimeout,
84+
TRACING_DEFAULTS.finalTimeout,
85+
TRACING_DEFAULTS.heartbeatInterval,
86+
true,
87+
);
88+
transaction.initSpanRecorder(10);
89+
90+
// @ts-ignore need to pass in hub
91+
const otherTransaction = new Transaction({ name: 'bar' }, hub);
92+
hub.getScope().setSpan(otherTransaction);
93+
94+
transaction.finish();
95+
jest.runAllTimers();
96+
97+
hub.configureScope(s => {
98+
expect(s.getTransaction()).toBe(otherTransaction);
99+
});
100+
});
78101
});
79102

80103
beforeEach(() => {

0 commit comments

Comments
 (0)