Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add span level measurements #1855 #2214

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Add support for span level measurements. ([#2214](https://github.com/getsentry/sentry-dart/pull/2214))
martinhaintz marked this conversation as resolved.
Show resolved Hide resolved

## 8.6.0

### Improvements
Expand Down
7 changes: 6 additions & 1 deletion dart/lib/src/protocol/sentry_span.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ class SentrySpan extends ISentrySpan {
num value, {
SentryMeasurementUnit? unit,
}) {
_tracer.setMeasurement(name, value, unit: unit);
if (finished) {
_hub.options.logger(SentryLevel.debug,
"The span is already finished. Measurement $name cannot be set");
return;
}
_tracer.setMeasurementFromChild(name, value, unit: unit);
}

@override
Expand Down
2 changes: 1 addition & 1 deletion dart/lib/src/sentry_span_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ abstract class ISentrySpan {
/// Returns the trace information that could be sent as a sentry-trace header.
SentryTraceHeader toSentryTrace();

/// Set observed measurement for this transaction.
/// Set observed measurement for this span or transaction.
void setMeasurement(
String name,
num value, {
Expand Down
19 changes: 13 additions & 6 deletions dart/lib/src/sentry_tracer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class SentryTracer extends ISentrySpan {
late final SentrySpan _rootSpan;
final List<SentrySpan> _children = [];
final Map<String, dynamic> _extra = {};

final Map<String, SentryMeasurement> _measurements = {};
Map<String, SentryMeasurement> get measurements => _measurements;

Timer? _autoFinishAfterTimer;
Duration? _autoFinishAfter;
Expand Down Expand Up @@ -320,10 +322,6 @@ class SentryTracer extends ISentrySpan {
@override
SentryTraceHeader toSentryTrace() => _rootSpan.toSentryTrace();

@visibleForTesting
Map<String, SentryMeasurement> get measurements =>
Map.unmodifiable(_measurements);

bool _haveAllChildrenFinished() {
for (final child in children) {
if (!child.finished) {
Expand All @@ -341,10 +339,19 @@ class SentryTracer extends ISentrySpan {
@override
void setMeasurement(String name, num value, {SentryMeasurementUnit? unit}) {
if (finished) {
_hub.options.logger(SentryLevel.debug,
"The tracer is already finished. Measurement $name cannot be set");
return;
}
final measurement = SentryMeasurement(name, value, unit: unit);
_measurements[name] = measurement;
_measurements[name] = SentryMeasurement(name, value, unit: unit);
}

void setMeasurementFromChild(String name, num value,
{SentryMeasurementUnit? unit}) {
// We don't want to overwrite span measurement, if it comes from a child.
buenaflor marked this conversation as resolved.
Show resolved Hide resolved
if (!_measurements.containsKey(name)) {
setMeasurement(name, value, unit: unit);
}
}

@override
Expand Down
15 changes: 15 additions & 0 deletions dart/test/sentry_span_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,21 @@ void main() {
expect(fixture.hub.options.enableSpanLocalMetricAggregation, false);
expect(sut.localMetricsAggregator, null);
});

test('setMeasurement sets a measurement', () async {
final sut = fixture.getSut();
sut.setMeasurement("test", 1);
expect(sut.tracer.measurements.containsKey("test"), true);
expect(sut.tracer.measurements["test"]!.value, 1);
});

test('setMeasurement does not set a measurement if a span is finished',
() async {
final sut = fixture.getSut();
await sut.finish();
sut.setMeasurement("test", 1);
expect(sut.tracer.measurements.isEmpty, true);
});
}

class Fixture {
Expand Down
16 changes: 16 additions & 0 deletions dart/test/sentry_tracer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,22 @@ void main() {
expect(fixture.hub.options.enableSpanLocalMetricAggregation, false);
expect(sut.localMetricsAggregator, null);
});

test('setMeasurement sets a measurement', () async {
final sut = fixture.getSut();
sut.setMeasurement("test", 1);
expect(sut.measurements.containsKey("test"), true);
expect(sut.measurements["test"]!.value, 1);
});

test('setMeasurementFromChild does not override existing measurements',
() async {
final sut = fixture.getSut();
sut.setMeasurement("test", 1);
sut.setMeasurementFromChild("test", 5);
expect(sut.measurements.containsKey("test"), true);
expect(sut.measurements["test"]!.value, 1);
});
});

group('$SentryBaggageHeader', () {
Expand Down
Loading