Skip to content

Commit

Permalink
Turn off Telemetry in CI. (#50)
Browse files Browse the repository at this point in the history
* Added check if CI env vars are set.

* Minor correction.

* Test fix.

* Flag to turn off telemetry in CI.

* Minor correction.

* Minor corrections.

* Check CI true.

* Added comment.
  • Loading branch information
popovaan authored Oct 18, 2023
1 parent 286176a commit 36e12b7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,23 @@ class Telemetry(metaclass=SingletonMetaClass):
no telemetry is sent until user accepts telemetry with dialog.
If enable_opt_in_dialog=False, telemetry is sent without opt-in dialog, unless user explicitly turned it off
with opt_in_out script.
:param disable_in_ci: Turn off telemetry for CI jobs.
"""
def __init__(self, app_name: str = None, app_version: str = None, tid: str = None,
backend: [str, None] = 'ga', enable_opt_in_dialog=True):
backend: [str, None] = 'ga', enable_opt_in_dialog=True, disable_in_ci=False):
# The case when instance is already configured
if app_name is None:
if not hasattr(self, 'sender') or self.sender is None:
raise RuntimeError('The first instantiation of the Telemetry should be done with the '
'application name, version and TID.')
return

self.init(app_name, app_version, tid, backend, enable_opt_in_dialog)
self.init(app_name, app_version, tid, backend, enable_opt_in_dialog, disable_in_ci)

def init(self, app_name: str = None, app_version: str = None, tid: str = None,
backend: [str, None] = 'ga', enable_opt_in_dialog=True):
backend: [str, None] = 'ga', enable_opt_in_dialog=True, disable_in_ci=False):
opt_in_checker = OptInChecker()
opt_in_check_result = opt_in_checker.check(enable_opt_in_dialog)
opt_in_check_result = opt_in_checker.check(enable_opt_in_dialog, disable_in_ci)
if enable_opt_in_dialog:
self.consent = opt_in_check_result == ConsentCheckResult.ACCEPTED
else:
Expand Down
1 change: 1 addition & 0 deletions src/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class GeneralTelemetryTest(unittest.TestCase):
def init_backend(self, test_dir, test_subdir):
self.backend = BackendRegistry.get_backend('ga4')("test_backend", "NONE")
OptInChecker.consent_file_base_dir = MagicMock(return_value=test_dir)
OptInChecker._run_in_ci = MagicMock(return_value=False)
self.cid_path = os.path.join(test_subdir, self.backend.cid_filename)
OptInChecker.consent_file_subdirectory = MagicMock(return_value=os.path.basename(test_subdir))
_ = Telemetry("a", "b", "c")
Expand Down
24 changes: 22 additions & 2 deletions src/utils/opt_in_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,31 @@ def _check_run_in_notebook():
pass
return False

def check(self, enable_opt_in_dialog):
@staticmethod
def _run_in_ci():
"""
Checks that script is executed in CI job.
:return: True if script is executed in CI job, otherwise False
"""
if "CI" in os.environ and os.environ["CI"].lower() == "true":
return True

if "TF_BUILD" in os.environ and len(os.environ["TF_BUILD"]):
return True

if "JENKINS_URL" in os.environ and len(os.environ["JENKINS_URL"]):
return True

return False

def check(self, enable_opt_in_dialog, disable_in_ci=False):
"""
Checks if user has accepted the collection of the information by checking the consent file.
:return: opt-in dialog result
:return: consent check result
"""
if disable_in_ci and self._run_in_ci():
return ConsentCheckResult.DECLINED

if not os.path.exists(self.consent_file()):
if enable_opt_in_dialog:
if not self._check_main_process():
Expand Down

0 comments on commit 36e12b7

Please sign in to comment.