Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- Discard long-lasting auto-generated transactions (#1903)
- Unset scope span when finishing idle transaction (#1902)
- Set max app start duration to 60s (#1899)

Expand Down
22 changes: 22 additions & 0 deletions Sources/Sentry/SentryTracer.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* transaction are allowed to be apart.
*/
static const NSTimeInterval SENTRY_APP_START_MEASUREMENT_DIFFERENCE = 5.0;
static const NSTimeInterval SENTRY_AUTO_TRANSACTION_MAX_DURATION = 500.0;

@interface
SentryTracer ()
Expand Down Expand Up @@ -181,6 +182,11 @@ - (BOOL)hasIdleTimeout
return self.idleTimeout > 0 && self.dispatchQueueWrapper != nil;
}

- (BOOL)isAutoGeneratedTransaction
{
return self.waitForChildren || [self hasIdleTimeout];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't waitForChildren a public api folks can use?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it isn't.

}

- (void)cancelIdleTimeout
{
if ([self hasIdleTimeout]) {
Expand Down Expand Up @@ -430,6 +436,21 @@ - (void)finishInternal
}

SentryTransaction *transaction = [self toTransaction];

// Prewarming can execute code up to viewDidLoad of a UIViewController, and keep the app in the
// background. This can lead to auto-generated transactions lasting for minutes or event hours.
// Therefore, we drop transactions lasting longer than SENTRY_AUTO_TRANSACTION_MAX_DURATION.
NSTimeInterval transactionDuration = [self.timestamp timeIntervalSinceDate:self.startTimestamp];
if ([self isAutoGeneratedTransaction]
&& transactionDuration >= SENTRY_AUTO_TRANSACTION_MAX_DURATION) {
NSString *message =
[NSString stringWithFormat:@"Auto generated transaction exceeded the max duration of "
@"%f seconds. Not capturing transaction.",
SENTRY_AUTO_TRANSACTION_MAX_DURATION];
[SentryLog logWithMessage:message andLevel:kSentryLevelInfo];
return;
}

NSMutableArray<SentryEnvelopeItem *> *additionalEnvelopeItems = [NSMutableArray array];
#if SENTRY_TARGET_PROFILING_SUPPORTED
if ([_hub getClient].options.enableProfiling) {
Expand All @@ -444,6 +465,7 @@ - (void)finishInternal
[profilerLock unlock];
}
#endif

[_hub captureTransaction:transaction
withScope:_hub.scope
additionalEnvelopeItems:additionalEnvelopeItems];
Expand Down
50 changes: 46 additions & 4 deletions Tests/SentryTests/Performance/SentryTracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,49 @@ class SentryTracerTests: XCTestCase {
XCTAssertEqual(0, fixture.hub.capturedEventsWithScopes.count)
}

func testFinish_WaitForAllChildren_ExceedsMaxDuration_NoTransactionCaptured() {
let sut = fixture.getSut()

advanceTime(bySeconds: 500)

sut.finish()

assertTransactionNotCaptured(sut)
}

func testFinish_WaitForAllChildren_DoesNotExceedsMaxDuration_TransactionCaptured() {
let sut = fixture.getSut()

advanceTime(bySeconds: 499.9)

sut.finish()

assertOneTransactionCaptured(sut)
}

func testFinish_WaitForAllChildren_StartTimeModified_NoTransactionCaptured() {
let appStartMeasurement = fixture.getAppStartMeasurement(type: .cold)
SentrySDK.setAppStartMeasurement(appStartMeasurement)
advanceTime(bySeconds: 1)

let sut = fixture.getSut()
advanceTime(bySeconds: 499)

sut.finish()

assertTransactionNotCaptured(sut)
}

func testFinish_IdleTimeout_ExceedsMaxDuration_NoTransactionCaptured() {
let sut = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)

advanceTime(bySeconds: 500)

sut.finish()

assertTransactionNotCaptured(sut)
}

func testIdleTimeout_NoChildren_TransactionNotCaptured() {
_ = fixture.getSut(idleTimeout: fixture.idleTimeout, dispatchQueueWrapper: fixture.dispatchQueue)

Expand Down Expand Up @@ -564,7 +607,7 @@ class SentryTracerTests: XCTestCase {
func testChangeStartTimeStamp_RemovesFramesMeasurement() {
let sut = fixture.getSut()
fixture.displayLinkWrapper.givenFrames(1, 1, 1)
sut.startTimestamp = Date(timeIntervalSince1970: 0)
sut.startTimestamp = sut.startTimestamp?.addingTimeInterval(-1)

sut.finish()

Expand Down Expand Up @@ -681,7 +724,6 @@ class SentryTracerTests: XCTestCase {

private func assertTransactionNotCaptured(_ tracer: SentryTracer) {
fixture.hub.group.wait()
XCTAssertFalse(tracer.isFinished)
XCTAssertEqual(0, fixture.hub.capturedEventsWithScopes.count)
}

Expand Down Expand Up @@ -742,8 +784,8 @@ class SentryTracerTests: XCTestCase {
fixture.hub.group.wait()

XCTAssertEqual(1, fixture.hub.capturedEventsWithScopes.count)
let serializedTransaction = fixture.hub.capturedEventsWithScopes.first!.event.serialize()
XCTAssertNil(serializedTransaction["measurements"])
let serializedTransaction = fixture.hub.capturedEventsWithScopes.first?.event.serialize()
XCTAssertNil(serializedTransaction?["measurements"])
}

}