1+ """
2+ Package-level telemetry for couchbase-haystack using Scarf.
3+
4+ Sends a single, non-blocking telemetry ping when the package is first imported.
5+ Respects DO_NOT_TRACK and SCARF_NO_ANALYTICS environment variables (handled by the Scarf SDK).
6+ All errors are silently suppressed — telemetry must never interrupt the user.
7+ """
8+
9+ import platform
10+ import threading
11+
12+ SCARF_ENDPOINT_URL = "https://couchbase.gateway.scarf.sh/couchbase-haystack"
13+
14+ _telemetry_sent = False
15+ _telemetry_lock = threading .Lock ()
16+
17+ def _get_package_version () -> str :
18+ """Return the installed package version, or 'unknown' if unavailable."""
19+ try :
20+ from importlib .metadata import version
21+
22+ return version ("couchbase-haystack" )
23+ except Exception :
24+ return "unknown"
25+
26+ def _send_telemetry () -> None :
27+ """Send a single telemetry event to Scarf."""
28+ global _telemetry_sent
29+
30+ with _telemetry_lock :
31+ if _telemetry_sent :
32+ return
33+ _telemetry_sent = True
34+
35+ try :
36+ from scarf import ScarfEventLogger
37+
38+ logger = ScarfEventLogger (
39+ endpoint_url = SCARF_ENDPOINT_URL ,
40+ timeout = 2.0 ,
41+ )
42+
43+ logger .log_event (
44+ {
45+ "package" : "couchbase-haystack" ,
46+ "version" : _get_package_version (),
47+ "python_version" : platform .python_version (),
48+ "os" : platform .system (),
49+ "arch" : platform .machine (),
50+ }
51+ )
52+ except Exception :
53+ # Telemetry must never raise — silently ignore all errors.
54+ pass
55+
56+ def send_telemetry () -> None :
57+ """Fire-and-forget telemetry in a background daemon thread.
58+
59+ Safe to call multiple times; only the first invocation actually sends.
60+ """
61+ try :
62+ t = threading .Thread (target = _send_telemetry , daemon = True )
63+ t .start ()
64+ except Exception :
65+ pass
0 commit comments