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

fix: Propagate span when copying scope #2952

Merged
merged 5 commits into from
May 2, 2023
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 @@ -17,6 +17,7 @@
- Possible crash in Core Data tracking (#2865)
- Ensure the current GPU frame rate is always reported for concurrent transaction profiling metrics (#2929)
- Move profiler metric collection to a background queue (#2956)
- Propagate span when copying scope (#2952)

## 8.5.0

Expand Down
1 change: 1 addition & 0 deletions Sources/Sentry/SentryScope.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ - (instancetype)initWithScope:(SentryScope *)scope
self.distString = scope.distString;
self.environmentString = scope.environmentString;
self.levelEnum = scope.levelEnum;
self.span = scope.span;
}
return self;
}
Expand Down
31 changes: 31 additions & 0 deletions Tests/SentryTests/SentryScopeSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,37 @@ class SentryScopeSwiftTests: XCTestCase {
XCTAssertNil(actual["transaction"])
XCTAssertNotNil(actual["breadcrumbs"])
}

func testInitWithScope() {
Copy link
Contributor Author

@kabiroberai kabiroberai Apr 24, 2023

Choose a reason for hiding this comment

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

this test is mostly a direct translation of the ObjC version, but with the addition of a check that calling cloned.applyTo has the same effect as calling it on the original scope. The new check would have failed without the change to the copy constructor.

Copy link
Member

Choose a reason for hiding this comment

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

Please add some validation for assigning self.span = scope.span in the initWithScope. So basically if I remove self.span = scope.span from initWithScope this test should fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, this was what the Event comparison was supposed to validate, but I forgot to assign a span to the scope. Removing that line goes from green -> red as expected now.

let scope = fixture.scope
scope.span = fixture.transaction

let snapshot = scope.serialize() as! [String: AnyHashable]

let cloned = Scope(scope: scope)
XCTAssertEqual(cloned.serialize() as! [String: AnyHashable], snapshot)

let (event1, event2) = (Event(), Event())
(event1.timestamp, event2.timestamp) = (fixture.date, fixture.date)
event2.eventId = event1.eventId
scope.applyTo(event: event1, maxBreadcrumbs: 10)
cloned.applyTo(event: event2, maxBreadcrumbs: 10)
XCTAssertEqual(
event1.serialize() as! [String: AnyHashable],
event2.serialize() as! [String: AnyHashable]
)

cloned.setExtras(["aa": "b"])
cloned.setTags(["ab": "c"])
cloned.addBreadcrumb(Breadcrumb(level: .debug, category: "http2"))
cloned.setUser(User(userId: "aid"))
cloned.setContext(value: ["ae": "af"], key: "myContext")
cloned.setDist("a456")
cloned.setEnvironment("a789")

XCTAssertEqual(scope.serialize() as! [String: AnyHashable], snapshot)
XCTAssertNotEqual(scope.serialize() as! [String: AnyHashable], cloned.serialize() as! [String: AnyHashable])
}

func testApplyToEvent() {
let actual = fixture.scope.applyTo(event: fixture.event, maxBreadcrumbs: 10)
Expand Down
30 changes: 0 additions & 30 deletions Tests/SentryTests/SentryScopeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -163,34 +163,4 @@ - (void)testClearBreadcrumb
XCTAssertTrue([[[scope serialize] objectForKey:@"breadcrumbs"] count] == 0);
}

- (void)testInitWithScope
{
SentryScope *scope = [[SentryScope alloc] init];
[scope setExtras:@{ @"a" : @"b" }];
[scope setTags:@{ @"b" : @"c" }];
[scope addBreadcrumb:[self getBreadcrumb]];
[scope setUser:[[SentryUser alloc] initWithUserId:@"id"]];
[scope setContextValue:@{ @"e" : @"f" } forKey:@"myContext"];
[scope setDist:@"456"];
[scope setEnvironment:@"789"];
[scope setFingerprint:@[ @"a" ]];

NSMutableDictionary *snapshot = [scope serialize].mutableCopy;

SentryScope *cloned = [[SentryScope alloc] initWithScope:scope];
XCTAssertEqualObjects(snapshot, [cloned serialize]);

[cloned setExtras:@{ @"aa" : @"b" }];
[cloned setTags:@{ @"ab" : @"c" }];
[cloned addBreadcrumb:[[SentryBreadcrumb alloc] initWithLevel:kSentryLevelDebug
category:@"http2"]];
[cloned setUser:[[SentryUser alloc] initWithUserId:@"aid"]];
[cloned setContextValue:@{ @"ae" : @"af" } forKey:@"myContext"];
[cloned setDist:@"a456"];
[cloned setEnvironment:@"a789"];

XCTAssertEqualObjects(snapshot, [scope serialize]);
XCTAssertNotEqualObjects([scope serialize], [cloned serialize]);
}

@end