Skip to content

Commit 691b1d2

Browse files
committed
fix(tracing): Trim idle transaction spans if they exceed final timeout
1 parent 5c60612 commit 691b1d2

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

packages/core/src/tracing/idletransaction.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,19 @@ export class IdleTransaction extends Transaction {
158158
logger.log('[Tracing] cancelling span since transaction ended early', JSON.stringify(span, undefined, 2));
159159
}
160160

161-
const keepSpan = span.startTimestamp < endTimestamp;
162-
if (!keepSpan) {
163-
__DEBUG_BUILD__ &&
164-
logger.log(
165-
'[Tracing] discarding Span since it happened after Transaction was finished',
166-
JSON.stringify(span, undefined, 2),
167-
);
161+
const spanStartedBeforeTransactionFinish = span.startTimestamp < endTimestamp;
162+
const spanEndedBeforeFinalTimeout = span.endTimestamp < (this._finalTimeout + this._idleTimeout) / 1000;
163+
164+
if (__DEBUG_BUILD__) {
165+
const stringifiedSpan = JSON.stringify(span, undefined, 2);
166+
if (!spanStartedBeforeTransactionFinish) {
167+
logger.log('[Tracing] discarding Span since it happened after Transaction was finished', stringifiedSpan);
168+
} else if (!spanEndedBeforeFinalTimeout) {
169+
logger.log('[Tracing] discarding Span since it finished after Transaction final timeout', stringifiedSpan);
170+
}
168171
}
169-
return keepSpan;
172+
173+
return spanStartedBeforeTransactionFinish && spanEndedBeforeFinalTimeout;
170174
});
171175

172176
__DEBUG_BUILD__ && logger.log('[Tracing] flushing IdleTransaction');

packages/tracing/test/idletransaction.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,19 @@ describe('IdleTransaction', () => {
202202
}
203203
});
204204

205+
it('filters out spans that exceed final timeout', () => {
206+
const transaction = new IdleTransaction({ name: 'foo', startTimestamp: 1234 }, hub, 1000, 3000);
207+
transaction.initSpanRecorder(10);
208+
209+
const span = transaction.startChild({ startTimestamp: transaction.startTimestamp + 2 });
210+
span.finish(span.startTimestamp + 10 + 30 + 1);
211+
212+
transaction.finish(transaction.startTimestamp + 10);
213+
214+
expect(transaction.spanRecorder).toBeDefined();
215+
expect(transaction.spanRecorder!.spans).toHaveLength(1);
216+
});
217+
205218
it('should record dropped transactions', async () => {
206219
const transaction = new IdleTransaction({ name: 'foo', startTimestamp: 1234, sampled: false }, hub, 1000);
207220

0 commit comments

Comments
 (0)