Skip to content

Commit 25e9b59

Browse files
authored
refactor: use options.clock wherever possible (#1110)
1 parent 2f2a9a9 commit 25e9b59

15 files changed

+88
-27
lines changed

dart/lib/src/default_integrations.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class RunZonedGuardedIntegration extends Integration {
4848
final event = SentryEvent(
4949
throwable: throwableMechanism,
5050
level: SentryLevel.fatal,
51+
timestamp: hub.options.clock(),
5152
);
5253

5354
await hub.captureEvent(event, stackTrace: stackTrace);

dart/lib/src/http_client/failed_request_client.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ class FailedRequestClient extends BaseClient {
198198
final event = SentryEvent(
199199
throwable: throwableMechanism,
200200
request: sentryRequest,
201+
timestamp: _hub.options.clock(),
201202
);
202203

203204
if (response != null) {

dart/lib/src/isolate_error_integration.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Future<void> handleIsolateError(
7272
final event = SentryEvent(
7373
throwable: throwableMechanism,
7474
level: SentryLevel.fatal,
75+
timestamp: hub.options.clock(),
7576
);
7677

7778
await hub.captureEvent(event, stackTrace: stackTrace);

dart/lib/src/protocol/sentry_span.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SentrySpan extends ISentrySpan {
3232
this.samplingDecision,
3333
Function({DateTime? endTimestamp})? finishedCallback,
3434
}) {
35-
_startTimestamp = startTimestamp?.toUtc() ?? getUtcDateTime();
35+
_startTimestamp = startTimestamp?.toUtc() ?? _hub.options.clock();
3636
_finishedCallback = finishedCallback;
3737
}
3838

@@ -42,20 +42,20 @@ class SentrySpan extends ISentrySpan {
4242
return;
4343
}
4444

45-
final utcDateTime = getUtcDateTime();
46-
4745
if (status != null) {
4846
_status = status;
4947
}
5048

51-
if (endTimestamp?.isBefore(_startTimestamp) ?? false) {
49+
if (endTimestamp == null) {
50+
_endTimestamp = _hub.options.clock();
51+
} else if (endTimestamp.isBefore(_startTimestamp)) {
5252
_hub.options.logger(
5353
SentryLevel.warning,
5454
'End timestamp ($endTimestamp) cannot be before start timestamp ($_startTimestamp)',
5555
);
56-
_endTimestamp = utcDateTime;
56+
_endTimestamp = _hub.options.clock();
5757
} else {
58-
_endTimestamp = endTimestamp?.toUtc() ?? utcDateTime;
58+
_endTimestamp = endTimestamp.toUtc();
5959
}
6060

6161
// associate error

dart/lib/src/sentry_options.dart

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class SentryOptions {
4141

4242
/// If [clock] is provided, it is used to get time instead of the system
4343
/// clock. This is useful in tests. Should be an implementation of [ClockProvider].
44+
/// The ClockProvider is expected to return UTC time.
45+
@internal
4446
ClockProvider clock = getUtcDateTime;
4547

4648
int _maxBreadcrumbs = 100;
@@ -87,6 +89,7 @@ class SentryOptions {
8789
_maxSpans = maxSpans;
8890
}
8991

92+
// ignore: deprecated_member_use_from_same_package
9093
SentryLogger _logger = noOpLogger;
9194

9295
/// Logger interface to log useful debugging information if debug is enabled
@@ -117,10 +120,12 @@ class SentryOptions {
117120

118121
set debug(bool newValue) {
119122
_debug = newValue;
123+
// ignore: deprecated_member_use_from_same_package
120124
if (_debug == true && logger == noOpLogger) {
121-
_logger = dartLogger;
125+
_logger = _debugLogger;
122126
}
123-
if (_debug == false && logger == dartLogger) {
127+
if (_debug == false && logger == _debugLogger) {
128+
// ignore: deprecated_member_use_from_same_package
124129
_logger = noOpLogger;
125130
}
126131
}
@@ -359,6 +364,23 @@ class SentryOptions {
359364
@internal
360365
late SentryClientAttachmentProcessor clientAttachmentProcessor =
361366
SentryClientAttachmentProcessor();
367+
368+
void _debugLogger(
369+
SentryLevel level,
370+
String message, {
371+
String? logger,
372+
Object? exception,
373+
StackTrace? stackTrace,
374+
}) {
375+
log(
376+
'[${level.name}] $message',
377+
level: level.toDartLogLevel(),
378+
name: logger ?? 'sentry',
379+
time: clock(),
380+
error: exception,
381+
stackTrace: stackTrace,
382+
);
383+
}
362384
}
363385

364386
/// This function is called with an SDK specific event object and can return a modified event
@@ -391,6 +413,7 @@ typedef TracesSamplerCallback = double? Function(
391413
SentrySamplingContext samplingContext);
392414

393415
/// A NoOp logger that does nothing
416+
@Deprecated('This will be made private or removed in the future')
394417
void noOpLogger(
395418
SentryLevel level,
396419
String message, {
@@ -400,6 +423,7 @@ void noOpLogger(
400423
}) {}
401424

402425
/// A Logger that prints out the level and message
426+
@Deprecated('This will be made private or removed in the future')
403427
void dartLogger(
404428
SentryLevel level,
405429
String message, {

dart/lib/src/sentry_tracer.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'dart:async';
22

33
import 'package:intl/intl.dart';
44
import 'package:meta/meta.dart';
5-
import 'utils.dart';
65

76
import '../sentry.dart';
87
import 'sentry_tracer_finish_status.dart';
@@ -72,7 +71,7 @@ class SentryTracer extends ISentrySpan {
7271

7372
@override
7473
Future<void> finish({SpanStatus? status, DateTime? endTimestamp}) async {
75-
final commonEndTimestamp = endTimestamp ?? getUtcDateTime();
74+
final commonEndTimestamp = endTimestamp ?? _hub.options.clock();
7675
_autoFinishAfterTimer?.cancel();
7776
_finishStatus = SentryTracerFinishStatus.finishing(status);
7877
if (!_rootSpan.finished &&

dart/test/sentry_options_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ void main() {
3434

3535
test('SentryLogger sets a diagnostic logger', () {
3636
final options = SentryOptions(dsn: fakeDsn);
37+
// ignore: deprecated_member_use_from_same_package
38+
expect(options.logger, noOpLogger);
39+
// ignore: deprecated_member_use_from_same_package
3740
options.logger = dartLogger;
3841

39-
expect(false, options.logger == noOpLogger);
42+
// ignore: deprecated_member_use_from_same_package
43+
expect(options.logger, isNot(noOpLogger));
4044
});
4145

4246
test('tracesSampler is null by default', () {

dart/test/sentry_test.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,23 @@ void main() {
342342
}, options: sentryOptions);
343343
});
344344

345-
test('options.logger is not dartLogger after debug = false', () async {
345+
test('options.logger is set by setting the debug flag', () async {
346346
final sentryOptions =
347347
SentryOptions(dsn: fakeDsn, checker: FakePlatformChecker.debugMode());
348348

349349
await Sentry.init((options) {
350350
options.dsn = fakeDsn;
351351
options.debug = true;
352-
expect(options.logger, dartLogger);
352+
// ignore: deprecated_member_use_from_same_package
353+
expect(options.logger, isNot(noOpLogger));
353354

354355
options.debug = false;
356+
// ignore: deprecated_member_use_from_same_package
355357
expect(options.logger, noOpLogger);
356358
}, options: sentryOptions);
357359

358-
expect(sentryOptions.logger == dartLogger, false);
360+
// ignore: deprecated_member_use_from_same_package
361+
expect(sentryOptions.logger, isNot(dartLogger));
359362
});
360363

361364
group("Sentry init optionsConfiguration", () {

flutter/lib/src/integrations/flutter_error_integration.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class FlutterErrorIntegration extends Integration<SentryFlutterOptions> {
7474
contexts: flutterErrorDetails.isNotEmpty
7575
? (Contexts()..['flutter_error_details'] = flutterErrorDetails)
7676
: null,
77+
// ignore: invalid_use_of_internal_member
78+
timestamp: options.clock(),
7779
);
7880

7981
await hub.captureEvent(event, stackTrace: errorDetails.stack);

flutter/lib/src/integrations/native_app_start_integration.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class NativeAppStartIntegration extends Integration<SentryFlutterOptions> {
2424
'Scheduler binding is null. Can\'t auto detect app start time.');
2525
} else {
2626
schedulerBinding.addPostFrameCallback((timeStamp) {
27-
_native.appStartEnd = DateTime.now().toUtc();
27+
// ignore: invalid_use_of_internal_member
28+
_native.appStartEnd = options.clock();
2829
});
2930
}
3031
}

0 commit comments

Comments
 (0)