Skip to content

Commit be2866b

Browse files
devoncarewcommit-bot@chromium.org
authored andcommitted
[analyzer] update lib/crash_reporting.dart
Change-Id: Ie3b3ffc8a0e0351341f28ff26bbb3c82deb0b5dc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/124640 Reviewed-by: Mike Fairhurst <mfairhurst@google.com> Commit-Queue: Devon Carew <devoncarew@google.com>
1 parent 8ca18f1 commit be2866b

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

pkg/analysis_server/lib/src/server/driver.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,13 @@ class Driver implements ServerStarter {
424424
analytics.setSessionValue('cd1', analysisServerOptions.clientVersion);
425425
}
426426

427+
final shouldSendCallback = () {
428+
// TODO(devoncarew): Replace with a real enablement check.
429+
return false;
430+
};
431+
427432
final crashReportSender =
428-
new CrashReportSender('Dart_analysis_server', analytics);
433+
new CrashReportSender('Dart_analysis_server', shouldSendCallback);
429434

430435
if (telemetry.SHOW_ANALYTICS_UI) {
431436
if (results.wasParsed(ANALYTICS_FLAG)) {

pkg/telemetry/lib/crash_reporting.dart

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@ import 'dart:io';
77

88
import 'package:http/http.dart' as http;
99
import 'package:stack_trace/stack_trace.dart';
10-
import 'package:usage/usage.dart';
1110

1211
// Reporting disabled until we're set up on the backend.
1312
const bool CRASH_REPORTING_DISABLED = true;
1413

14+
/// Tells crash backend that this is a Dart error (as opposed to, say, Java).
15+
const String _dartTypeId = 'DartError';
16+
1517
/// Crash backend host.
1618
const String _crashServerHost = 'clients2.google.com';
1719

1820
/// Path to the crash servlet.
19-
const String _crashEndpointPath = '/cr/report'; // or, staging_report
21+
const String _crashEndpointPath = '/cr/report'; // or, 'staging_report'
2022

2123
/// The field corresponding to the multipart/form-data file attachment where
2224
/// crash backend expects to find the Dart stack trace.
2325
const String _stackTraceFileField = 'DartError';
2426

25-
/// The name of the file attached as [stackTraceFileField].
27+
/// The name of the file attached as [_stackTraceFileField].
2628
///
2729
/// The precise value is not important. It is ignored by the crash back end, but
2830
/// it must be supplied in the request.
@@ -36,41 +38,43 @@ class CrashReportSender {
3638
scheme: 'https', host: _crashServerHost, path: _crashEndpointPath);
3739

3840
final String crashProductId;
39-
final Analytics analytics;
41+
final EnablementCallback shouldSend;
4042
final http.Client _httpClient;
4143

4244
/// Create a new [CrashReportSender], using the data from the given
4345
/// [Analytics] instance.
44-
CrashReportSender(this.crashProductId, this.analytics,
46+
CrashReportSender(this.crashProductId, this.shouldSend,
4547
{http.Client httpClient})
4648
: _httpClient = httpClient ?? new http.Client();
4749

4850
/// Sends one crash report.
4951
///
5052
/// The report is populated from data in [error] and [stackTrace].
5153
Future sendReport(dynamic error, {StackTrace stackTrace}) async {
52-
if (!analytics.enabled || CRASH_REPORTING_DISABLED) {
54+
if (!shouldSend() || CRASH_REPORTING_DISABLED) {
5355
return;
5456
}
5557

5658
try {
59+
final String dartVersion = Platform.version.split(' ').first;
60+
5761
final Uri uri = _baseUri.replace(
5862
queryParameters: <String, String>{
59-
'product': analytics.trackingId,
60-
'version': analytics.applicationVersion,
63+
'product': crashProductId,
64+
'version': dartVersion,
6165
},
6266
);
6367

6468
final http.MultipartRequest req = new http.MultipartRequest('POST', uri);
65-
req.fields['uuid'] = analytics.clientId;
6669
req.fields['product'] = crashProductId;
67-
req.fields['version'] = analytics.applicationVersion;
70+
req.fields['version'] = dartVersion;
6871
req.fields['osName'] = Platform.operatingSystem;
6972
req.fields['osVersion'] = Platform.operatingSystemVersion;
70-
req.fields['type'] = 'DartError';
73+
req.fields['type'] = _dartTypeId;
7174
req.fields['error_runtime_type'] = '${error.runtimeType}';
75+
req.fields['error_message'] = '$error';
7276

73-
final Chain chain = new Chain.parse(stackTrace.toString());
77+
final Chain chain = new Chain.forTrace(stackTrace);
7478
req.files.add(new http.MultipartFile.fromString(
7579
_stackTraceFileField, chain.terse.toString(),
7680
filename: _stackTraceFilename));
@@ -94,3 +98,7 @@ class CrashReportSender {
9498
_httpClient.close();
9599
}
96100
}
101+
102+
/// A typedef to allow crash reporting to query as to whether it should send a
103+
/// crash report.
104+
typedef bool EnablementCallback();

pkg/telemetry/test/crash_reporting_test.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,20 @@ void main() {
2424
});
2525

2626
test('CrashReportSender', () async {
27+
EnablementCallback shouldSend = () {
28+
return true;
29+
};
30+
2731
AnalyticsMock analytics = new AnalyticsMock()..enabled = true;
2832
CrashReportSender sender = new CrashReportSender(
29-
analytics.trackingId, analytics,
33+
analytics.trackingId, shouldSend,
3034
httpClient: mockClient);
3135

3236
await sender.sendReport('test-error', stackTrace: StackTrace.current);
3337

3438
String body = utf8.decode(request.bodyBytes);
3539
expect(body, contains('String')); // error.runtimeType
36-
expect(body, contains(analytics.trackingId));
37-
expect(body, contains('1.0.0'));
38-
expect(body, contains(analytics.clientId));
40+
expect(body, contains('test-error'));
3941
}, skip: CRASH_REPORTING_DISABLED);
4042
});
4143
}

0 commit comments

Comments
 (0)