Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Allow to set startTimestamp explicitly for SentrySpan
  • Loading branch information
fatih ergin committed Mar 8, 2022
commit 3206bdc8f86bdec405caa59cf6cf430e111de658
4 changes: 4 additions & 0 deletions dart/lib/src/hub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ class Hub {
String name,
String operation, {
String? description,
DateTime? startTimestamp,
bool? bindToScope,
bool? waitForChildren,
Duration? autoFinishAfter,
Expand All @@ -354,6 +355,7 @@ class Hub {
operation,
description: description,
),
startTimestamp: startTimestamp,
bindToScope: bindToScope,
waitForChildren: waitForChildren,
autoFinishAfter: autoFinishAfter,
Expand All @@ -365,6 +367,7 @@ class Hub {
ISentrySpan startTransactionWithContext(
SentryTransactionContext transactionContext, {
Map<String, dynamic>? customSamplingContext,
DateTime? startTimestamp,
bool? bindToScope,
bool? waitForChildren,
Duration? autoFinishAfter,
Expand Down Expand Up @@ -395,6 +398,7 @@ class Hub {
final tracer = SentryTracer(
transactionContext,
this,
startTimestamp: startTimestamp,
waitForChildren: waitForChildren ?? false,
autoFinishAfter: autoFinishAfter,
trimEnd: trimEnd ?? false,
Expand Down
4 changes: 4 additions & 0 deletions dart/lib/src/hub_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class HubAdapter implements Hub {
ISentrySpan startTransactionWithContext(
SentryTransactionContext transactionContext, {
Map<String, dynamic>? customSamplingContext,
DateTime? startTimestamp,
bool? bindToScope,
bool? waitForChildren,
Duration? autoFinishAfter,
Expand All @@ -116,6 +117,7 @@ class HubAdapter implements Hub {
Sentry.startTransactionWithContext(
transactionContext,
customSamplingContext: customSamplingContext,
startTimestamp: startTimestamp,
bindToScope: bindToScope,
waitForChildren: waitForChildren,
autoFinishAfter: autoFinishAfter,
Expand All @@ -127,6 +129,7 @@ class HubAdapter implements Hub {
String name,
String operation, {
String? description,
DateTime? startTimestamp,
bool? bindToScope,
bool? waitForChildren,
Duration? autoFinishAfter,
Expand All @@ -137,6 +140,7 @@ class HubAdapter implements Hub {
name,
operation,
description: description,
startTimestamp: startTimestamp,
bindToScope: bindToScope,
waitForChildren: waitForChildren,
autoFinishAfter: autoFinishAfter,
Expand Down
2 changes: 2 additions & 0 deletions dart/lib/src/noop_hub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class NoOpHub implements Hub {
String name,
String operation, {
String? description,
DateTime? startTimestamp,
bool? bindToScope,
bool? waitForChildren,
Duration? autoFinishAfter,
Expand All @@ -99,6 +100,7 @@ class NoOpHub implements Hub {
ISentrySpan startTransactionWithContext(
SentryTransactionContext transactionContext, {
Map<String, dynamic>? customSamplingContext,
DateTime? startTimestamp,
bool? bindToScope,
bool? waitForChildren,
Duration? autoFinishAfter,
Expand Down
6 changes: 5 additions & 1 deletion dart/lib/src/noop_sentry_span.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ class NoOpSentrySpan extends ISentrySpan {
void setTag(String key, String value) {}

@override
ISentrySpan startChild(String operation, {String? description}) =>
ISentrySpan startChild(
String operation, {
String? description,
DateTime? startTimestamp,
}) =>
NoOpSentrySpan();

@override
Expand Down
14 changes: 13 additions & 1 deletion dart/lib/src/protocol/sentry_span.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import '../utils.dart';
class SentrySpan extends ISentrySpan {
final SentrySpanContext _context;
DateTime? _timestamp;
final DateTime _startTimestamp = getUtcDateTime();
late final DateTime _startTimestamp;
final Hub _hub;

final SentryTracer _tracer;
Expand All @@ -28,9 +28,11 @@ class SentrySpan extends ISentrySpan {
this._tracer,
this._context,
this._hub, {
DateTime? startTimestamp,
bool? sampled,
Function()? finishedCallback,
}) {
_startTimestamp = startTimestamp?.toUtc() ?? getUtcDateTime();
this.sampled = sampled;
_finishedCallback = finishedCallback;
}
Expand Down Expand Up @@ -94,15 +96,25 @@ class SentrySpan extends ISentrySpan {
ISentrySpan startChild(
String operation, {
String? description,
DateTime? startTimestamp,
}) {
if (finished) {
return NoOpSentrySpan();
}

if (startTimestamp?.isBefore(_startTimestamp) ?? false) {
_hub.options.logger(
SentryLevel.warning,
"Start timestamp ($startTimestamp) cannot be before parent span's start timestamp ($_startTimestamp). Returning NoOpSpan.",
);
return NoOpSentrySpan();
}

return _tracer.startChildWithParentSpanId(
_context.spanId,
operation,
description: description,
startTimestamp: startTimestamp,
);
}

Expand Down
4 changes: 4 additions & 0 deletions dart/lib/src/sentry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ class Sentry {
String name,
String operation, {
String? description,
DateTime? startTimestamp,
bool? bindToScope,
bool? waitForChildren,
Duration? autoFinishAfter,
Expand All @@ -234,6 +235,7 @@ class Sentry {
name,
operation,
description: description,
startTimestamp: startTimestamp,
bindToScope: bindToScope,
waitForChildren: waitForChildren,
autoFinishAfter: autoFinishAfter,
Expand All @@ -245,6 +247,7 @@ class Sentry {
static ISentrySpan startTransactionWithContext(
SentryTransactionContext transactionContext, {
Map<String, dynamic>? customSamplingContext,
DateTime? startTimestamp,
bool? bindToScope,
bool? waitForChildren,
Duration? autoFinishAfter,
Expand All @@ -253,6 +256,7 @@ class Sentry {
_hub.startTransactionWithContext(
transactionContext,
customSamplingContext: customSamplingContext,
startTimestamp: startTimestamp,
bindToScope: bindToScope,
waitForChildren: waitForChildren,
autoFinishAfter: autoFinishAfter,
Expand Down
1 change: 1 addition & 0 deletions dart/lib/src/sentry_span_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ abstract class ISentrySpan {
ISentrySpan startChild(
String operation, {
String? description,
DateTime? startTimestamp,
});

/// Sets the tag on span or transaction.
Expand Down
12 changes: 9 additions & 3 deletions dart/lib/src/sentry_tracer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ class SentryTracer extends ISentrySpan {
/// transaction after a given "idle time" and we don't want this "idle time"
/// to be part of the transaction.
SentryTracer(SentryTransactionContext transactionContext, this._hub,
{bool waitForChildren = false,
{DateTime? startTimestamp,
bool waitForChildren = false,
Duration? autoFinishAfter,
bool trimEnd = false}) {
_rootSpan = SentrySpan(
this,
transactionContext,
_hub,
sampled: transactionContext.sampled,
startTimestamp: startTimestamp,
);
_waitForChildren = waitForChildren;
if (autoFinishAfter != null) {
Expand Down Expand Up @@ -136,6 +138,7 @@ class SentryTracer extends ISentrySpan {
ISentrySpan startChild(
String operation, {
String? description,
DateTime? startTimestamp,
}) {
if (finished) {
return NoOpSentrySpan();
Expand All @@ -152,13 +155,15 @@ class SentryTracer extends ISentrySpan {
return _rootSpan.startChild(
operation,
description: description,
startTimestamp: startTimestamp,
);
}

ISentrySpan startChildWithParentSpanId(
SpanId parentSpanId,
String operation, {
String? description,
DateTime? startTimestamp,
}) {
if (finished) {
return NoOpSentrySpan();
Expand All @@ -178,8 +183,9 @@ class SentryTracer extends ISentrySpan {
operation: operation,
description: description);

final child = SentrySpan(this, context, _hub, sampled: _rootSpan.sampled,
finishedCallback: () {
final child = SentrySpan(this, context, _hub,
sampled: _rootSpan.sampled,
startTimestamp: startTimestamp, finishedCallback: () {
final finishStatus = _finishStatus;
if (finishStatus.finishing) {
finish(status: finishStatus.status);
Expand Down
1 change: 1 addition & 0 deletions dart/test/default_integrations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ class PrintRecursionMockHub extends MockHub {
ISentrySpan startTransactionWithContext(
SentryTransactionContext transactionContext, {
Map<String, dynamic>? customSamplingContext,
DateTime? startTimestamp,
bool? bindToScope,
bool? waitForChildren,
Duration? autoFinishAfter,
Expand Down
6 changes: 5 additions & 1 deletion dart/test/hub_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,21 @@ void main() {
fixture = Fixture();
});

test('start transaction with given name, op and desc', () async {
test('start transaction with given name, op, desc and start time',
() async {
final hub = fixture.getSut();
final startTime = DateTime.now();

final tr = hub.startTransaction(
'name',
'op',
startTimestamp: startTime,
description: 'desc',
);

expect(tr.context.operation, 'op');
expect(tr.context.description, 'desc');
expect(tr.startTimestamp.isAtSameMomentAs(startTime), true);
expect((tr as SentryTracer).name, 'name');
});

Expand Down
27 changes: 26 additions & 1 deletion dart/test/sentry_span_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import 'mocks/mock_hub.dart';
void main() {
final fixture = Fixture();

test('convert given startTimestamp to utc date time', () async {
final nonUtcStartTimestamp = DateTime.now().toLocal();

final sut = fixture.getSut(startTimestamp: nonUtcStartTimestamp);

expect(nonUtcStartTimestamp.isUtc, false);
expect(sut.startTimestamp.isUtc, true);
});

test('finish sets status', () async {
final sut = fixture.getSut();

Expand Down Expand Up @@ -171,6 +180,18 @@ void main() {
expect(NoOpSentrySpan(), span);
});

test(
'startChild isnt allowed to be called if childs startTimestamp is before parents',
() async {
final parentStartTimestamp = DateTime.now();
final childStartTimestamp = parentStartTimestamp.add(-Duration(hours: 1));
final sut = fixture.getSut(startTimestamp: parentStartTimestamp);

final span = sut.startChild('op', startTimestamp: childStartTimestamp);

expect(NoOpSentrySpan(), span);
});

test('callback called on finish', () async {
var numberOfCallbackCalls = 0;
final sut = fixture.getSut(finishedCallback: () {
Expand Down Expand Up @@ -201,13 +222,17 @@ class Fixture {
late SentryTracer tracer;
final hub = MockHub();

SentrySpan getSut({bool? sampled = true, Function()? finishedCallback}) {
SentrySpan getSut(
{DateTime? startTimestamp,
bool? sampled = true,
Function()? finishedCallback}) {
tracer = SentryTracer(context, hub);

return SentrySpan(
tracer,
context,
hub,
startTimestamp: startTimestamp,
sampled: sampled,
finishedCallback: finishedCallback,
);
Expand Down
1 change: 1 addition & 0 deletions flutter/test/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ISentrySpan startTransactionShim(
String? name,
String? operation, {
String? description,
DateTime? startTimestamp,
bool? bindToScope,
bool? waitForChildren,
Duration? autoFinishAfter,
Expand Down
12 changes: 9 additions & 3 deletions flutter/test/mocks.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ class MockNoOpSentrySpan extends _i1.Mock implements _i2.NoOpSentrySpan {
super.noSuchMethod(Invocation.method(#setTag, [key, value]),
returnValueForMissingStub: null);
@override
_i2.ISentrySpan startChild(String? operation, {String? description}) =>
_i2.ISentrySpan startChild(String? operation,
{String? description, DateTime? startTimestamp}) =>
(super.noSuchMethod(
Invocation.method(
#startChild, [operation], {#description: description}),
Invocation.method(#startChild, [operation],
{#description: description, #startTimestamp: startTimestamp}),
returnValue: _FakeISentrySpan_2()) as _i2.ISentrySpan);
@override
_i3.SentryTraceHeader toSentryTrace() =>
Expand Down Expand Up @@ -211,6 +212,7 @@ class MockHub extends _i1.Mock implements _i5.Hub {
@override
_i2.ISentrySpan startTransaction(String? name, String? operation,
{String? description,
DateTime? startTimestamp,
bool? bindToScope,
bool? waitForChildren,
Duration? autoFinishAfter,
Expand All @@ -222,6 +224,7 @@ class MockHub extends _i1.Mock implements _i5.Hub {
operation
], {
#description: description,
#startTimestamp: startTimestamp,
#bindToScope: bindToScope,
#waitForChildren: waitForChildren,
#autoFinishAfter: autoFinishAfter,
Expand All @@ -230,6 +233,7 @@ class MockHub extends _i1.Mock implements _i5.Hub {
}),
returnValue: _i11.startTransactionShim(name, operation,
description: description,
startTimestamp: startTimestamp,
bindToScope: bindToScope,
waitForChildren: waitForChildren,
autoFinishAfter: autoFinishAfter,
Expand All @@ -240,6 +244,7 @@ class MockHub extends _i1.Mock implements _i5.Hub {
_i2.ISentrySpan startTransactionWithContext(
_i2.SentryTransactionContext? transactionContext,
{Map<String, dynamic>? customSamplingContext,
DateTime? startTimestamp,
bool? bindToScope,
bool? waitForChildren,
Duration? autoFinishAfter,
Expand All @@ -249,6 +254,7 @@ class MockHub extends _i1.Mock implements _i5.Hub {
transactionContext
], {
#customSamplingContext: customSamplingContext,
#startTimestamp: startTimestamp,
#bindToScope: bindToScope,
#waitForChildren: waitForChildren,
#autoFinishAfter: autoFinishAfter,
Expand Down