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

Auto performance monitoring for widgets #1137

Merged
merged 20 commits into from Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions dart/lib/src/noop_sentry_span.dart
Expand Up @@ -86,4 +86,7 @@ class NoOpSentrySpan extends ISentrySpan {

@override
SentryTracesSamplingDecision? get samplingDecision => null;

@override
void scheduleFinish() {}
}
27 changes: 27 additions & 0 deletions dart/lib/src/protocol/breadcrumb.dart
Expand Up @@ -81,6 +81,33 @@ class Breadcrumb {
);
}

factory Breadcrumb.userInteraction({
String? message,
SentryLevel? level,
DateTime? timestamp,
Map<String, dynamic>? data,
required String subCategory,
String? viewId,
String? viewClass,
}) {
final newData = data ?? {};
if (viewId != null) {
newData['view.id'] = viewId;
}
if (viewClass != null) {
newData['view.class'] = viewClass;
}

return Breadcrumb(
message: message,
level: level,
category: 'ui.$subCategory',
type: 'user',
timestamp: timestamp,
data: newData,
);
}

/// Describes the breadcrumb.
///
/// This field is optional and may be set to null.
Expand Down
3 changes: 3 additions & 0 deletions dart/lib/src/protocol/sentry_span.dart
Expand Up @@ -197,4 +197,7 @@ class SentrySpan extends ISentrySpan {

@override
SentryTraceContextHeader? traceContext() => _tracer.traceContext();

@override
void scheduleFinish() {}
}
3 changes: 3 additions & 0 deletions dart/lib/src/sentry_span_interface.dart
Expand Up @@ -71,4 +71,7 @@ abstract class ISentrySpan {
/// Returns the trace context.
@experimental
SentryTraceContextHeader? traceContext();

@internal
void scheduleFinish();
}
34 changes: 29 additions & 5 deletions dart/lib/src/sentry_tracer.dart
Expand Up @@ -18,6 +18,7 @@ class SentryTracer extends ISentrySpan {
final Map<String, SentryMeasurement> _measurements = {};

Timer? _autoFinishAfterTimer;
Duration? _autoFinishAfter;
Function(SentryTracer)? _onFinish;
var _finishStatus = SentryTracerFinishStatus.notFinishing();
late final bool _trimEnd;
Expand Down Expand Up @@ -56,11 +57,9 @@ class SentryTracer extends ISentrySpan {
startTimestamp: startTimestamp,
);
_waitForChildren = waitForChildren;
if (autoFinishAfter != null) {
_autoFinishAfterTimer = Timer(autoFinishAfter, () async {
await finish(status: status ?? SpanStatus.ok());
});
}
_autoFinishAfter = autoFinishAfter;

_scheduleTimer();
name = transactionContext.name;
// always default to custom if not provided
transactionNameSource = transactionContext.transactionNameSource ??
Expand Down Expand Up @@ -117,6 +116,11 @@ class SentryTracer extends ISentrySpan {
}
});

// if it's an idle transaction which has no children, we drop it to save user's quota
if (children.isEmpty && _autoFinishAfter != null) {
return;
}

final transaction = SentryTransaction(this);
transaction.measurements.addAll(_measurements);
await _hub.captureTransaction(
Expand Down Expand Up @@ -346,4 +350,24 @@ class SentryTracer extends ISentrySpan {
@override
SentryTracesSamplingDecision? get samplingDecision =>
_rootSpan.samplingDecision;

@override
void scheduleFinish() {
if (finished) {
return;
}
if (_autoFinishAfterTimer != null) {
_autoFinishAfterTimer?.cancel();
_scheduleTimer();
}
}

void _scheduleTimer() {
final autoFinishAfter = _autoFinishAfter;
if (autoFinishAfter != null) {
_autoFinishAfterTimer = Timer(autoFinishAfter, () async {
await finish(status: status ?? SpanStatus.ok());
});
}
}
}
31 changes: 24 additions & 7 deletions flutter/example/lib/main.dart
Expand Up @@ -31,15 +31,19 @@ Future<void> main() async {
options.attachThreads = true;
options.enableWindowMetricBreadcrumbs = true;
options.addIntegration(LoggingIntegration());
options.attachScreenshot = true;
options.sendDefaultPii = true;
options.reportSilentFlutterErrors = true;
options.enableNdkScopeSync = true;

// options.attachScreenshot = true;
// We can enable Sentry debug logging during development. This is likely
// going to log too much for your app, but can be useful when figuring out
// configuration issues, e.g. finding out why your events are not uploaded.
options.debug = true;
},
// Init your App.
appRunner: () => runApp(
SentryScreenshotWidget(
SentryUserInteractionWidget(
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
child: DefaultAssetBundle(
bundle: SentryAssetBundle(enableStructuredDataTracing: true),
child: MyApp(),
Expand Down Expand Up @@ -122,6 +126,7 @@ class MainScaffold extends StatelessWidget {
),
ElevatedButton(
onPressed: () => tryCatch(),
key: Key('dart_try_catch'),
child: const Text('Dart: try catch'),
),
ElevatedButton(
Expand Down Expand Up @@ -199,10 +204,17 @@ class MainScaffold extends StatelessWidget {
child: const Text('Flutter: Load assets'),
),
ElevatedButton(
onPressed: () => makeWebRequestWithDio(context),
key: Key('dio_web_request'),
onPressed: () async => await makeWebRequestWithDio(context),
child: const Text('Dio: Web request'),
),
ElevatedButton(
key: Key('dio_web_request_2'),
onPressed: () async => await makeWebRequestWithDio(context),
child: const Text('Dio: Web request 2'),
),
ElevatedButton(
key: Key('print_breadcrumb'),
onPressed: () {
print('A print breadcrumb');
Sentry.captureMessage('A message with a print() Breadcrumb');
Expand Down Expand Up @@ -558,6 +570,7 @@ Future<void> makeWebRequestWithDio(BuildContext context) async {
dio.addSentry(
captureFailedRequests: true,
maxRequestBodySize: MaxRequestBodySize.always,
maxResponseBodySize: MaxResponseBodySize.always,
);

final transaction = Sentry.getSpan() ??
Expand All @@ -566,16 +579,20 @@ Future<void> makeWebRequestWithDio(BuildContext context) async {
'request',
bindToScope: true,
);
final span = transaction.startChild(
'dio',
description: 'desc',
);
Response<String>? response;
try {
response = await dio.get<String>('https://flutter.dev/');
transaction.status = SpanStatus.ok();
span.status = SpanStatus.ok();
} catch (exception, stackTrace) {
transaction.throwable = exception;
transaction.status = SpanStatus.internalError();
span.throwable = exception;
span.status = SpanStatus.internalError();
await Sentry.captureException(exception, stackTrace: stackTrace);
} finally {
await transaction.finish();
await span.finish();
}

await showDialog<void>(
Expand Down
1 change: 1 addition & 0 deletions flutter/lib/sentry_flutter.dart
Expand Up @@ -9,3 +9,4 @@ export 'src/flutter_sentry_attachment.dart';
export 'src/sentry_asset_bundle.dart';
export 'src/integrations/on_error_integration.dart';
export 'src/screenshot/sentry_screenshot_widget.dart';
export 'src/sentry_user_interaction_widget.dart';