Skip to content

Commit

Permalink
Do not allow setting measurement if transaction is finished (#1026)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Sep 29, 2022
1 parent a62d9aa commit 4c2c7c7
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 27 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Tracer does not allow setting measurement if finished ([#1026](https://github.com/getsentry/sentry-dart/pull/1026))

## 6.11.1

### Fixes
Expand Down
4 changes: 2 additions & 2 deletions dart/lib/src/protocol/sentry_span.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SentrySpan extends ISentrySpan {

SpanStatus? _status;
final Map<String, String> _tags = {};
void Function({DateTime? endTimestamp})? _finishedCallback;
Function({DateTime? endTimestamp})? _finishedCallback;

@override
final SentryTracesSamplingDecision? samplingDecision;
Expand Down Expand Up @@ -62,7 +62,7 @@ class SentrySpan extends ISentrySpan {
if (_throwable != null) {
_hub.setSpanContext(_throwable, this, _tracer.name);
}
_finishedCallback?.call(endTimestamp: _endTimestamp);
await _finishedCallback?.call(endTimestamp: _endTimestamp);
return super.finish(status: status, endTimestamp: _endTimestamp);
}

Expand Down
25 changes: 16 additions & 9 deletions dart/lib/src/sentry_tracer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ class SentryTracer extends ISentrySpan {
}
}

await _rootSpan.finish(endTimestamp: _rootEndTimestamp);
// the callback should run before because if the span is finished,
// we cannot attach data, its immutable after being finished.
await _onFinish?.call(this);
await _rootSpan.finish(endTimestamp: _rootEndTimestamp);

// remove from scope
await _hub.configureScope((scope) {
Expand Down Expand Up @@ -216,21 +218,23 @@ class SentryTracer extends ISentrySpan {
_hub,
samplingDecision: _rootSpan.samplingDecision,
startTimestamp: startTimestamp,
finishedCallback: ({
DateTime? endTimestamp,
}) {
final finishStatus = _finishStatus;
if (finishStatus.finishing) {
finish(status: finishStatus.status, endTimestamp: endTimestamp);
}
},
finishedCallback: _finishedCallback,
);

_children.add(child);

return child;
}

Future<void> _finishedCallback({
DateTime? endTimestamp,
}) async {
final finishStatus = _finishStatus;
if (finishStatus.finishing) {
await finish(status: finishStatus.status, endTimestamp: endTimestamp);
}
}

@override
SpanStatus? get status => _rootSpan.status;

Expand Down Expand Up @@ -284,6 +288,9 @@ class SentryTracer extends ISentrySpan {

@override
void setMeasurement(String name, num value, {SentryMeasurementUnit? unit}) {
if (finished) {
return;
}
final measurement = SentryMeasurement(name, value, unit: unit);
_measurements[name] = measurement;
}
Expand Down
31 changes: 31 additions & 0 deletions dart/test/sentry_tracer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,37 @@ void main() {

expect(sut.startChild('child3'), isA<NoOpSentrySpan>());
});

test('tracer sets measurement', () async {
final sut = fixture.getSut();

sut.setMeasurement('key', 1.0);

expect(sut.measurements['key']!.value, 1.0);

await sut.finish();
});

test('tracer sets custom measurement unit', () async {
final sut = fixture.getSut();

sut.setMeasurement('key', 1.0, unit: SentryMeasurementUnit.hour);

expect(sut.measurements['key']!.value, 1.0);
expect(sut.measurements['key']?.unit, SentryMeasurementUnit.hour);

await sut.finish();
});

test('tracer does not allow setting measurement if finished', () async {
final sut = fixture.getSut();

await sut.finish();

sut.setMeasurement('key', 1.0);

expect(sut.measurements.isEmpty, true);
});
});

group('$SentryBaggageHeader', () {
Expand Down
27 changes: 11 additions & 16 deletions flutter/lib/src/navigation/sentry_navigator_observer.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// ignore: implementation_imports
import 'package:sentry/src/sentry_tracer.dart';
import 'package:flutter/widgets.dart';

import '../../sentry_flutter.dart';
Expand Down Expand Up @@ -181,20 +179,17 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
autoFinishAfter: _autoFinishAfter,
trimEnd: true,
onFinish: (transaction) async {
// ignore: invalid_use_of_internal_member
if (transaction is SentryTracer) {
final nativeFrames = await _native
.endNativeFramesCollection(transaction.context.traceId);
if (nativeFrames != null) {
final measurements = nativeFrames.toMeasurements();
for (final item in measurements.entries) {
final measurement = item.value;
transaction.setMeasurement(
item.key,
measurement.value,
unit: measurement.unit,
);
}
final nativeFrames = await _native
.endNativeFramesCollection(transaction.context.traceId);
if (nativeFrames != null) {
final measurements = nativeFrames.toMeasurements();
for (final item in measurements.entries) {
final measurement = item.value;
transaction.setMeasurement(
item.key,
measurement.value,
unit: measurement.unit,
);
}
}
},
Expand Down

0 comments on commit 4c2c7c7

Please sign in to comment.