Skip to content

Commit

Permalink
fix(tracing): Trim idle transaction spans if they exceed final timeout (
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Jul 27, 2023
1 parent 40fe2a0 commit aff5677
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
23 changes: 15 additions & 8 deletions packages/core/src/tracing/idletransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,22 @@ export class IdleTransaction extends Transaction {
logger.log('[Tracing] cancelling span since transaction ended early', JSON.stringify(span, undefined, 2));
}

const keepSpan = span.startTimestamp < endTimestamp;
if (!keepSpan) {
__DEBUG_BUILD__ &&
logger.log(
'[Tracing] discarding Span since it happened after Transaction was finished',
JSON.stringify(span, undefined, 2),
);
const spanStartedBeforeTransactionFinish = span.startTimestamp < endTimestamp;

// Add a delta with idle timeout so that we prevent false positives
const timeoutWithMarginOfError = (this._finalTimeout + this._idleTimeout) / 1000;
const spanEndedBeforeFinalTimeout = span.endTimestamp - this.startTimestamp < timeoutWithMarginOfError;

if (__DEBUG_BUILD__) {
const stringifiedSpan = JSON.stringify(span, undefined, 2);
if (!spanStartedBeforeTransactionFinish) {
logger.log('[Tracing] discarding Span since it happened after Transaction was finished', stringifiedSpan);
} else if (!spanEndedBeforeFinalTimeout) {
logger.log('[Tracing] discarding Span since it finished after Transaction final timeout', stringifiedSpan);
}
}
return keepSpan;

return spanStartedBeforeTransactionFinish && spanEndedBeforeFinalTimeout;
});

__DEBUG_BUILD__ && logger.log('[Tracing] flushing IdleTransaction');
Expand Down
13 changes: 13 additions & 0 deletions packages/tracing/test/idletransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ describe('IdleTransaction', () => {
}
});

it('filters out spans that exceed final timeout', () => {
const transaction = new IdleTransaction({ name: 'foo', startTimestamp: 1234 }, hub, 1000, 3000);
transaction.initSpanRecorder(10);

const span = transaction.startChild({ startTimestamp: transaction.startTimestamp + 2 });
span.finish(span.startTimestamp + 10 + 30 + 1);

transaction.finish(transaction.startTimestamp + 50);

expect(transaction.spanRecorder).toBeDefined();
expect(transaction.spanRecorder!.spans).toHaveLength(1);
});

it('should record dropped transactions', async () => {
const transaction = new IdleTransaction({ name: 'foo', startTimestamp: 1234, sampled: false }, hub, 1000);

Expand Down

0 comments on commit aff5677

Please sign in to comment.