Skip to content

Commit 9454d5d

Browse files
authored
fix: Propagate span when copying scope (#2952)
Previously, copied scopes (such as those created in -[SentrySDK captureEvent:withScopeBlock:]) did not inherit the original scope's span. This resulted in emitted events not linking back to the relevant span. Other SDKs do copy over the parent span/transaction. This is fixed now.
1 parent 17afc4b commit 9454d5d

File tree

4 files changed

+33
-30
lines changed

4 files changed

+33
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Possible crash in Core Data tracking (#2865)
1818
- Ensure the current GPU frame rate is always reported for concurrent transaction profiling metrics (#2929)
1919
- Move profiler metric collection to a background queue (#2956)
20+
- Propagate span when copying scope (#2952)
2021

2122
## 8.5.0
2223

Sources/Sentry/SentryScope.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ - (instancetype)initWithScope:(SentryScope *)scope
115115
self.distString = scope.distString;
116116
self.environmentString = scope.environmentString;
117117
self.levelEnum = scope.levelEnum;
118+
self.span = scope.span;
118119
}
119120
return self;
120121
}

Tests/SentryTests/SentryScopeSwiftTests.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,37 @@ class SentryScopeSwiftTests: XCTestCase {
118118
XCTAssertNil(actual["transaction"])
119119
XCTAssertNotNil(actual["breadcrumbs"])
120120
}
121+
122+
func testInitWithScope() {
123+
let scope = fixture.scope
124+
scope.span = fixture.transaction
125+
126+
let snapshot = scope.serialize() as! [String: AnyHashable]
127+
128+
let cloned = Scope(scope: scope)
129+
XCTAssertEqual(cloned.serialize() as! [String: AnyHashable], snapshot)
130+
131+
let (event1, event2) = (Event(), Event())
132+
(event1.timestamp, event2.timestamp) = (fixture.date, fixture.date)
133+
event2.eventId = event1.eventId
134+
scope.applyTo(event: event1, maxBreadcrumbs: 10)
135+
cloned.applyTo(event: event2, maxBreadcrumbs: 10)
136+
XCTAssertEqual(
137+
event1.serialize() as! [String: AnyHashable],
138+
event2.serialize() as! [String: AnyHashable]
139+
)
140+
141+
cloned.setExtras(["aa": "b"])
142+
cloned.setTags(["ab": "c"])
143+
cloned.addBreadcrumb(Breadcrumb(level: .debug, category: "http2"))
144+
cloned.setUser(User(userId: "aid"))
145+
cloned.setContext(value: ["ae": "af"], key: "myContext")
146+
cloned.setDist("a456")
147+
cloned.setEnvironment("a789")
148+
149+
XCTAssertEqual(scope.serialize() as! [String: AnyHashable], snapshot)
150+
XCTAssertNotEqual(scope.serialize() as! [String: AnyHashable], cloned.serialize() as! [String: AnyHashable])
151+
}
121152

122153
func testApplyToEvent() {
123154
let actual = fixture.scope.applyTo(event: fixture.event, maxBreadcrumbs: 10)

Tests/SentryTests/SentryScopeTests.m

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -163,34 +163,4 @@ - (void)testClearBreadcrumb
163163
XCTAssertTrue([[[scope serialize] objectForKey:@"breadcrumbs"] count] == 0);
164164
}
165165

166-
- (void)testInitWithScope
167-
{
168-
SentryScope *scope = [[SentryScope alloc] init];
169-
[scope setExtras:@{ @"a" : @"b" }];
170-
[scope setTags:@{ @"b" : @"c" }];
171-
[scope addBreadcrumb:[self getBreadcrumb]];
172-
[scope setUser:[[SentryUser alloc] initWithUserId:@"id"]];
173-
[scope setContextValue:@{ @"e" : @"f" } forKey:@"myContext"];
174-
[scope setDist:@"456"];
175-
[scope setEnvironment:@"789"];
176-
[scope setFingerprint:@[ @"a" ]];
177-
178-
NSMutableDictionary *snapshot = [scope serialize].mutableCopy;
179-
180-
SentryScope *cloned = [[SentryScope alloc] initWithScope:scope];
181-
XCTAssertEqualObjects(snapshot, [cloned serialize]);
182-
183-
[cloned setExtras:@{ @"aa" : @"b" }];
184-
[cloned setTags:@{ @"ab" : @"c" }];
185-
[cloned addBreadcrumb:[[SentryBreadcrumb alloc] initWithLevel:kSentryLevelDebug
186-
category:@"http2"]];
187-
[cloned setUser:[[SentryUser alloc] initWithUserId:@"aid"]];
188-
[cloned setContextValue:@{ @"ae" : @"af" } forKey:@"myContext"];
189-
[cloned setDist:@"a456"];
190-
[cloned setEnvironment:@"a789"];
191-
192-
XCTAssertEqualObjects(snapshot, [scope serialize]);
193-
XCTAssertNotEqualObjects([scope serialize], [cloned serialize]);
194-
}
195-
196166
@end

0 commit comments

Comments
 (0)