From d57cbdc50e957f0816fa793c0b0af8b1dc979a2e Mon Sep 17 00:00:00 2001 From: Gaurav Saral Date: Wed, 27 Mar 2019 13:46:23 +0530 Subject: [PATCH 1/4] update to capture more in telemetry --- azure-devops/azext_devops/__init__.py | 2 +- .../azext_devops/dev/common/telemetry.py | 39 ++++++++----------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/azure-devops/azext_devops/__init__.py b/azure-devops/azext_devops/__init__.py index c26683cd..a2c4f30b 100644 --- a/azure-devops/azext_devops/__init__.py +++ b/azure-devops/azext_devops/__init__.py @@ -53,7 +53,7 @@ def post_parse_args(_cli_ctx, **kwargs): # we need to set tracking data only after we know that all args are valid, # otherwise we may log EUII data that a user inadvertently sent as an argument # name. We already don't log argument values. - set_tracking_data(kwargs['command'].split()) + set_tracking_data(**kwargs) COMMAND_LOADER_CLS = DevCommandsLoader diff --git a/azure-devops/azext_devops/dev/common/telemetry.py b/azure-devops/azext_devops/dev/common/telemetry.py index dedbd4b4..e2501b66 100644 --- a/azure-devops/azext_devops/dev/common/telemetry.py +++ b/azure-devops/azext_devops/dev/common/telemetry.py @@ -23,31 +23,26 @@ def try_send_telemetry_data(organization): logger.debug('Azure devops telemetry sending failed.') -def set_tracking_data(argv): +def set_tracking_data(**kwargs): try: vsts_tracking_data.area = 'AzureDevopsCli' vsts_tracking_data.properties = {} - if argv: - vsts_tracking_data.feature = argv[0] - else: - vsts_tracking_data.feature = None - if len(argv) > 1: - command = [] - args = [] - command_populated = False - for arg in argv[1:]: - if arg and argv: - if not command_populated and arg[0] != '-': - command.append(arg) - elif arg[0] == '-': - args.append(arg.lstrip('-')) - command_populated = True - if command: - vsts_tracking_data.properties['Command'] = ' '.join(command) - else: - vsts_tracking_data.properties['Command'] = '' - vsts_tracking_data.properties['Args'] = args - vsts_tracking_data.properties['ShellType'] = _get_shell_type() + command_line_args = vars(kwargs.get('args', None)) + command_line_split = command_line_args['command'].split() + vsts_tracking_data.feature = command_line_split[0] + if len(command_line_split) > 1: + vsts_tracking_data.properties['Command'] = ''.join(command_line_split[1:]) + + args = [] + for key, value in command_line_args.items(): + if value and type(value) == 'str' and not key.startswith('_') and key != 'command': + args.append(key) + + vsts_tracking_data.properties['Args'] = ''.join(args) + vsts_tracking_data.properties['ShellType'] = _get_shell_type() + import sys + vsts_tracking_data.properties['IsInteractive'] = sys.stdin.isatty() + vsts_tracking_data.properties['OutputType'] = command_line_args['_output_format'] except BaseException as ex: logger.debug(ex, exc_info=True) From 62b01d4d2222181ad33dc42f5905f47a53f1ba7a Mon Sep 17 00:00:00 2001 From: Gaurav Saral Date: Wed, 27 Mar 2019 14:11:00 +0530 Subject: [PATCH 2/4] style fix --- azure-devops/azext_devops/dev/common/telemetry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-devops/azext_devops/dev/common/telemetry.py b/azure-devops/azext_devops/dev/common/telemetry.py index e2501b66..5cc03946 100644 --- a/azure-devops/azext_devops/dev/common/telemetry.py +++ b/azure-devops/azext_devops/dev/common/telemetry.py @@ -35,7 +35,7 @@ def set_tracking_data(**kwargs): args = [] for key, value in command_line_args.items(): - if value and type(value) == 'str' and not key.startswith('_') and key != 'command': + if value and isinstance(value, str) and not key.startswith('_') and key != 'command': args.append(key) vsts_tracking_data.properties['Args'] = ''.join(args) From d643a38eaa05ddfc5b015709f65c5cadb1480054 Mon Sep 17 00:00:00 2001 From: Gaurav Saral Date: Wed, 27 Mar 2019 14:25:08 +0530 Subject: [PATCH 3/4] minor minor --- azure-devops/azext_devops/dev/common/telemetry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-devops/azext_devops/dev/common/telemetry.py b/azure-devops/azext_devops/dev/common/telemetry.py index 5cc03946..652866d6 100644 --- a/azure-devops/azext_devops/dev/common/telemetry.py +++ b/azure-devops/azext_devops/dev/common/telemetry.py @@ -41,7 +41,7 @@ def set_tracking_data(**kwargs): vsts_tracking_data.properties['Args'] = ''.join(args) vsts_tracking_data.properties['ShellType'] = _get_shell_type() import sys - vsts_tracking_data.properties['IsInteractive'] = sys.stdin.isatty() + vsts_tracking_data.properties['IsInteractive'] = str(sys.stdin.isatty()) vsts_tracking_data.properties['OutputType'] = command_line_args['_output_format'] except BaseException as ex: From c44ed9c5702d3f7387f35d87911137046afebe1c Mon Sep 17 00:00:00 2001 From: Gaurav Saral Date: Wed, 27 Mar 2019 15:09:21 +0530 Subject: [PATCH 4/4] fix unit test and some more changes --- .../azext_devops/dev/common/telemetry.py | 4 ++-- .../test/common/test_telemetry.py | 22 ++++++++++--------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/azure-devops/azext_devops/dev/common/telemetry.py b/azure-devops/azext_devops/dev/common/telemetry.py index 652866d6..516b58dd 100644 --- a/azure-devops/azext_devops/dev/common/telemetry.py +++ b/azure-devops/azext_devops/dev/common/telemetry.py @@ -31,14 +31,14 @@ def set_tracking_data(**kwargs): command_line_split = command_line_args['command'].split() vsts_tracking_data.feature = command_line_split[0] if len(command_line_split) > 1: - vsts_tracking_data.properties['Command'] = ''.join(command_line_split[1:]) + vsts_tracking_data.properties['Command'] = ' '.join(command_line_split[1:]) args = [] for key, value in command_line_args.items(): if value and isinstance(value, str) and not key.startswith('_') and key != 'command': args.append(key) - vsts_tracking_data.properties['Args'] = ''.join(args) + vsts_tracking_data.properties['Args'] = ' '.join(args) vsts_tracking_data.properties['ShellType'] = _get_shell_type() import sys vsts_tracking_data.properties['IsInteractive'] = str(sys.stdin.isatty()) diff --git a/azure-devops/azext_devops/test/common/test_telemetry.py b/azure-devops/azext_devops/test/common/test_telemetry.py index fb4851bf..0b61c9c7 100644 --- a/azure-devops/azext_devops/test/common/test_telemetry.py +++ b/azure-devops/azext_devops/test/common/test_telemetry.py @@ -12,8 +12,7 @@ # Attempt to load mock (works on Python version below 3.3) from mock import patch -from azext_devops.dev.common.telemetry import (set_tracking_data, - try_send_telemetry_data, vsts_tracking_data) +from azext_devops.dev.common.telemetry import (set_tracking_data, try_send_telemetry_data) class TestTelemetryMethods(unittest.TestCase): @@ -39,14 +38,17 @@ def test_send_telemetry_should_not_send_if_disabled(self): def test_set_tracking_data_should_set_data_correctly_with_command(self): - set_tracking_data(['devops', 'project', 'list']) - assert vsts_tracking_data.area == 'AzureDevopsCli' - assert vsts_tracking_data.feature == 'devops' - assert vsts_tracking_data.properties['Command'] == 'project list' + class FakeTelemetryClass: + def __init__(self, command): + self.command = command + def __iter__(): + for key, value in self.command: + yield(key, value) - def test_set_tracking_data_should_set_data_correctly_with_none(self): - set_tracking_data(None) + command_dict = FakeTelemetryClass('devops repos list') + set_tracking_data(**{'args': command_dict}) + from azext_devops.dev.common.telemetry import vsts_tracking_data assert vsts_tracking_data.area == 'AzureDevopsCli' - assert vsts_tracking_data.feature == None - assert vsts_tracking_data.properties == {} + assert vsts_tracking_data.feature == 'devops' + assert vsts_tracking_data.properties['Command'] == 'repos list'