diff --git a/azure-cli.pyproj b/azure-cli.pyproj index 272586ace28..8982d5eb059 100644 --- a/azure-cli.pyproj +++ b/azure-cli.pyproj @@ -52,6 +52,9 @@ + + Code + diff --git a/requirements.txt b/requirements.txt index 558ba24df39..0da2a62bd58 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +applicationinsights==0.10.0 azure==2.0.0rc1 mock==1.3.0 pylint==1.5.4 diff --git a/src/azure/cli/__main__.py b/src/azure/cli/__main__.py index 666cda4f24e..9ceb748a4ee 100644 --- a/src/azure/cli/__main__.py +++ b/src/azure/cli/__main__.py @@ -2,4 +2,15 @@ import azure.cli.main -sys.exit(azure.cli.main.main(sys.argv[1:])) +from ._telemetry import init_telemetry, user_agrees_to_telemetry, telemetry_flush + +try: + try: + if user_agrees_to_telemetry(): + init_telemetry() + except Exception: #pylint: disable=broad-except + pass + + sys.exit(azure.cli.main.main(sys.argv[1:])) +finally: + telemetry_flush() diff --git a/src/azure/cli/_telemetry.py b/src/azure/cli/_telemetry.py new file mode 100644 index 00000000000..d6a504b61b7 --- /dev/null +++ b/src/azure/cli/_telemetry.py @@ -0,0 +1,34 @@ +import getpass +from applicationinsights import TelemetryClient +from applicationinsights.exceptions import enable +import azure.cli as cli + +client = {} + +def init_telemetry(): + try: + instrumentation_key = 'eb6e9d3a-b6ee-41a6-804f-70e152fdfc36' + + global client #pylint: disable=global-statement + client = TelemetryClient(instrumentation_key) + + client.context.application.id = 'Azure CLI' + client.context.application.ver = cli.__version__ + client.context.user.id = hash(getpass.getuser()) + + enable(instrumentation_key) + except Exception: #pylint: disable=broad-except + # Never fail the command because of telemetry + pass + +def user_agrees_to_telemetry(): + # TODO: agreement, needs to take Y/N from the command line + # and needs a "skip" param to not show (for scripts) + return True + +def telemetry_flush(): + try: + client.flush() + except Exception: #pylint: disable=broad-except + # Never fail the command because of telemetry + pass