Skip to content

Commit

Permalink
Silence errors to not interrupt the outer pipeline (#59)
Browse files Browse the repository at this point in the history
* Silence error messages in Telemetry.

* Minor corrections.

* Minor corrections.
  • Loading branch information
popovaan authored Oct 29, 2024
1 parent 4cddb7f commit 1f98695
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/backend/backend_ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def send(self, message: Message):
if self.backend_url.lower().startswith('http'):
req = request.Request(self.backend_url, data=data)
else:
raise ValueError("Incorrect backend URL.")
log.info("Incorrect backend URL.")
return

request.urlopen(req) #nosec
except Exception as err:
Expand Down
5 changes: 4 additions & 1 deletion src/backend/backend_ga4.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import json
import uuid
import logging as log

from copy import copy
from urllib import request

Expand Down Expand Up @@ -39,7 +41,8 @@ def send(self, message: dict):
if self.backend_url.lower().startswith('http'):
req = request.Request(self.backend_url, data=data)
else:
raise ValueError("Incorrect backend URL.")
log.info("Incorrect backend URL.")
return

request.urlopen(req) # nosec
except Exception as err:
Expand Down
1 change: 1 addition & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ def _update_opt_in_status(tid: str, new_opt_in_status: bool):
else:
updated = opt_in_checker.update_result(ConsentCheckResult.DECLINED)
if not updated:
print("Could not update the consent file. No telemetry will be sent.")
return

telemetry = Telemetry(tid=tid, app_name=app_name, app_version=app_version, backend='ga4')
Expand Down
37 changes: 24 additions & 13 deletions src/utils/opt_in_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ def consent_file_base_dir():
dir_to_check = Path.home()

if dir_to_check is None:
raise Exception('Failed to find location of the openvino_telemetry file.')
log.info('Failed to find location of the openvino_telemetry file.')
return None

consent_base_dir = os.path.expandvars(dir_to_check)
if not os.path.isdir(consent_base_dir):
raise Exception('Failed to find location of the openvino_telemetry file.')
log.info('Failed to find location of the openvino_telemetry file.')
return None

return consent_base_dir

Expand All @@ -116,7 +118,8 @@ def consent_file_subdirectory():
return 'Intel Corporation'
elif platform in ['Linux', 'Darwin']:
return 'intel'
raise Exception('Failed to find location of the openvino_telemetry file.')
log.info('Failed to find location of the openvino_telemetry file.')
return None

def consent_file(self):
"""
Expand Down Expand Up @@ -144,6 +147,9 @@ def create_or_check_consent_dir(self):
:return: True if the directory is created and writable, otherwise False
"""
base_dir = self.consent_file_base_dir()
consent_file_subdirectory = self.consent_file_subdirectory()
if base_dir is None or consent_file_subdirectory is None:
return False
base_is_dir = os.path.isdir(base_dir)
base_dir_exists = os.path.exists(base_dir)
base_w_access = os.access(base_dir, os.W_OK)
Expand All @@ -155,33 +161,33 @@ def create_or_check_consent_dir(self):
"Please allow write access to the following directory: {}".format(base_dir))
return False

consect_file_dir = os.path.join(self.consent_file_base_dir(), self.consent_file_subdirectory())
consent_file_is_dir = os.path.isdir(consect_file_dir)
consent_file_dir_exists = os.path.exists(consect_file_dir)
consent_file_dir = os.path.join(base_dir, consent_file_subdirectory)
consent_file_is_dir = os.path.isdir(consent_file_dir)
consent_file_dir_exists = os.path.exists(consent_file_dir)

# If consent path exists and it is not directory, we try to remove it
if consent_file_dir_exists and not consent_file_is_dir:
try:
os.remove(consect_file_dir)
os.remove(consent_file_dir)
except:
log.warning("Unable to create directory for openvino_telemetry file, as {} is invalid directory.".format(consect_file_dir))
log.warning("Unable to create directory for openvino_telemetry file, as {} is invalid directory.".format(consent_file_dir))
return False

if not os.path.exists(consect_file_dir):
if not os.path.exists(consent_file_dir):
try:
os.mkdir(consect_file_dir)
os.mkdir(consent_file_dir)

# check that directory is created
if not os.path.exists(consect_file_dir):
if not os.path.exists(consent_file_dir):
return False
except Exception as e:
log.warning("Failed to create directory for openvino_telemetry file: {}".format(str(e)))
return False

consent_file_w_access = os.access(consect_file_dir, os.W_OK)
consent_file_w_access = os.access(consent_file_dir, os.W_OK)
if not consent_file_w_access:
log.warning("Failed to create openvino_telemetry file. "
"Please allow write access to the following directory: {}".format(consect_file_dir))
"Please allow write access to the following directory: {}".format(consent_file_dir))
return False
return True

Expand All @@ -191,6 +197,8 @@ def update_result(self, result: ConsentCheckResult):
:param result: opt-in dialog result.
:return: False if the consent file is not writable, otherwise True
"""
if self.consent_file_base_dir() is None or self.consent_file_subdirectory() is None:
return False
if not os.path.exists(self.consent_file()):
if not self.create_new_consent_file():
return False
Expand Down Expand Up @@ -297,6 +305,9 @@ 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: consent check result
"""
if self.consent_file_base_dir() is None or self.consent_file_subdirectory() is None:
return ConsentCheckResult.DECLINED

if disable_in_ci and self._run_in_ci():
return ConsentCheckResult.DECLINED

Expand Down

0 comments on commit 1f98695

Please sign in to comment.