Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/azure-cli-core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

DEPENDENCIES = [
'argcomplete~=3.1.1',
'azure-cli-telemetry==1.0.8.*',
'azure-cli-telemetry==1.1.0.*',
'azure-mgmt-core>=1.2.0,<2',
'cryptography',
# On Linux, the distribution (Ubuntu, Debian, etc) and version are logged in telemetry
Expand Down
4 changes: 4 additions & 0 deletions src/azure-cli-telemetry/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Release History
===============
1.1.0
+++++
* Drop telemetry cache strategy. Records will be uploaded immediately

1.0.8
+++++
* Keep storing telemetry to CLI AppInsights in local cache but send telemetry to other AppInsights immediately
Expand Down
12 changes: 3 additions & 9 deletions src/azure-cli-telemetry/azure/cli/telemetry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from azure.cli.telemetry.util import save_payload

__version__ = "1.0.8"
__version__ = "1.1.0"

DEFAULT_INSTRUMENTATION_KEY = 'c4395b75-49cc-422c-bc95-c7d51aef5d46'

Expand Down Expand Up @@ -47,15 +47,14 @@ def _start(config_dir):


def save(config_dir, payload):
from azure.cli.telemetry.util import should_upload
from azure.cli.telemetry.components.telemetry_client import CliTelemetryClient
from azure.cli.telemetry.components.telemetry_logging import get_logger

logger = get_logger('main')
try:
# Split payload to cli events and extra events by instrumentation key
# cli events should be stored in local cache and sent together
# extra events can be sent immediately
# cli events will be handled in separate process
import json
events = json.loads(payload)

Expand All @@ -75,14 +74,13 @@ def save(config_dir, payload):
logger.info("Split cli events and extra events failure: %s", str(ex))
cli_payload = payload

if save_payload(config_dir, cli_payload) and should_upload(config_dir):
if save_payload(config_dir, cli_payload):
logger.info('Begin creating telemetry upload process.')
_start(config_dir)
logger.info('Finish creating telemetry upload process.')


def main():
from azure.cli.telemetry.util import should_upload
from azure.cli.telemetry.components.telemetry_note import TelemetryNote
from azure.cli.telemetry.components.records_collection import RecordsCollection
from azure.cli.telemetry.components.telemetry_client import CliTelemetryClient
Expand All @@ -95,10 +93,6 @@ def main():
logger = get_logger('main')
logger.info('Attempt start. Configuration directory [%s].', sys.argv[1])

if not should_upload(config_dir):
logger.info('Exit early. The note file indicates it is not a suitable time to upload telemetry.')
sys.exit(0)

try:
with TelemetryNote(config_dir) as telemetry_note:
telemetry_note.touch()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import shutil
import stat
import tempfile
from knack.config import CLIConfig


class RecordsCollection:
Expand Down Expand Up @@ -36,11 +35,8 @@ def snapshot_and_read(self):
if not os.path.isdir(folder):
return

# Collect all cache.x files. If it has been a long time since last sent, also collect cache file itself.
push_interval = datetime.timedelta(hours=self._get_push_interval_config())
include_cache = datetime.datetime.now() - self._last_sent > push_interval
candidates = [(fn, os.stat(os.path.join(folder, fn))) for fn in os.listdir(folder)
if include_cache or fn != 'cache']
# Collect all cache/cache.x files
candidates = [(fn, os.stat(os.path.join(folder, fn))) for fn in os.listdir(folder)]

# sort the cache files base on their last modification time.
candidates = [(fn, file_stat) for fn, file_stat in candidates if stat.S_ISREG(file_stat.st_mode)]
Expand Down Expand Up @@ -72,12 +68,6 @@ def snapshot_and_read(self):
onerror=lambda _, p, tr: self._logger.error('Fail to remove file %s', p))
self._logger.info('Remove directory %s', tmp)

def _get_push_interval_config(self):
config = CLIConfig(config_dir=self._config_dir)
threshold = config.getint('telemetry', 'push_interval_in_hours', fallback=1)
# the threshold for push telemetry can't be less than 1 hour, default value is 1 hour
return threshold if threshold >= 1 else 1

def _read_file(self, path):
""" Read content of a telemetry cache file and parse them into records. """
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def test_create_records_collection_with_last_send(self):
collection = RecordsCollection(last_send, self.work_dir)
collection.snapshot_and_read()

# the threshold for pushing 'cache' file is 1 hour, so 'cache' file should not be moved
self.assert_cache_files_count(1)
# cache strategy has been dropped, so no `cache` file
self.assert_cache_files_count(0)
# no new records since last_send
self.assertEqual(0, len([r for r in collection]))

Expand Down
33 changes: 0 additions & 33 deletions src/azure-cli-telemetry/azure/cli/telemetry/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,8 @@
# --------------------------------------------------------------------------------------------

import os
import stat
import logging
import logging.handlers
from datetime import datetime

from azure.cli.telemetry.const import TELEMETRY_NOTE_NAME, MANDATORY_WAIT_PERIOD


def should_upload(config_dir):
"""Returns True if it is the right moment to add telemetry.
Before the client request a telemetry add process, it can invoke this method to test if it is the right moment.
The conditions are:
1. The telemetry.txt file doesn't exist; OR
2. The telemetry.txt file is a regular file AND there have been enough time passed since last add.
"""
logger = logging.getLogger('telemetry.check')

telemetry_note_path = os.path.join(config_dir, TELEMETRY_NOTE_NAME)
if not os.path.exists(telemetry_note_path):
logger.info('Positive: The %s does not exist.', telemetry_note_path)
return True

file_stat = os.stat(telemetry_note_path)
if not stat.S_ISREG(file_stat.st_mode):
logger.warning('Negative: The %s is not a regular file.', telemetry_note_path)
return False

modify_time = datetime.fromtimestamp(file_stat.st_mtime)
if datetime.now() - modify_time < MANDATORY_WAIT_PERIOD:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a second thought, would it be safer to keep the existing code and let MANDATORY_WAIT_PERIOD = 0 in order to make the wait period configurable?

logger.warning('Negative: The %s was modified at %s, which in less than %f s',
telemetry_note_path, modify_time, MANDATORY_WAIT_PERIOD.total_seconds())
return False

logger.info('Returns Positive.')
return True


def save_payload(config_dir, payload):
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli-telemetry/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from codecs import open
from setuptools import setup

VERSION = "1.0.8"
VERSION = "1.1.0"

CLASSIFIERS = [
'Development Status :: 5 - Production/Stable',
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli/requirements.py3.Darwin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ asn1crypto==0.24.0
azure-appconfiguration==1.1.1
azure-batch==14.0.0
azure-cli-core==2.50.0
azure-cli-telemetry==1.0.8
azure-cli-telemetry==1.1.0
azure-cli==2.50.0
azure-common==1.1.22
azure-core==1.26.0
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli/requirements.py3.Linux.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ asn1crypto==0.24.0
azure-appconfiguration==1.1.1
azure-batch==14.0.0
azure-cli-core==2.50.0
azure-cli-telemetry==1.0.8
azure-cli-telemetry==1.1.0
azure-cli==2.50.0
azure-common==1.1.22
azure-core==1.26.0
Expand Down
2 changes: 1 addition & 1 deletion src/azure-cli/requirements.py3.windows.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ asn1crypto==0.24.0
azure-appconfiguration==1.1.1
azure-batch==14.0.0
azure-cli-core==2.50.0
azure-cli-telemetry==1.0.8
azure-cli-telemetry==1.1.0
azure-cli==2.50.0
azure-common==1.1.22
azure-core==1.26.0
Expand Down