Skip to content

Commit

Permalink
Add analytics to analyzer-cli and analysis server.
Browse files Browse the repository at this point in the history
BUG=
R=brianwilkerson@google.com, scheglov@google.com, zra@google.com

Review-Url: https://codereview.chromium.org/2963323002 .
  • Loading branch information
devoncarew committed Jul 6, 2017
1 parent 2bfde5a commit d4abd49
Show file tree
Hide file tree
Showing 33 changed files with 394 additions and 89 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ vars = {
"test_tag": "@0.12.18+1",
"tuple_tag": "@v1.0.1",
"typed_data_tag": "@1.1.3",
"usage_tag": "@3.2.0+1",
"usage_tag": "@3.3.0",
"utf_tag": "@0.9.0+3",
"watcher_tag": "@0.9.7+3",
"web_components_rev": "@6349e09f9118dce7ae1b309af5763745e25a9d61",
Expand Down
3 changes: 3 additions & 0 deletions pkg/analysis_server/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ dart_package("analysis_server") {

deps = [
"//dart/pkg/analyzer",
"//dart/pkg/telemetry",
"//dart/third_party/pkg/linter",
"//third_party/dart-pkg/pub/args",
"//third_party/dart-pkg/pub/dart_style",
"//third_party/dart-pkg/pub/intl",
"//third_party/dart-pkg/pub/isolate",
"//third_party/dart-pkg/pub/logging",
"//third_party/dart-pkg/pub/package_config",
"//third_party/dart-pkg/pub/path",
"//third_party/dart-pkg/pub/plugin",
"//third_party/dart-pkg/pub/usage",
"//third_party/dart-pkg/pub/watcher",
"//third_party/dart-pkg/pub/yaml",
]
Expand Down
30 changes: 27 additions & 3 deletions pkg/analysis_server/lib/src/analysis_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ import 'package:front_end/src/base/performace_logger.dart';
import 'package:front_end/src/incremental/byte_store.dart';
import 'package:front_end/src/incremental/file_byte_store.dart';
import 'package:plugin/plugin.dart';
import 'package:telemetry/crash_reporting.dart';
import 'package:telemetry/telemetry.dart' as telemetry;
import 'package:watcher/watcher.dart';

typedef void OptionUpdater(AnalysisOptionsImpl options);
Expand Down Expand Up @@ -778,6 +780,9 @@ class AnalysisServer {
new ServerErrorParams(fatal, message, buffer.toString())
.toNotification());

// send to crash reporting
options.crashReportSender?.sendReport(exception, stackTrace: stackTrace);

// remember the last few exceptions
if (exception is CaughtException) {
stackTrace ??= exception.stackTrace;
Expand Down Expand Up @@ -904,8 +909,13 @@ class AnalysisServer {
return contextManager.isInAnalysisRoot(file);
}

void shutdown() {
Future<Null> shutdown() async {
running = false;

await options.analytics
?.waitForLastPing(timeout: new Duration(milliseconds: 200));
options.analytics?.close();

// Defer closing the channel and shutting down the instrumentation server so
// that the shutdown response can be sent and logged.
new Future(() {
Expand Down Expand Up @@ -1043,16 +1053,30 @@ class AnalysisServer {
}
}

/**
* Various IDE options.
*/
class AnalysisServerOptions {
bool useAnalysisHighlight2 = false;
bool enableVerboseFlutterCompletions = false;

String fileReadMode = 'as-is';
String newAnalysisDriverLog;

String clientId;
String clientVersion;

// IDE options
bool enableVerboseFlutterCompletions = false;
/**
* The analytics instance; note, this object can be `null`, and should be
* accessed via a null-aware operator.
*/
telemetry.Analytics analytics;

/**
* The crash report sender instance; note, this object can be `null`, and
* should be accessed via a null-aware operator.
*/
CrashReportSender crashReportSender;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions pkg/analysis_server/lib/src/domain_analysis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ class AnalysisDomainHandler extends AbstractRequestHandler {
* Implement the 'analysis.reanalyze' request.
*/
Response reanalyze(Request request) {
server.options.analytics?.sendEvent('analysis', 'reanalyze');

AnalysisReanalyzeParams params =
new AnalysisReanalyzeParams.fromRequest(request);
List<String> roots = params.roots;
Expand Down Expand Up @@ -314,6 +316,10 @@ class AnalysisDomainHandler extends AbstractRequestHandler {
var params = new AnalysisSetAnalysisRootsParams.fromRequest(request);
List<String> includedPathList = params.included;
List<String> excludedPathList = params.excluded;

server.options.analytics?.sendEvent('analysis', 'setAnalysisRoots',
value: includedPathList.length);

// validate
for (String path in includedPathList) {
if (!server.isValidFilePath(path)) {
Expand Down
11 changes: 7 additions & 4 deletions pkg/analysis_server/lib/src/domain_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';

import 'package:analysis_server/protocol/protocol.dart';
import 'package:analysis_server/protocol/protocol_constants.dart';
import 'package:analysis_server/protocol/protocol_generated.dart';
Expand Down Expand Up @@ -39,7 +41,8 @@ class ServerDomainHandler implements RequestHandler {
} else if (requestName == SERVER_REQUEST_SET_SUBSCRIPTIONS) {
return setSubscriptions(request);
} else if (requestName == SERVER_REQUEST_SHUTDOWN) {
return shutdown(request);
shutdown(request);
return Response.DELAYED_RESPONSE;
}
} on RequestFailure catch (exception) {
return exception.response;
Expand All @@ -63,9 +66,9 @@ class ServerDomainHandler implements RequestHandler {
/**
* Cleanly shutdown the analysis server.
*/
Response shutdown(Request request) {
server.shutdown();
Future<Null> shutdown(Request request) async {
await server.shutdown();
Response response = new ServerShutdownResult().toResponse(request.id);
return response;
server.sendResponse(response);
}
}
12 changes: 12 additions & 0 deletions pkg/analysis_server/lib/src/edit/edit_domain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class EditDomainHandler extends AbstractRequestHandler {
}

Response format(Request request) {
server.options.analytics?.sendEvent('edit', 'format');

EditFormatParams params = new EditFormatParams.fromRequest(request);
String file = params.file;

Expand Down Expand Up @@ -270,6 +272,8 @@ class EditDomainHandler extends AbstractRequestHandler {
}

Future getPostfixCompletion(Request request) async {
server.options.analytics?.sendEvent('edit', 'getPostfixCompletion');

var params = new EditGetPostfixCompletionParams.fromRequest(request);
SourceChange change;

Expand Down Expand Up @@ -448,6 +452,8 @@ class EditDomainHandler extends AbstractRequestHandler {
}

Future<Null> organizeDirectives(Request request) async {
server.options.analytics?.sendEvent('edit', 'organizeDirectives');

var params = new EditOrganizeDirectivesParams.fromRequest(request);
// prepare file
String file = params.file;
Expand Down Expand Up @@ -717,6 +723,12 @@ class _RefactoringManager {
EMPTY_PROBLEM_LIST, EMPTY_PROBLEM_LIST, EMPTY_PROBLEM_LIST);
// process the request
var params = new EditGetRefactoringParams.fromRequest(_request);

if (params.kind != null) {
server.options.analytics
?.sendEvent('refactor', params.kind.name.toLowerCase());
}

runZoned(() async {
await _init(params.kind, params.file, params.offset, params.length);
if (initStatus.hasFatalError) {
Expand Down
96 changes: 74 additions & 22 deletions pkg/analysis_server/lib/src/server/driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import 'package:args/args.dart';
import 'package:linter/src/rules.dart' as linter;
import 'package:plugin/manager.dart';
import 'package:plugin/plugin.dart';
import 'package:telemetry/crash_reporting.dart';
import 'package:telemetry/telemetry.dart' as telemetry;

/// Commandline argument parser. (Copied from analyzer/lib/options.dart)
/// TODO(pquitslund): replaces with a simple [ArgParser] instance
Expand Down Expand Up @@ -125,6 +127,10 @@ class CommandLineParser {
String arg = args[i];
if (arg.startsWith('--') && arg.length > 2) {
String option = arg.substring(2);
// remove any leading 'no-'
if (option.startsWith('no-')) {
option = option.substring(3);
}
// strip the last '=value'
int equalsOffset = option.lastIndexOf('=');
if (equalsOffset != -1) {
Expand Down Expand Up @@ -194,6 +200,16 @@ class Driver implements ServerStarter {
*/
static const String HELP_OPTION = "help";

/**
* The name of the flag used to configure reporting analytics.
*/
static const String ANALYTICS_FLAG = "analytics";

/**
* Suppress analytics for this session.
*/
static const String SUPPRESS_ANALYTICS_FLAG = "suppress-analytics";

/**
* The name of the option used to cause instrumentation to also be written to
* a local file.
Expand Down Expand Up @@ -232,8 +248,6 @@ class Driver implements ServerStarter {

/**
* The path to the SDK.
* TODO(paulberry): get rid of this once the 'analysis.updateSdks' request is
* operational.
*/
static const String SDK_OPTION = "sdk";

Expand Down Expand Up @@ -284,8 +298,43 @@ class Driver implements ServerStarter {
AnalysisServer start(List<String> arguments) {
CommandLineParser parser = _createArgParser();
ArgResults results = parser.parse(arguments, <String, String>{});

AnalysisServerOptions analysisServerOptions = new AnalysisServerOptions();
analysisServerOptions.useAnalysisHighlight2 =
results[USE_ANALYSIS_HIGHLIGHT2];
analysisServerOptions.fileReadMode = results[FILE_READ_MODE];
analysisServerOptions.newAnalysisDriverLog =
results[NEW_ANALYSIS_DRIVER_LOG];
analysisServerOptions.clientId = results[CLIENT_ID];
analysisServerOptions.clientVersion = results[CLIENT_VERSION];
analysisServerOptions.enableVerboseFlutterCompletions =
results[VERBOSE_FLUTTER_COMPLETIONS];

telemetry.Analytics analytics = telemetry.createAnalyticsInstance(
'UA-26406144-29', 'analysis-server',
disableForSession: results[SUPPRESS_ANALYTICS_FLAG]);
analysisServerOptions.analytics = analytics;

if (analysisServerOptions.clientId != null) {
// Record the client name as the application installer ID.
analytics.setSessionValue('aiid', analysisServerOptions.clientId);
}
if (analysisServerOptions.clientVersion != null) {
analytics.setSessionValue('cd1', analysisServerOptions.clientVersion);
}

// TODO(devoncarew): Replace with the real crash product ID.
analysisServerOptions.crashReportSender =
new CrashReportSender('Dart_analysis_server', analytics);

if (results.wasParsed(ANALYTICS_FLAG)) {
analytics.enabled = results[ANALYTICS_FLAG];
print(telemetry.createAnalyticsStatusMessage(analytics.enabled));
return null;
}

if (results[HELP_OPTION]) {
_printUsage(parser.parser);
_printUsage(parser.parser, analytics, fromHelp: true);
return null;
}

Expand All @@ -298,25 +347,12 @@ class Driver implements ServerStarter {
} on FormatException {
print('Invalid port number: ${results[PORT_OPTION]}');
print('');
_printUsage(parser.parser);
_printUsage(parser.parser, analytics);
exitCode = 1;
return null;
}
}

AnalysisServerOptions analysisServerOptions = new AnalysisServerOptions();
analysisServerOptions.useAnalysisHighlight2 =
results[USE_ANALYSIS_HIGHLIGHT2];
analysisServerOptions.fileReadMode = results[FILE_READ_MODE];
analysisServerOptions.newAnalysisDriverLog =
results[NEW_ANALYSIS_DRIVER_LOG];

analysisServerOptions.clientId = results[CLIENT_ID];
analysisServerOptions.clientVersion = results[CLIENT_VERSION];

analysisServerOptions.enableVerboseFlutterCompletions =
results[VERBOSE_FLUTTER_COMPLETIONS];

//
// Process all of the plugins so that extensions are registered.
//
Expand Down Expand Up @@ -361,14 +397,17 @@ class Driver implements ServerStarter {
new InstrumentationService(instrumentationServer);
instrumentationService.logVersion(
_readUuid(instrumentationService),
results[CLIENT_ID],
results[CLIENT_VERSION],
analysisServerOptions.clientId,
analysisServerOptions.clientVersion,
AnalysisServer.VERSION,
defaultSdk.sdkVersion);
AnalysisEngine.instance.instrumentationService = instrumentationService;

_DiagnosticServerImpl diagnosticServer = new _DiagnosticServerImpl();

// Ping analytics with our initial call.
analytics.sendScreenView('home');

//
// Create the sockets and start listening for requests.
//
Expand Down Expand Up @@ -452,8 +491,7 @@ class Driver implements ServerStarter {
defaultsTo: false,
negatable: false);
parser.addOption(INSTRUMENTATION_LOG_FILE,
help:
"the path of the file to which instrumentation data will be written");
help: "write instrumentation data to the given file");
parser.addFlag(INTERNAL_PRINT_TO_CONSOLE,
help: "enable sending `print` output to the console",
defaultsTo: false,
Expand All @@ -462,6 +500,10 @@ class Driver implements ServerStarter {
help: "set a destination for the new analysis driver's log");
parser.addFlag(VERBOSE_FLUTTER_COMPLETIONS,
help: "enable verbose code completion for Flutter (experimental)");
parser.addFlag(ANALYTICS_FLAG,
help: 'enable or disable sending analytics information to Google');
parser.addFlag(SUPPRESS_ANALYTICS_FLAG,
negatable: false, help: 'suppress analytics for this session');
parser.addOption(PORT_OPTION,
help: "the http diagnostic port on which the server provides"
" status and performance information");
Expand Down Expand Up @@ -497,11 +539,21 @@ class Driver implements ServerStarter {
/**
* Print information about how to use the server.
*/
void _printUsage(ArgParser parser) {
void _printUsage(ArgParser parser, telemetry.Analytics analytics,
{bool fromHelp: false}) {
print('Usage: $BINARY_NAME [flags]');
print('');
print('Supported flags are:');
print(parser.usage);

// Print analytics status and information.
if (fromHelp) {
print('');
print(telemetry.analyticsNotice);
}
print('');
print(telemetry.createAnalyticsStatusMessage(analytics.enabled,
command: ANALYTICS_FLAG));
}

/**
Expand Down
2 changes: 2 additions & 0 deletions pkg/analysis_server/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ dependencies:
package_config: '>=0.1.5 <2.0.0'
path: any
plugin: ^0.2.0
telemetry: ^0.0.1
usage: ^3.2.0+1
watcher: any
yaml: any
dev_dependencies:
Expand Down
Loading

0 comments on commit d4abd49

Please sign in to comment.