|
| 1 | +import { getClient, spanToJSON, startSpanManual } from '@sentry/core'; |
| 2 | +import { adjustTransactionDuration } from '../../src/js/tracing/onSpanEndUtils'; |
| 3 | +import { setupTestClient } from '../mocks/client'; |
| 4 | + |
| 5 | +jest.mock('react-native', () => { |
| 6 | + return { |
| 7 | + AppState: { |
| 8 | + isAvailable: true, |
| 9 | + currentState: 'active', |
| 10 | + addEventListener: jest.fn(() => ({ remove: jest.fn() })), |
| 11 | + }, |
| 12 | + Platform: { OS: 'ios' }, |
| 13 | + NativeModules: { |
| 14 | + RNSentry: {}, |
| 15 | + }, |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +describe('adjustTransactionDuration', () => { |
| 20 | + beforeEach(() => { |
| 21 | + setupTestClient(); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + jest.clearAllMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('marks span as deadline_exceeded when duration exceeds maxDurationMs', () => { |
| 29 | + const client = getClient()!; |
| 30 | + const span = startSpanManual({ name: 'Test Transaction', forceTransaction: true }, span => span); |
| 31 | + |
| 32 | + const maxDurationMs = 60_000; // 60 seconds |
| 33 | + adjustTransactionDuration(client, span, maxDurationMs); |
| 34 | + |
| 35 | + // End the span 120 seconds after it started (exceeds 60s max) |
| 36 | + const startTimestamp = spanToJSON(span).start_timestamp; |
| 37 | + span.end(startTimestamp + 120); |
| 38 | + |
| 39 | + expect(spanToJSON(span).status).toBe('deadline_exceeded'); |
| 40 | + expect(spanToJSON(span).data).toMatchObject({ maxTransactionDurationExceeded: 'true' }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('does not mark span as deadline_exceeded when duration is within maxDurationMs', () => { |
| 44 | + const client = getClient()!; |
| 45 | + const span = startSpanManual({ name: 'Test Transaction', forceTransaction: true }, span => span); |
| 46 | + |
| 47 | + const maxDurationMs = 60_000; // 60 seconds |
| 48 | + adjustTransactionDuration(client, span, maxDurationMs); |
| 49 | + |
| 50 | + // End the span 30 seconds after it started (within 60s max) |
| 51 | + const startTimestamp = spanToJSON(span).start_timestamp; |
| 52 | + span.end(startTimestamp + 30); |
| 53 | + |
| 54 | + expect(spanToJSON(span).status).not.toBe('deadline_exceeded'); |
| 55 | + expect(spanToJSON(span).data).not.toMatchObject({ maxTransactionDurationExceeded: 'true' }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('does not mark span as deadline_exceeded when duration equals maxDurationMs exactly', () => { |
| 59 | + const client = getClient()!; |
| 60 | + const span = startSpanManual({ name: 'Test Transaction', forceTransaction: true }, span => span); |
| 61 | + |
| 62 | + const maxDurationMs = 60_000; // 60 seconds |
| 63 | + adjustTransactionDuration(client, span, maxDurationMs); |
| 64 | + |
| 65 | + // End the span exactly 60 seconds after it started |
| 66 | + const startTimestamp = spanToJSON(span).start_timestamp; |
| 67 | + span.end(startTimestamp + 60); |
| 68 | + |
| 69 | + expect(spanToJSON(span).status).not.toBe('deadline_exceeded'); |
| 70 | + expect(spanToJSON(span).data).not.toMatchObject({ maxTransactionDurationExceeded: 'true' }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('marks span as deadline_exceeded when duration is negative', () => { |
| 74 | + const client = getClient()!; |
| 75 | + const span = startSpanManual({ name: 'Test Transaction', forceTransaction: true }, span => span); |
| 76 | + |
| 77 | + const maxDurationMs = 60_000; |
| 78 | + adjustTransactionDuration(client, span, maxDurationMs); |
| 79 | + |
| 80 | + // End the span before it started (negative duration) |
| 81 | + const startTimestamp = spanToJSON(span).start_timestamp; |
| 82 | + span.end(startTimestamp - 10); |
| 83 | + |
| 84 | + expect(spanToJSON(span).status).toBe('deadline_exceeded'); |
| 85 | + expect(spanToJSON(span).data).toMatchObject({ maxTransactionDurationExceeded: 'true' }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('correctly handles maxDurationMs in milliseconds not seconds', () => { |
| 89 | + const client = getClient()!; |
| 90 | + const span = startSpanManual({ name: 'Test Transaction', forceTransaction: true }, span => span); |
| 91 | + |
| 92 | + // maxDurationMs = 600_000 ms = 600 seconds = 10 minutes |
| 93 | + // A span lasting 601 seconds should exceed this limit |
| 94 | + const maxDurationMs = 600_000; |
| 95 | + adjustTransactionDuration(client, span, maxDurationMs); |
| 96 | + |
| 97 | + const startTimestamp = spanToJSON(span).start_timestamp; |
| 98 | + span.end(startTimestamp + 601); |
| 99 | + |
| 100 | + expect(spanToJSON(span).status).toBe('deadline_exceeded'); |
| 101 | + expect(spanToJSON(span).data).toMatchObject({ maxTransactionDurationExceeded: 'true' }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('does not mark span when duration is 599 seconds with 600_000ms max', () => { |
| 105 | + const client = getClient()!; |
| 106 | + const span = startSpanManual({ name: 'Test Transaction', forceTransaction: true }, span => span); |
| 107 | + |
| 108 | + // maxDurationMs = 600_000 ms = 600 seconds |
| 109 | + // A span lasting 599 seconds should NOT exceed this limit |
| 110 | + const maxDurationMs = 600_000; |
| 111 | + adjustTransactionDuration(client, span, maxDurationMs); |
| 112 | + |
| 113 | + const startTimestamp = spanToJSON(span).start_timestamp; |
| 114 | + span.end(startTimestamp + 599); |
| 115 | + |
| 116 | + expect(spanToJSON(span).status).not.toBe('deadline_exceeded'); |
| 117 | + expect(spanToJSON(span).data).not.toMatchObject({ maxTransactionDurationExceeded: 'true' }); |
| 118 | + }); |
| 119 | + |
| 120 | + it('does not affect spans from other transactions', () => { |
| 121 | + const client = getClient()!; |
| 122 | + const trackedSpan = startSpanManual({ name: 'Tracked Transaction', forceTransaction: true }, span => span); |
| 123 | + const otherSpan = startSpanManual({ name: 'Other Transaction', forceTransaction: true }, span => span); |
| 124 | + |
| 125 | + const maxDurationMs = 60_000; |
| 126 | + adjustTransactionDuration(client, trackedSpan, maxDurationMs); |
| 127 | + |
| 128 | + // End the other span with a duration that exceeds the limit |
| 129 | + const otherStartTimestamp = spanToJSON(otherSpan).start_timestamp; |
| 130 | + otherSpan.end(otherStartTimestamp + 120); |
| 131 | + |
| 132 | + // Other span should not be affected by adjustTransactionDuration |
| 133 | + expect(spanToJSON(otherSpan).status).not.toBe('deadline_exceeded'); |
| 134 | + |
| 135 | + // End tracked span within limits |
| 136 | + const trackedStartTimestamp = spanToJSON(trackedSpan).start_timestamp; |
| 137 | + trackedSpan.end(trackedStartTimestamp + 30); |
| 138 | + |
| 139 | + expect(spanToJSON(trackedSpan).status).not.toBe('deadline_exceeded'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('handles very short maxDurationMs values', () => { |
| 143 | + const client = getClient()!; |
| 144 | + const span = startSpanManual({ name: 'Test Transaction', forceTransaction: true }, span => span); |
| 145 | + |
| 146 | + // 100ms max duration |
| 147 | + const maxDurationMs = 100; |
| 148 | + adjustTransactionDuration(client, span, maxDurationMs); |
| 149 | + |
| 150 | + // End after 1 second (1000ms > 100ms) |
| 151 | + const startTimestamp = spanToJSON(span).start_timestamp; |
| 152 | + span.end(startTimestamp + 1); |
| 153 | + |
| 154 | + expect(spanToJSON(span).status).toBe('deadline_exceeded'); |
| 155 | + expect(spanToJSON(span).data).toMatchObject({ maxTransactionDurationExceeded: 'true' }); |
| 156 | + }); |
| 157 | +}); |
0 commit comments