From ed7e3d5871c93da758c49e06469bb67baa633250 Mon Sep 17 00:00:00 2001 From: Konrad Kaim Date: Fri, 14 Nov 2025 08:30:58 +0000 Subject: [PATCH 1/3] feat: flush to clearcut synchronously if uploader script cannot be found --- src/xpk/core/telemetry.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/xpk/core/telemetry.py b/src/xpk/core/telemetry.py index a11c59d29..0a1f0b319 100644 --- a/src/xpk/core/telemetry.py +++ b/src/xpk/core/telemetry.py @@ -17,11 +17,13 @@ import platform import uuid import json +import os import time import sys import importlib import subprocess import tempfile +import requests from enum import Enum from typing import Any from dataclasses import dataclass @@ -34,7 +36,8 @@ def send_clearcut_payload(data: str, wait_to_complete: bool = False) -> None: """Sends payload to clearcut endpoint.""" try: file_path = _store_payload_in_temp_file(data) - _flush_temp_file_to_clearcut(file_path, wait_to_complete) + if not _schedule_clearcut_background_flush(file_path, wait_to_complete): + _clearcut_flush(file_path) except Exception: # pylint: disable=broad-exception-caught pass @@ -56,10 +59,13 @@ def _store_payload_in_temp_file(data: str) -> str: return file.name -def _flush_temp_file_to_clearcut( +def _schedule_clearcut_background_flush( file_path: str, wait_to_complete: bool ) -> None: with importlib.resources.path("xpk", "telemetry_uploader.py") as path: + if not os.path.exists(path): + return False + kwargs: dict[str, Any] = {} if sys.platform == "win32": kwargs["creationflags"] = ( @@ -80,6 +86,14 @@ def _flush_temp_file_to_clearcut( ) if wait_to_complete: process.wait() + return True + + +def _clearcut_flush(file_path: str) -> None: + with open(file_path, "r") as file: + kwargs = json.load(file) + requests.request(**kwargs) + os.remove(file_path) def ensure_client_id() -> str: From 15206723190913f827c9e4ae214fabb59dd644fd Mon Sep 17 00:00:00 2001 From: Konrad Kaim Date: Fri, 14 Nov 2025 08:37:36 +0000 Subject: [PATCH 2/3] fix: pylint --- src/xpk/core/telemetry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xpk/core/telemetry.py b/src/xpk/core/telemetry.py index 0a1f0b319..9bf115605 100644 --- a/src/xpk/core/telemetry.py +++ b/src/xpk/core/telemetry.py @@ -90,7 +90,7 @@ def _schedule_clearcut_background_flush( def _clearcut_flush(file_path: str) -> None: - with open(file_path, "r") as file: + with open(file_path, mode="r", encoding="utf-8") as file: kwargs = json.load(file) requests.request(**kwargs) os.remove(file_path) From 1087a99a7ed3b9f93a352c3e3e7d24044a16a119 Mon Sep 17 00:00:00 2001 From: Konrad Kaim Date: Fri, 14 Nov 2025 08:57:51 +0000 Subject: [PATCH 3/3] feat: address peer review feedback --- src/xpk/core/telemetry.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/xpk/core/telemetry.py b/src/xpk/core/telemetry.py index 9bf115605..4e57774ae 100644 --- a/src/xpk/core/telemetry.py +++ b/src/xpk/core/telemetry.py @@ -61,7 +61,16 @@ def _store_payload_in_temp_file(data: str) -> str: def _schedule_clearcut_background_flush( file_path: str, wait_to_complete: bool -) -> None: +) -> bool: + """Schedules clearcut background flush. + + Args: + file_path: path to the temporary file where the events are stored. + wait_to_complete: whenever to wait for the background script completion. + + Returns: + True if successful and False otherwise + """ with importlib.resources.path("xpk", "telemetry_uploader.py") as path: if not os.path.exists(path): return False