Skip to content

Commit 34c05a1

Browse files
committed
telemetry cleanup
1 parent 40f9781 commit 34c05a1

1 file changed

Lines changed: 15 additions & 21 deletions

File tree

src/couchbase_haystack/telemetry.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,37 @@
88

99
import platform
1010
import threading
11+
from contextlib import suppress
12+
from importlib import import_module
13+
from importlib.metadata import version
1114

1215
SCARF_ENDPOINT_URL = "https://couchbase.gateway.scarf.sh/couchbase-haystack"
1316

14-
_telemetry_sent = False
17+
_telemetry_sent = threading.Event()
1518
_telemetry_lock = threading.Lock()
1619

20+
1721
def _get_package_version() -> str:
1822
"""Return the installed package version, or 'unknown' if unavailable."""
19-
try:
20-
from importlib.metadata import version
21-
23+
with suppress(Exception):
2224
return version("couchbase-haystack")
23-
except Exception:
24-
return "unknown"
25+
return "unknown"
26+
2527

2628
def _send_telemetry() -> None:
2729
"""Send a single telemetry event to Scarf."""
28-
global _telemetry_sent
29-
3030
with _telemetry_lock:
31-
if _telemetry_sent:
31+
if _telemetry_sent.is_set():
3232
return
33-
_telemetry_sent = True
34-
35-
try:
36-
from scarf import ScarfEventLogger
33+
_telemetry_sent.set()
3734

38-
logger = ScarfEventLogger(
35+
with suppress(Exception):
36+
event_logger = import_module("scarf").ScarfEventLogger(
3937
endpoint_url=SCARF_ENDPOINT_URL,
4038
timeout=2.0,
4139
)
4240

43-
logger.log_event(
41+
event_logger.log_event(
4442
{
4543
"package": "couchbase-haystack",
4644
"version": _get_package_version(),
@@ -49,17 +47,13 @@ def _send_telemetry() -> None:
4947
"arch": platform.machine(),
5048
}
5149
)
52-
except Exception:
53-
# Telemetry must never raise — silently ignore all errors.
54-
pass
50+
5551

5652
def send_telemetry() -> None:
5753
"""Fire-and-forget telemetry in a background daemon thread.
5854
5955
Safe to call multiple times; only the first invocation actually sends.
6056
"""
61-
try:
57+
with suppress(Exception):
6258
t = threading.Thread(target=_send_telemetry, daemon=True)
6359
t.start()
64-
except Exception:
65-
pass

0 commit comments

Comments
 (0)