Skip to content

Commit 79f6b41

Browse files
Fix trace ID mismatch in Sentry (#3069)
* Add PropagationContext constructors and copy method for trace continuity Co-authored-by: giancarlo.buenaflor <[email protected]> * Remove PropagationContext constructors and share context in Scope clone Co-authored-by: giancarlo.buenaflor <[email protected]> * Fix trace ID consistency in scope propagation and cloning Co-authored-by: giancarlo.buenaflor <[email protected]> * Update * Update CHANGELOG * Update test * Update test * Update test * Update test --------- Co-authored-by: Cursor Agent <[email protected]>
1 parent cc4e375 commit 79f6b41

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Span ids not re-generating for headers created from scope ([#3051](https://github.com/getsentry/sentry-dart/pull/3051))
88
- `ScreenshotIntegration` not being added for web ([#3055](https://github.com/getsentry/sentry-dart/pull/3055))
9+
- `PropagationContext` not being set when `Scope` is cloned resulting in different trace ids when using `withScope` ([#3069](https://github.com/getsentry/sentry-dart/pull/3069))
910

1011
### Enhancements
1112

dart/lib/src/scope.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ class Scope {
426426
.._transaction = _transaction
427427
..span = span
428428
.._enableScopeSync = false
429+
..propagationContext = propagationContext
429430
.._replayId = _replayId;
430431

431432
clone._setUserSync(user);

dart/test/hub_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,26 @@ void main() {
779779
expect(calls[2].scope?.user, isNull);
780780
expect(calls[2].formatted, 'foo bar 2');
781781
});
782+
783+
test(
784+
'withScope should use the same propagation context as the current scope',
785+
() async {
786+
final hub = fixture.getSut();
787+
late Scope clonedScope;
788+
final currentScope = hub.scope;
789+
await hub.captureEvent(SentryEvent(), withScope: (scope) async {
790+
clonedScope = scope;
791+
});
792+
793+
// Verify the propagation context is shared (same instance)
794+
expect(
795+
identical(
796+
clonedScope.propagationContext, currentScope.propagationContext),
797+
true,
798+
reason: 'Propagation context should be the same instance');
799+
expect(clonedScope.propagationContext.traceId,
800+
currentScope.propagationContext.traceId);
801+
});
782802
});
783803

784804
group('ClientReportRecorder', () {

dart/test/scope_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,18 @@ void main() {
411411
expect(0, fixture.mockScopeObserver.numberOfSetContextsCalls);
412412
});
413413

414+
test('clone shares propagation context to maintain trace continuity', () {
415+
final sut = fixture.getSut();
416+
417+
// Clone the scope
418+
final clone = sut.clone();
419+
420+
// Verify the propagation context is shared (same instance)
421+
expect(identical(sut.propagationContext, clone.propagationContext), true,
422+
reason: 'Propagation context should be the same instance');
423+
expect(clone.propagationContext.traceId, sut.propagationContext.traceId);
424+
});
425+
414426
group('Scope apply', () {
415427
final scopeUser = SentryUser(
416428
id: '800',

0 commit comments

Comments
 (0)