From d73dd6b3560620109dfb094505bb63de53471b80 Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Tue, 25 Feb 2020 15:22:17 -0500 Subject: [PATCH] [flutter_tools] Rework iOS vmservice handshake failure usage event (#51419) --- .../lib/src/ios/fallback_discovery.dart | 37 ++++++++++++------- .../test/general.shard/ios/devices_test.dart | 2 +- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/packages/flutter_tools/lib/src/ios/fallback_discovery.dart b/packages/flutter_tools/lib/src/ios/fallback_discovery.dart index cf8883f8bdc..12d9f2d188e 100644 --- a/packages/flutter_tools/lib/src/ios/fallback_discovery.dart +++ b/packages/flutter_tools/lib/src/ios/fallback_discovery.dart @@ -6,6 +6,7 @@ import 'package:meta/meta.dart'; import 'package:vm_service/vm_service.dart'; import 'package:vm_service/vm_service_io.dart' as vm_service_io; +import '../base/io.dart'; import '../base/logger.dart'; import '../device.dart'; import '../mdns_discovery.dart'; @@ -120,19 +121,14 @@ class FallbackDiscovery { } on Exception catch (err) { _logger.printTrace(err.toString()); _logger.printTrace('Failed to connect directly, falling back to mDNS'); - UsageEvent( - _kEventName, - 'failure', - label: err.toString(), - value: hostPort, - ).send(); + _sendFailureEvent(err, assumedDevicePort); return null; } // Attempt to connect to the VM service 5 times. int attempts = 0; const int kDelaySeconds = 2; - Object firstException; + Exception firstException; while (attempts < 5) { try { final VmService vmService = await _vmServiceConnectUri(assumedWsUri.toString()); @@ -163,12 +159,27 @@ class FallbackDiscovery { attempts += 1; } _logger.printTrace('Failed to connect directly, falling back to mDNS'); - UsageEvent( - _kEventName, - 'failure', - label: firstException?.toString() ?? 'Connection attempts exhausted', - value: hostPort, - ).send(); + _sendFailureEvent(firstException, assumedDevicePort); return null; } + + void _sendFailureEvent(Exception err, int assumedDevicePort) { + String eventAction; + String eventLabel; + if (err == null) { + eventAction = 'failure-attempts-exhausted'; + eventLabel = assumedDevicePort.toString(); + } else if (err is HttpException) { + eventAction = 'failure-http'; + eventLabel = '${err.message}, device port = $assumedDevicePort'; + } else { + eventAction = 'failure-other'; + eventLabel = '$err, device port = $assumedDevicePort'; + } + UsageEvent( + _kEventName, + eventAction, + label: eventLabel, + ).send(); + } } diff --git a/packages/flutter_tools/test/general.shard/ios/devices_test.dart b/packages/flutter_tools/test/general.shard/ios/devices_test.dart index 6ec864d2d31..1daacc484c2 100644 --- a/packages/flutter_tools/test/general.shard/ios/devices_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/devices_test.dart @@ -643,7 +643,7 @@ void main() { expect(launchResult.hasObservatory, isFalse); verify(mockUsage.sendEvent( 'ios-handshake', - 'failure', + 'failure-other', label: anyNamed('label'), value: anyNamed('value'), )).called(1);