From 8e6d68b8230d51e72fff482f4c11a72705f9c0c5 Mon Sep 17 00:00:00 2001 From: Philipp Hofmann Date: Tue, 5 Nov 2024 11:49:06 +0100 Subject: [PATCH] fix tests --- Sources/Sentry/SentryScope.m | 31 ++++++++++++++----- Tests/SentryTests/SentryScopeSwiftTests.swift | 11 +++++-- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Sources/Sentry/SentryScope.m b/Sources/Sentry/SentryScope.m index fd75c4ee89..0893881424 100644 --- a/Sources/Sentry/SentryScope.m +++ b/Sources/Sentry/SentryScope.m @@ -97,7 +97,7 @@ - (instancetype)initWithScope:(SentryScope *)scope [_fingerprintArray addObjectsFromArray:[scope fingerprints]]; [_attachmentArray addObjectsFromArray:[scope attachments]]; - self.propagationContext = [[SentryPropagationContext alloc] init]; + self.propagationContext = scope.propagationContext; self.maxBreadcrumbs = scope.maxBreadcrumbs; self.userObject = scope.userObject.copy; self.distString = scope.distString; @@ -145,11 +145,7 @@ - (void)setSpan:(nullable id)span _span = span; for (id observer in self.observers) { - if (span != nil) { - [observer setTraceContext:[span serialize]]; - } else { - [observer setTraceContext:[self.propagationContext traceContextForEvent]]; - } + [observer setTraceContext:[self buildTraceContext:span]]; } } } @@ -461,9 +457,16 @@ - (void)clearAttachments if (self.extras.count > 0) { [serializedData setValue:[self extras] forKey:@"extra"]; } - if (self.context.count > 0) { - [serializedData setValue:[self context] forKey:@"context"]; + + NSDictionary *traceContext = nil; + @synchronized(_spanLock) { + traceContext = [self buildTraceContext:_span]; } + + NSMutableDictionary *newContext = [self context].mutableCopy; + newContext[@"trace"] = traceContext; + [serializedData setValue:newContext forKey:@"context"]; + [serializedData setValue:[self.userObject serialize] forKey:@"user"]; [serializedData setValue:self.distString forKey:@"dist"]; [serializedData setValue:self.environmentString forKey:@"environment"]; @@ -609,6 +612,18 @@ - (void)addObserver:(id)observer [self.observers addObject:observer]; } +/** + * Make sure to call this inside @c synchronized(_spanLock) caus this method isn't thread safe. + */ +- (NSDictionary *)buildTraceContext:(id)span +{ + if (span != nil) { + return [span serialize]; + } else { + return [self.propagationContext traceContextForEvent]; + } +} + @end NS_ASSUME_NONNULL_END diff --git a/Tests/SentryTests/SentryScopeSwiftTests.swift b/Tests/SentryTests/SentryScopeSwiftTests.swift index 9667c6ca98..e06f90beb2 100644 --- a/Tests/SentryTests/SentryScopeSwiftTests.swift +++ b/Tests/SentryTests/SentryScopeSwiftTests.swift @@ -70,6 +70,12 @@ class SentryScopeSwiftTests: XCTestCase { transaction = SentryTracer(transactionContext: TransactionContext(name: transactionName, operation: transactionOperation), hub: nil) } + var contextWithTraceContext: [String: [String: String]] { + var expectedContext = context + expectedContext["trace"] = scope.propagationContext.traceForEvent() + return expectedContext + } + var observer: TestScopeObserver { return TestScopeObserver() } @@ -87,7 +93,7 @@ class SentryScopeSwiftTests: XCTestCase { fixture = Fixture() } - func testSerialize() { + func testSerialize() throws { let scope = fixture.scope let actual = scope.serialize() @@ -106,7 +112,8 @@ class SentryScopeSwiftTests: XCTestCase { XCTAssertEqual(["key": "value"], actual["tags"] as? [String: String]) XCTAssertEqual(["key": "value"], actual["extra"] as? [String: String]) - XCTAssertEqual(fixture.context, actual["context"] as? [String: [String: String]]) + + XCTAssertEqual(fixture.contextWithTraceContext, actual["context"] as? [String: [String: String]]) let actualUser = actual["user"] as? [String: Any] XCTAssertEqual(fixture.ipAddress, actualUser?["ip_address"] as? String)