From a3538999ec5ccb7acf2ac380070c0646bdb347cd Mon Sep 17 00:00:00 2001 From: Philipp Hofmann Date: Thu, 3 Oct 2024 13:46:57 +0200 Subject: [PATCH] fix: Linking ongoing trace to crash event (#4393) Fix linking a crash event to an ongoing trace by skipping setting the trace context in Scope.applyToEvent for crash events. Fixes GH-4375 --- CHANGELOG.md | 1 + Sources/Sentry/SentryScope.m | 9 ++++++++- Tests/SentryTests/SentryScopeSwiftTests.swift | 11 +++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74c4f42213b..0ed7cd68b49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ ### Fixes - Fix the versioning to support app release with Beta versions (#4368) +- Linking ongoing trace to crash event (#4393) ## 8.37.0-beta.1 diff --git a/Sources/Sentry/SentryScope.m b/Sources/Sentry/SentryScope.m index dee7df38cec..21fb2e873bf 100644 --- a/Sources/Sentry/SentryScope.m +++ b/Sources/Sentry/SentryScope.m @@ -2,7 +2,7 @@ #import "SentryAttachment+Private.h" #import "SentryBreadcrumb.h" #import "SentryEnvelopeItemType.h" -#import "SentryEvent.h" +#import "SentryEvent+Private.h" #import "SentryGlobalEventProcessor.h" #import "SentryLevelMapper.h" #import "SentryLog.h" @@ -564,6 +564,13 @@ - (SentryEvent *__nullable)applyToEvent:(SentryEvent *)event [SentryDictionary mergeEntriesFromDictionary:event.context intoDictionary:newContext]; } + // Don't add the trace context of a current trace to a crash event because crash events are from + // a previous run. + if (event.isCrashEvent) { + event.context = newContext; + return event; + } + if (self.span != nil) { id span; @synchronized(_spanLock) { diff --git a/Tests/SentryTests/SentryScopeSwiftTests.swift b/Tests/SentryTests/SentryScopeSwiftTests.swift index 9031bd6eb3f..f6ec28907be 100644 --- a/Tests/SentryTests/SentryScopeSwiftTests.swift +++ b/Tests/SentryTests/SentryScopeSwiftTests.swift @@ -225,6 +225,17 @@ class SentryScopeSwiftTests: XCTestCase { XCTAssertEqual(trace?["span_id"] as? String, fixture.transaction.spanId.sentrySpanIdString) } + func testApplyToEvent_ScopeWithSpan_NotAppliedToCrashEvent() { + let scope = fixture.scope + scope.span = fixture.transaction + let event = fixture.event + event.isCrashEvent = true + + let actual = scope.applyTo(event: event, maxBreadcrumbs: 10) + XCTAssertNil(fixture.event.context?["trace"]) + XCTAssertNil(actual?.transaction) + } + func testApplyToEvent_EventWithDist() { let event = fixture.event event.dist = "myDist"