Skip to content

changed(tracing): always clear timeout when new activity starts within idle timeout #4450

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions packages/tracing/src/idletransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class IdleTransaction extends Transaction {
* If a transaction is created and no activities are added, we want to make sure that
* it times out properly. This is cleared and not used when activities are added.
*/
private _initTimeout: ReturnType<typeof setTimeout> | undefined;
private _idleTimeout: ReturnType<typeof setTimeout> | undefined;

public constructor(
transactionContext: TransactionContext,
Expand All @@ -80,7 +80,7 @@ export class IdleTransaction extends Transaction {
* The time to wait in ms until the idle transaction will be finished.
* @default 1000
*/
private readonly _idleTimeout: number = DEFAULT_IDLE_TIMEOUT,
private readonly _idleTimeoutDuration: number = DEFAULT_IDLE_TIMEOUT,
// Whether or not the transaction should put itself on the scope when it starts and pop itself off when it ends
private readonly _onScope: boolean = false,
) {
Expand All @@ -96,11 +96,11 @@ export class IdleTransaction extends Transaction {
_idleHub.configureScope(scope => scope.setSpan(this));
}

this._initTimeout = setTimeout(() => {
this._idleTimeout = setTimeout(() => {
if (!this._finished) {
this.finish();
}
}, this._idleTimeout);
}, this._idleTimeoutDuration);
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -194,9 +194,9 @@ export class IdleTransaction extends Transaction {
* @param spanId The span id that represents the activity
*/
private _pushActivity(spanId: string): void {
if (this._initTimeout) {
clearTimeout(this._initTimeout);
this._initTimeout = undefined;
if (this._idleTimeout) {
clearTimeout(this._idleTimeout);
this._idleTimeout = undefined;
}
logger.log(`[Tracing] pushActivity: ${spanId}`);
this.activities[spanId] = true;
Expand All @@ -216,12 +216,15 @@ export class IdleTransaction extends Transaction {
}

if (Object.keys(this.activities).length === 0) {
const timeout = this._idleTimeout;
const timeout = this._idleTimeoutDuration;
// We need to add the timeout here to have the real endtimestamp of the transaction
// Remember timestampWithMs is in seconds, timeout is in ms
const end = timestampWithMs() + timeout / 1000;

setTimeout(() => {
if (this._idleTimeout) {
clearTimeout(this._idleTimeout);
}
this._idleTimeout = setTimeout(() => {
if (!this._finished) {
this.setTag(FINISH_REASON_TAG, IDLE_TRANSACTION_FINISH_REASONS[1]);
this.finish(end);
Expand Down
8 changes: 7 additions & 1 deletion packages/tracing/test/idletransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,16 @@ describe('IdleTransaction', () => {
it('does not finish if a activity is started', () => {
const transaction = new IdleTransaction({ name: 'foo', startTimestamp: 1234 }, hub, DEFAULT_IDLE_TIMEOUT);
transaction.initSpanRecorder(10);
transaction.startChild({});
const span = transaction.startChild({});
span.finish();
const span2 = transaction.startChild({});

jest.advanceTimersByTime(DEFAULT_IDLE_TIMEOUT);
expect(transaction.endTimestamp).toBeUndefined();

span2.finish();
jest.advanceTimersByTime(DEFAULT_IDLE_TIMEOUT + DEFAULT_IDLE_TIMEOUT);
expect(transaction.endTimestamp).toBeDefined();
});
});

Expand Down