Skip to content

Commit 379d7a8

Browse files
authored
fix: Reset transaction in scope if span is reset (#1125)
1 parent a49594a commit 379d7a8

File tree

3 files changed

+57
-40
lines changed

3 files changed

+57
-40
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixes
66

77
- Disable `enableUserInteractionBreadcrumbs` on Android when `enableAutoNativeBreadcrumbs` is disabled ([#1131](https://github.com/getsentry/sentry-dart/pull/1131))
8+
- Transaction name is reset after the transaction finishes ([#1125](https://github.com/getsentry/sentry-dart/pull/1125))
89

910
### Dependencies
1011

dart/lib/src/scope.dart

+11-23
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,22 @@ class Scope {
1919
/// The name of the transaction which generated this event,
2020
/// for example, the route name: `"/users/<username>/"`.
2121
String? get transaction {
22-
return ((_span is SentryTracer) ? (_span as SentryTracer?)?.name : null) ??
22+
return ((span is SentryTracer) ? (span as SentryTracer?)?.name : null) ??
2323
_transaction;
2424
}
2525

2626
set transaction(String? transaction) {
2727
_transaction = transaction;
2828

29-
if (_transaction != null && _span != null) {
29+
if (_transaction != null && span != null) {
3030
final currentTransaction =
31-
(_span is SentryTracer) ? (_span as SentryTracer?) : null;
31+
(span is SentryTracer) ? (span as SentryTracer?) : null;
3232
currentTransaction?.name = _transaction!;
3333
}
3434
}
3535

36-
ISentrySpan? _span;
37-
3836
/// Returns active transaction or null if there is no active transaction.
39-
ISentrySpan? get span => _span;
40-
41-
set span(ISentrySpan? span) {
42-
_span = span;
43-
44-
if (_span != null) {
45-
final currentTransaction =
46-
(_span is SentryTracer) ? (_span as SentryTracer?) : null;
47-
_transaction = currentTransaction?.name ?? _transaction;
48-
}
49-
}
37+
ISentrySpan? span;
5038

5139
SentryUser? _user;
5240

@@ -242,7 +230,7 @@ class Scope {
242230
Future<void> clear() async {
243231
clearAttachments();
244232
level = null;
245-
_span = null;
233+
span = null;
246234
_transaction = null;
247235
_fingerprint = [];
248236
_tags.clear();
@@ -297,7 +285,7 @@ class Scope {
297285
dynamic hint,
298286
}) async {
299287
event = event.copyWith(
300-
transaction: event.transaction ?? _transaction,
288+
transaction: event.transaction ?? transaction,
301289
user: _mergeUsers(user, event.user),
302290
breadcrumbs: (event.breadcrumbs?.isNotEmpty ?? false)
303291
? event.breadcrumbs
@@ -327,10 +315,10 @@ class Scope {
327315
}
328316
});
329317

330-
final span = _span;
331-
if (event.contexts.trace == null && span != null) {
332-
event.contexts.trace = span.context.toTraceContext(
333-
sampled: span.samplingDecision?.sampled,
318+
final newSpan = span;
319+
if (event.contexts.trace == null && newSpan != null) {
320+
event.contexts.trace = newSpan.context.toTraceContext(
321+
sampled: newSpan.samplingDecision?.sampled,
334322
);
335323
}
336324

@@ -420,7 +408,7 @@ class Scope {
420408
..level = level
421409
..fingerprint = List.from(fingerprint)
422410
.._transaction = _transaction
423-
.._span = _span;
411+
..span = span;
424412

425413
clone._setUserSync(user);
426414

dart/test/scope_test.dart

+45-17
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ void main() {
3535
test('sets transaction overwrites span name', () {
3636
final sut = fixture.getSut();
3737

38-
final tracer = SentryTracer(fixture.context, MockHub());
39-
sut.span = tracer;
38+
sut.span = fixture.sentryTracer;
4039
sut.transaction = 'test';
4140

4241
expect(sut.transaction, 'test');
@@ -46,13 +45,31 @@ void main() {
4645
test('sets span overwrites transaction name', () {
4746
final sut = fixture.getSut();
4847

49-
final tracer = SentryTracer(fixture.context, MockHub());
50-
sut.span = tracer;
48+
sut.span = fixture.sentryTracer;
5149

5250
expect(sut.transaction, 'name');
5351
expect((sut.span as SentryTracer).name, 'name');
5452
});
5553

54+
test('removing span resets transaction if not set separately', () {
55+
final sut = fixture.getSut();
56+
57+
sut.span = fixture.sentryTracer;
58+
sut.span = null;
59+
60+
expect(sut.transaction, isNull);
61+
});
62+
63+
test('removing span does not reset transaction if set separately', () {
64+
final sut = fixture.getSut();
65+
66+
sut.transaction = 'test';
67+
sut.span = fixture.sentryTracer;
68+
sut.span = null;
69+
70+
expect(sut.transaction, 'test');
71+
});
72+
5673
test('sets $SentryUser', () {
5774
final sut = fixture.getSut();
5875

@@ -403,9 +420,9 @@ void main() {
403420
});
404421

405422
test('apply trace context to event', () async {
406-
final tracer = SentryTracer(fixture.context, MockHub());
407423
final event = SentryEvent();
408-
final scope = Scope(SentryOptions(dsn: fakeDsn))..span = tracer;
424+
final scope = Scope(SentryOptions(dsn: fakeDsn))
425+
..span = fixture.sentryTracer;
409426

410427
final updatedEvent = await scope.applyToEvent(event);
411428

@@ -537,6 +554,16 @@ void main() {
537554

538555
expect(updatedEvent?.level, SentryLevel.error);
539556
});
557+
558+
test('should apply the scope transaction from the span', () async {
559+
final event = SentryEvent();
560+
final scope = Scope(SentryOptions(dsn: fakeDsn))
561+
..span = fixture.sentryTracer;
562+
563+
final updatedEvent = await scope.applyToEvent(event);
564+
565+
expect(updatedEvent?.transaction, 'name');
566+
});
540567
});
541568

542569
test('event processor drops the event', () async {
@@ -551,8 +578,7 @@ void main() {
551578
});
552579

553580
test('should not apply fingerprint if transaction', () async {
554-
final tracer = SentryTracer(fixture.context, MockHub());
555-
var tr = SentryTransaction(tracer);
581+
var tr = SentryTransaction(fixture.sentryTracer);
556582
final scope = Scope(SentryOptions(dsn: fakeDsn))..fingerprint = ['test'];
557583

558584
final updatedTr = await scope.applyToEvent(tr);
@@ -561,8 +587,7 @@ void main() {
561587
});
562588

563589
test('should not apply level if transaction', () async {
564-
final tracer = SentryTracer(fixture.context, MockHub());
565-
var tr = SentryTransaction(tracer);
590+
var tr = SentryTransaction(fixture.sentryTracer);
566591
final scope = Scope(SentryOptions(dsn: fakeDsn))..level = SentryLevel.error;
567592

568593
final updatedTr = await scope.applyToEvent(tr);
@@ -571,8 +596,7 @@ void main() {
571596
});
572597

573598
test('apply sampled to trace', () async {
574-
final tracer = SentryTracer(fixture.context, MockHub());
575-
var tr = SentryTransaction(tracer);
599+
var tr = SentryTransaction(fixture.sentryTracer);
576600
final scope = Scope(SentryOptions(dsn: fakeDsn))..level = SentryLevel.error;
577601

578602
final updatedTr = await scope.applyToEvent(tr);
@@ -701,15 +725,19 @@ void main() {
701725
}
702726

703727
class Fixture {
704-
final context = SentryTransactionContext(
705-
'name',
706-
'op',
707-
samplingDecision: SentryTracesSamplingDecision(true),
708-
);
709728
final mockScopeObserver = MockScopeObserver();
710729

711730
final options = SentryOptions(dsn: fakeDsn);
712731

732+
final sentryTracer = SentryTracer(
733+
SentryTransactionContext(
734+
'name',
735+
'op',
736+
samplingDecision: SentryTracesSamplingDecision(true),
737+
),
738+
MockHub(),
739+
);
740+
713741
SentryLevel? loggedLevel;
714742
Object? loggedException;
715743

0 commit comments

Comments
 (0)