Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tracing): Make sure idle transaction does not override other transactions #7725

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 4 additions & 14 deletions packages/core/src/tracing/idletransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ export class IdleTransaction extends Transaction {
super(transactionContext, _idleHub);

if (_onScope) {
// There should only be one active transaction on the scope
clearActiveTransaction(_idleHub);

// We set the transaction here on the scope so error events pick up the trace
// context and attach it to the error.
__DEBUG_BUILD__ && logger.log(`Setting idle transaction on scope. Span ID: ${this.spanId}`);
Expand Down Expand Up @@ -179,7 +176,10 @@ export class IdleTransaction extends Transaction {

// if `this._onScope` is `true`, the transaction put itself on the scope when it started
if (this._onScope) {
clearActiveTransaction(this._idleHub);
const scope = this._idleHub.getScope();
if (scope.getTransaction() === this) {
scope.setSpan(undefined);
}
}

return super.finish(endTimestamp);
Expand Down Expand Up @@ -353,13 +353,3 @@ export class IdleTransaction extends Transaction {
}, this._heartbeatInterval);
}
}

/**
* Reset transaction on scope to `undefined`
*/
function clearActiveTransaction(hub: Hub): void {
const scope = hub.getScope();
if (scope.getTransaction()) {
scope.setSpan(undefined);
}
}
25 changes: 24 additions & 1 deletion packages/tracing/test/idletransaction.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BrowserClient } from '@sentry/browser';
import { TRACING_DEFAULTS } from '@sentry/core';
import { TRACING_DEFAULTS, Transaction } from '@sentry/core';

import { Hub, IdleTransaction, Span } from '../../core/src';
import { IdleTransactionSpanRecorder } from '../../core/src/tracing/idletransaction';
Expand Down Expand Up @@ -75,6 +75,29 @@ describe('IdleTransaction', () => {
expect(s.getTransaction()).toBe(undefined);
});
});

it('does not remove transaction from scope on finish if another transaction was set there', () => {
const transaction = new IdleTransaction(
{ name: 'foo' },
hub,
TRACING_DEFAULTS.idleTimeout,
TRACING_DEFAULTS.finalTimeout,
TRACING_DEFAULTS.heartbeatInterval,
true,
);
transaction.initSpanRecorder(10);

// @ts-ignore need to pass in hub
const otherTransaction = new Transaction({ name: 'bar' }, hub);
hub.getScope().setSpan(otherTransaction);

transaction.finish();
jest.runAllTimers();

hub.configureScope(s => {
expect(s.getTransaction()).toBe(otherTransaction);
});
});
});

beforeEach(() => {
Expand Down