ktx/python/ktx-daemon/src/ktx_daemon/telemetry/identity.py
Andrey Avtomonov b0dd13ce7c
feat(telemetry): anonymous posthog usage telemetry across node cli and python daemon (#205)
* feat: add telemetry phase 1

* feat: add node telemetry event catalog

* feat: add telemetry event helpers

* feat: emit setup and connection telemetry

* feat: emit connection and stack telemetry

* feat: emit ingest and scan telemetry

* feat: emit query telemetry

* feat: emit sampled mcp telemetry

* docs: expand telemetry event catalog

* feat: add telemetry schema sync artifact

* feat: pass telemetry project id to semantic daemon

* feat: add daemon telemetry foundation

* feat: emit semantic daemon telemetry

* feat: emit daemon lifecycle telemetry

* docs: document full telemetry event catalog

* feat(telemetry): dim first-run notice

* feat(telemetry): show first-run notice before command output

* feat(telemetry): wire ktx PostHog project for live ingestion

* docs(telemetry): drop posthog project name and host from storage section

* docs(telemetry): trim to general overview and disclaimer

* docs(agents): add short telemetry guidelines

* feat(telemetry): enable posthog geoip enrichment

* docs(telemetry): drop ip-geoip note from public overview

* refactor(telemetry): drop no-op groupIdentify, rely on capture groups field

* fix(telemetry): respect CI kill switch in python daemon identity

* fix(sql): route table-count analysis to existing analyze-batch endpoint

* fix(telemetry): emit install_first_run from notice path and derive flagsPresent from commander

* fix(telemetry): read package info via getKtxCliPackageInfo to satisfy boundary check

* fix(telemetry): make python identity env={} bypass os.environ and unset CI in tests

* fix(telemetry): unset CI kill switch in cli-program-telemetry tests
2026-05-22 18:18:47 +02:00

81 lines
2.1 KiB
Python

from __future__ import annotations
import json
import os
import time
from collections.abc import Callable
from dataclasses import dataclass
from pathlib import Path
from collections.abc import Mapping
IDENTITY_TTL_SECONDS = 60.0
@dataclass(frozen=True)
class TelemetryIdentity:
install_id: str | None
enabled: bool
path: Path
_cache: tuple[float, Path, TelemetryIdentity] | None = None
def _telemetry_path(home_dir: Path | None = None) -> Path:
return (home_dir or Path.home()) / ".ktx" / "telemetry.json"
def _env_disables(env: Mapping[str, str] | None = None) -> bool:
source = os.environ if env is None else env
return bool(
source.get("KTX_TELEMETRY_DISABLED")
or source.get("DO_NOT_TRACK")
or source.get("CI")
)
def _read_identity(path: Path) -> TelemetryIdentity:
try:
raw = json.loads(path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError):
return TelemetryIdentity(install_id=None, enabled=False, path=path)
install_id = raw.get("installId")
enabled = raw.get("enabled")
if not isinstance(install_id, str) or enabled is not True:
return TelemetryIdentity(
install_id=install_id if isinstance(install_id, str) else None,
enabled=False,
path=path,
)
return TelemetryIdentity(install_id=install_id, enabled=True, path=path)
def load_telemetry_identity(
*,
home_dir: Path | None = None,
env: Mapping[str, str] | None = None,
now: Callable[[], float] | None = None,
) -> TelemetryIdentity:
global _cache
path = _telemetry_path(home_dir)
clock = now or time.monotonic
current = float(clock())
if _cache and _cache[1] == path and current - _cache[0] < IDENTITY_TTL_SECONDS:
cached = _cache[2]
else:
cached = _read_identity(path)
_cache = (current, path, cached)
if _env_disables(env):
return TelemetryIdentity(install_id=cached.install_id, enabled=False, path=path)
return cached
def reset_identity_cache() -> None:
global _cache
_cache = None