Skip to content

Commit

Permalink
feat: Add a timeout for auto-generated transactions
Browse files Browse the repository at this point in the history
Closes #1173
  • Loading branch information
kevinrenskers committed Dec 14, 2022
1 parent 53e149f commit 29bde6c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Sources/Sentry/SentryTracer.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/
static const NSTimeInterval SENTRY_APP_START_MEASUREMENT_DIFFERENCE = 5.0;
static const NSTimeInterval SENTRY_AUTO_TRANSACTION_MAX_DURATION = 500.0;
static const NSTimeInterval SENTRY_AUTO_TRANSACTION_DEADLINE = 30.0;

@interface
SentryTracer ()
Expand All @@ -51,6 +52,7 @@
@property (nonatomic) BOOL wasFinishCalled;
@property (nonatomic) NSTimeInterval idleTimeout;
@property (nonatomic, nullable, strong) SentryDispatchQueueWrapper *dispatchQueueWrapper;
@property (nonatomic, nullable, strong) NSTimer *deadlineTimer;

@end

Expand Down Expand Up @@ -166,6 +168,10 @@ - (instancetype)initWithTransactionContext:(SentryTransactionContext *)transacti
[self dispatchIdleTimeout];
}

if ([self isAutoGeneratedTransaction]) {
[self startDeadlineTimer];
}

#if SENTRY_HAS_UIKIT
_startTimeChanged = NO;

Expand Down Expand Up @@ -217,6 +223,33 @@ - (void)cancelIdleTimeout
}
}

- (void)startDeadlineTimer
{
self.deadlineTimer = [NSTimer scheduledTimerWithTimeInterval:SENTRY_AUTO_TRANSACTION_DEADLINE
target:self
selector:@selector(deadlineTimerFired)
userInfo:nil
repeats:NO];
}

- (void)deadlineTimerFired
{
@synchronized(_children) {
for (id<SentrySpan> span in _children) {
if (![span isFinished])
[span finishWithStatus:kSentrySpanStatusDeadlineExceeded];
}
}

[self finishWithStatus:kSentrySpanStatusDeadlineExceeded];
}

- (void)cancelDeadlineTimer
{
[self.deadlineTimer invalidate];
self.deadlineTimer = nil;
}

- (id<SentrySpan>)getActiveSpan
{
id<SentrySpan> span;
Expand Down Expand Up @@ -485,6 +518,7 @@ - (void)finishWithStatus:(SentrySpanStatus)status
self.wasFinishCalled = YES;
_finishStatus = status;
[self cancelIdleTimeout];
[self cancelDeadlineTimer];
[self canBeFinished];
}

Expand Down

0 comments on commit 29bde6c

Please sign in to comment.