Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: Add abnormal mechanism to SentrySession #4842

Merged
merged 4 commits into from
Feb 19, 2025
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
10 changes: 10 additions & 0 deletions Sources/Sentry/SentrySession.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ - (nullable instancetype)initWithJSONObject:(NSDictionary *)jsonObject
if ([duration isKindOfClass:[NSNumber class]]) {
_duration = duration;
}

id abnormalMechanism = [jsonObject valueForKey:@"abnormal_mechanism"];
if ([abnormalMechanism isKindOfClass:[NSString class]]) {
_abnormalMechanism = abnormalMechanism;
}
}

return self;
Expand Down Expand Up @@ -238,6 +243,10 @@ - (void)incrementErrors

[serializedData setValue:_distinctId forKey:@"did"];

if (_abnormalMechanism != nil) {
serializedData[@"abnormal_mechanism"] = _abnormalMechanism;
}

return serializedData;
}
}
Expand All @@ -258,6 +267,7 @@ - (id)copyWithZone:(nullable NSZone *)zone
copy->_releaseName = _releaseName;
copy.environment = self.environment;
copy.user = self.user;
copy->_abnormalMechanism = _abnormalMechanism;
copy->_init = _init;
}

Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/include/SentrySession.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ SENTRY_NO_INIT
@property (nonatomic, copy) NSString *_Nullable environment;
@property (nonatomic, copy) SentryUser *_Nullable user;

/**
* The reason for session to become abnormal, for example an app hang.
*/
@property (nonatomic, copy) NSString *_Nullable abnormalMechanism;

- (NSDictionary<NSString *, id> *)serialize;

@end
Expand Down
51 changes: 51 additions & 0 deletions Tests/SentryTests/SentrySessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ class SentrySessionTestsSwift: XCTestCase {

let session = SentrySession(releaseName: "1.0.0", distinctId: "some-id")
session.user = user
session.abnormalMechanism = "app hang"
let copiedSession = try XCTUnwrap(session.copy() as? SentrySession)

XCTAssertEqual(session, copiedSession)
XCTAssertEqual(session.abnormalMechanism, copiedSession.abnormalMechanism)

// The user is copied as well
session.user?.email = "[email protected]"
Expand Down Expand Up @@ -116,7 +118,56 @@ class SentrySessionTestsSwift: XCTestCase {

XCTAssertTrue(result["init"] as? Bool ?? false)
XCTAssertNotEqual(2, result["init"] as? NSNumber ?? 2)
}

func testSerializeAbormalMechanism() {
// Arrange
let session = SentrySession(releaseName: "1.0.0", distinctId: "distinctId")
session.abnormalMechanism = "app hang"

// Act
let jsonDict = session.serialize()

// Assert
XCTAssertEqual(session.abnormalMechanism, jsonDict["abnormal_mechanism"] as? String)
}

func testSerializeAbormalMechanism_IfNil_NotAddedToDict() {
// Arrange
let session = SentrySession(releaseName: "1.0.0", distinctId: "distinctId")

// Act
let jsonDict = session.serialize()

// Assert
XCTAssertNil(jsonDict["abnormal_mechanism"])
}

func testInitWithJson_AbnormalMechanism_SetsAbnormalMechanism() throws {
// Arrange
let session = SentrySession(releaseName: "1.0.0", distinctId: "distinctId")
session.abnormalMechanism = "app hang"
let jsonDict = session.serialize()

// Act
let actual = try XCTUnwrap(SentrySession(jsonObject: jsonDict))

// Assert
XCTAssertEqual(session.abnormalMechanism, actual.abnormalMechanism)
}

func testInitWithJson_AbnormalMechanismIsInt_DoesNotSetAbnormalMechanism() throws {
// Arrange
let session = SentrySession(releaseName: "1.0.0", distinctId: "distinctId")
session.abnormalMechanism = "app hang"
var jsonDict = session.serialize()
jsonDict["abnormal_mechanism"] = 1

// Act
let actual = try XCTUnwrap(SentrySession(jsonObject: jsonDict))

// Assert
XCTAssertNil(actual.abnormalMechanism)
}
}

Expand Down
Loading