Skip to content

Commit

Permalink
fix scope
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphofmann committed Feb 25, 2025
1 parent 81d1578 commit 0f66013
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Sources/Sentry/SentryANRTrackingIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "SentryLog.h"
#import "SentryMechanism.h"
#import "SentrySDK+Private.h"
#import "SentryScope+Private.h"
#import "SentryStacktrace.h"
#import "SentrySwift.h"
#import "SentryThread.h"
Expand Down Expand Up @@ -153,6 +154,16 @@ - (void)anrDetectedWithType:(enum SentryANRType)type
// when either sending a normal or fatal app hang event. Otherwise, we would have to rely on
// string parsing to retrieve the app hang duration info from the error message.
mechanism.data = @{ SentryANRMechanismDataAppHangDuration : appHangDurationInfo };

// We need to apply the scope now because if the app hang turns into a fatal one,
// we would lose the scope. Furthermore, we want to know in which state the app was when the
// app hang started.
SentryScope *scope = [SentrySDK currentHub].scope;
SentryOptions *options = SentrySDK.options;
if (scope != nil && options != nil) {
[scope applyToEvent:event maxBreadcrumb:options.maxBreadcrumbs];
}

[self.fileManager storeAppHangEvent:event];
} else {
#endif // SENTRY_HAS_UIKIT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,18 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
// Arrange
givenInitializedTracker(enableV2: true)
setUpThreadInspector()
SentrySDK.configureScope { scope in
scope.setTag(value: "value", key: "key")
}
let crumb = Breadcrumb()
crumb.message = "crumb"
SentrySDK.addBreadcrumb(crumb)
Dynamic(sut).anrDetectedWithType(SentryANRType.fullyBlocking)

// This must not impact on the stored event
SentrySDK.configureScope { scope in
scope.setTag(value: "value2", key: "key")
}

// Act
let result = SentryANRStoppedResult(minDuration: 1.851, maxDuration: 2.249)
Expand Down Expand Up @@ -386,15 +397,34 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
}.count

XCTAssertTrue(threadsWithFrames > 1, "Not enough threads with frames")

let tags = try XCTUnwrap(event?.tags)
XCTAssertEqual(1, tags.count)
XCTAssertEqual("value", tags["key"])

let breadcrumbs = try XCTUnwrap(event?.breadcrumbs)
XCTAssertEqual(1, breadcrumbs.count)
XCTAssertEqual("crumb", breadcrumbs.first?.message)
}
}

func testV2_ANRDetected_StopNotCalled_SendsFatalANROnNextInstall() throws {
// Arrange
givenInitializedTracker(enableV2: true)
setUpThreadInspector()
SentrySDK.configureScope { scope in
scope.setTag(value: "value", key: "key")
}
let crumb = Breadcrumb()
crumb.message = "crumb"
SentrySDK.addBreadcrumb(crumb)
Dynamic(sut).anrDetectedWithType(SentryANRType.nonFullyBlocking)

// This must not impact on the stored event
SentrySDK.configureScope { scope in
scope.setTag(value: "value2", key: "key")
}

// Act
givenInitializedTracker(enableV2: true)

Expand All @@ -411,6 +441,14 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
// This asserts that we remove the mechanism data before sending the event.
let mechanismData = try XCTUnwrap(ex.mechanism?.data)
XCTAssertTrue(mechanismData.isEmpty)

let tags = try XCTUnwrap(event?.tags)
XCTAssertEqual(1, tags.count)
XCTAssertEqual("value", tags["key"])

let breadcrumbs = try XCTUnwrap(event?.breadcrumbs)
XCTAssertEqual(1, breadcrumbs.count)
XCTAssertEqual("crumb", breadcrumbs.first?.message)
}
}

Expand Down

0 comments on commit 0f66013

Please sign in to comment.