|
| 1 | +"""Databricks Labs telemetry for CoDA. |
| 2 | +
|
| 3 | +Follows the DQX pattern: piggybacks telemetry on the Databricks SDK's |
| 4 | +User-Agent header. Each log_telemetry() call creates a throwaway |
| 5 | +WorkspaceClient, augments the User-Agent with key-value data, and fires |
| 6 | +clusters.select_spark_version() to transmit the header to Databricks |
| 7 | +servers where it's recorded. |
| 8 | +
|
| 9 | +All telemetry runs in background daemon threads -- never blocks the |
| 10 | +Flask request path or terminal I/O. |
| 11 | +
|
| 12 | +Reference: https://github.com/databrickslabs/dqx/blob/main/src/databricks/labs/dqx/telemetry.py |
| 13 | +""" |
| 14 | + |
| 15 | +import functools |
| 16 | +import logging |
| 17 | +import os |
| 18 | +import threading |
| 19 | + |
| 20 | +import tomllib |
| 21 | + |
| 22 | +logger = logging.getLogger(__name__) |
| 23 | + |
| 24 | +_version_cache = None |
| 25 | + |
| 26 | + |
| 27 | +def _get_version(): |
| 28 | + """Get CoDA version from pyproject.toml (cached after first call).""" |
| 29 | + global _version_cache |
| 30 | + if _version_cache is not None: |
| 31 | + return _version_cache |
| 32 | + try: |
| 33 | + pyproject = os.path.join(os.path.dirname(__file__), "pyproject.toml") |
| 34 | + with open(pyproject, "rb") as f: |
| 35 | + _version_cache = tomllib.load(f)["project"]["version"] |
| 36 | + except Exception: |
| 37 | + _version_cache = "0.0.0" |
| 38 | + return _version_cache |
| 39 | + |
| 40 | + |
| 41 | +def set_product_info(ws): |
| 42 | + """Set CoDA product info on a WorkspaceClient for telemetry attribution. |
| 43 | +
|
| 44 | + Call this on any WorkspaceClient so all SDK API calls carry the 'coda' |
| 45 | + product identifier in the User-Agent header. |
| 46 | + """ |
| 47 | + product_info = getattr(ws.config, "_product_info", None) |
| 48 | + if product_info is None or product_info[0] != "coda": |
| 49 | + setattr(ws.config, "_product_info", ("coda", _get_version())) |
| 50 | + |
| 51 | + |
| 52 | +def log_telemetry(key, value): |
| 53 | + """Send a telemetry key-value pair via the Databricks SDK User-Agent header. |
| 54 | +
|
| 55 | + Creates a throwaway WorkspaceClient from ~/.databrickscfg, adds the |
| 56 | + key-value to the User-Agent, and fires clusters.select_spark_version() |
| 57 | + to transmit. Runs in a background daemon thread. Errors are caught and |
| 58 | + logged, never raised. |
| 59 | + """ |
| 60 | + |
| 61 | + def _send(): |
| 62 | + try: |
| 63 | + from databricks.sdk import WorkspaceClient |
| 64 | + from databricks.sdk.errors import DatabricksError |
| 65 | + |
| 66 | + ws = WorkspaceClient() |
| 67 | + set_product_info(ws) |
| 68 | + new_config = ws.config.copy().with_user_agent_extra(key, value) |
| 69 | + temp_ws = WorkspaceClient(config=new_config) |
| 70 | + try: |
| 71 | + temp_ws.clusters.select_spark_version() |
| 72 | + except DatabricksError as e: |
| 73 | + logger.debug(f"Telemetry transmit failed: {e}") |
| 74 | + except Exception as e: |
| 75 | + logger.debug(f"Telemetry error ({key}={value}): {e}") |
| 76 | + |
| 77 | + threading.Thread(target=_send, daemon=True, name=f"telemetry-{key}").start() |
| 78 | + |
| 79 | + |
| 80 | +def telemetry_logger(key, value): |
| 81 | + """Decorator that fires telemetry before executing the wrapped function. |
| 82 | +
|
| 83 | + Works on standalone functions and class methods alike. Creates its own |
| 84 | + WorkspaceClient from ~/.databrickscfg -- no self.ws required. |
| 85 | + """ |
| 86 | + |
| 87 | + def decorator(func): |
| 88 | + @functools.wraps(func) |
| 89 | + def wrapper(*args, **kwargs): |
| 90 | + try: |
| 91 | + log_telemetry(key, value) |
| 92 | + except Exception: |
| 93 | + pass # Telemetry must never break the wrapped function |
| 94 | + return func(*args, **kwargs) |
| 95 | + |
| 96 | + return wrapper |
| 97 | + |
| 98 | + return decorator |
0 commit comments