diff --git a/kedro-telemetry/kedro_telemetry/plugin.py b/kedro-telemetry/kedro_telemetry/plugin.py index 5164b5ecd..4c437cfbb 100644 --- a/kedro-telemetry/kedro_telemetry/plugin.py +++ b/kedro-telemetry/kedro_telemetry/plugin.py @@ -56,31 +56,35 @@ def _get_or_create_uuid(): """ Reads a UUID from a configuration file or generates and saves a new one if not present. """ - - config_path = user_config_dir("kedro") - full_path = os.path.join(config_path, CONFIG_FILENAME) - config = ConfigParser() - - if os.path.exists(full_path): - config.read(full_path) - - if config.has_section("telemetry") and "uuid" in config["telemetry"]: - try: - return uuid.UUID(config["telemetry"]["uuid"]).hex - except ValueError: - pass # Invalid UUID found, will generate a new one - - # Generate a new UUID and save it to the config file - if not config.has_section("telemetry"): - config.add_section("telemetry") - new_uuid = uuid.uuid4().hex - config.set("telemetry", "uuid", new_uuid) - - os.makedirs(config_path, exist_ok=True) - with open(full_path, "w") as configfile: - config.write(configfile) - - return new_uuid + try: + config_path = user_config_dir("kedro") + full_path = os.path.join(config_path, CONFIG_FILENAME) + config = ConfigParser() + + if os.path.exists(full_path): + config.read(full_path) + + if config.has_section("telemetry") and "uuid" in config["telemetry"]: + try: + return uuid.UUID(config["telemetry"]["uuid"]).hex + except ValueError: + pass # Invalid UUID found, will generate a new one + + # Generate a new UUID and save it to the config file + if not config.has_section("telemetry"): + config.add_section("telemetry") + new_uuid = uuid.uuid4().hex + config.set("telemetry", "uuid", new_uuid) + + os.makedirs(config_path, exist_ok=True) + with open(full_path, "w") as configfile: + config.write(configfile) + + return new_uuid + + except Exception as e: + logging.error(f"Failed to get or create UUID: {e}") + return "" class KedroTelemetryCLIHooks: diff --git a/kedro-telemetry/tests/test_plugin.py b/kedro-telemetry/tests/test_plugin.py index 15a33cd01..9d57c13f5 100644 --- a/kedro-telemetry/tests/test_plugin.py +++ b/kedro-telemetry/tests/test_plugin.py @@ -226,13 +226,17 @@ def test_before_command_run_empty_args(self, mocker, fake_metadata): mocked_anon_id = mocker.patch("kedro_telemetry.plugin._hash") mocked_anon_id.return_value = "digested" mocker.patch("kedro_telemetry.plugin.PACKAGE_NAME", "spaceflights") + mocker.patch( + "kedro_telemetry.plugin._get_or_create_uuid", + return_value="hashed_username", + ) mocked_heap_call = mocker.patch("kedro_telemetry.plugin._send_heap_event") telemetry_hook = KedroTelemetryCLIHooks() command_args = [] telemetry_hook.before_command_run(fake_metadata, command_args) expected_properties = { - "username": "digested", + "username": "hashed_username", "package_name": "digested", "project_version": kedro_version, "telemetry_version": TELEMETRY_VERSION, @@ -249,12 +253,12 @@ def test_before_command_run_empty_args(self, mocker, fake_metadata): expected_calls = [ mocker.call( event_name="Command run: kedro", - identity="digested", + identity="hashed_username", properties=expected_properties, ), mocker.call( event_name="CLI command", - identity="digested", + identity="hashed_username", properties=generic_properties, ), ] @@ -296,7 +300,7 @@ def test_before_command_run_anonymous(self, mocker, fake_metadata): mocked_anon_id = mocker.patch("kedro_telemetry.plugin._hash") mocked_anon_id.return_value = "digested" mocker.patch("kedro_telemetry.plugin.PACKAGE_NAME", "spaceflights") - mocker.patch("getpass.getuser", side_effect=Exception) + mocker.patch("kedro_telemetry.plugin.user_config_dir", side_effect=Exception) mocked_heap_call = mocker.patch("kedro_telemetry.plugin._send_heap_event") telemetry_hook = KedroTelemetryCLIHooks()