|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +import json |
| 5 | +import time |
| 6 | +import uuid |
| 7 | +import threading |
| 8 | +from enum import Enum |
| 9 | +from typing import Any, TypeVar, Callable |
| 10 | +from functools import wraps |
| 11 | + |
| 12 | +import click |
| 13 | +import httpx |
| 14 | +import machineid |
| 15 | + |
| 16 | +from together import __version__ |
| 17 | +from together.lib.utils import log_debug |
| 18 | + |
| 19 | +F = TypeVar("F", bound=Callable[..., Any]) |
| 20 | + |
| 21 | +SESSION_ID = int(str(uuid.uuid4().int)[0:13]) |
| 22 | + |
| 23 | + |
| 24 | +def is_tracking_enabled() -> bool: |
| 25 | + # Users can opt-out of tracking with the environment variable. |
| 26 | + if os.getenv("TOGETHER_TELEMETRY_DISABLED"): |
| 27 | + log_debug("Analytics tracking disabled by environment variable") |
| 28 | + return False |
| 29 | + |
| 30 | + return True |
| 31 | + |
| 32 | + |
| 33 | +class CliTrackingEvents(Enum): |
| 34 | + CommandStarted = "cli_command_started" |
| 35 | + CommandCompleted = "cli_commmand_completed" |
| 36 | + CommandFailed = "cli_command_failed" |
| 37 | + CommandUserAborted = "cli_command_user_aborted" |
| 38 | + ApiRequest = "cli_command_api_request" |
| 39 | + |
| 40 | + |
| 41 | +def track_cli(event_name: CliTrackingEvents, args: dict[str, Any]) -> None: |
| 42 | + """Track a CLI event. Non-Blocking.""" |
| 43 | + if is_tracking_enabled() == False: |
| 44 | + return |
| 45 | + |
| 46 | + def send_event() -> None: |
| 47 | + ANALYTICS_API_ENV_VAR = os.getenv("TOGETHER_TELEMETRY_API") |
| 48 | + ANALYTICS_API = ( |
| 49 | + ANALYTICS_API_ENV_VAR if ANALYTICS_API_ENV_VAR else "https://api.together.ai/api/together-cli-events" |
| 50 | + ) |
| 51 | + |
| 52 | + try: |
| 53 | + client = httpx.Client() |
| 54 | + client.post( |
| 55 | + ANALYTICS_API, |
| 56 | + headers={"content-type": "application/json", "user-agent": f"together-cli:{__version__}"}, |
| 57 | + content=json.dumps( |
| 58 | + { |
| 59 | + "event_name": event_name.value, |
| 60 | + "event_properties": { |
| 61 | + "is_ci": os.getenv("CI") is not None, |
| 62 | + **args, |
| 63 | + }, |
| 64 | + "event_options": { |
| 65 | + "time": int(time.time() * 1000), |
| 66 | + "session_id": str(SESSION_ID), |
| 67 | + "device_id": machineid.id().lower(), |
| 68 | + }, |
| 69 | + } |
| 70 | + ), |
| 71 | + ) |
| 72 | + except Exception as e: |
| 73 | + log_debug("Error sending analytics event", error=e) |
| 74 | + # No-op - this is not critical and we don't want to block the CLI |
| 75 | + pass |
| 76 | + |
| 77 | + threading.Thread(target=send_event).start() |
| 78 | + |
| 79 | + |
| 80 | +def auto_track_command(command: str) -> Callable[[F], F]: |
| 81 | + """Decorator for click commands to automatically track CLI commands start/completion/failure.""" |
| 82 | + |
| 83 | + def decorator(f: F) -> F: |
| 84 | + @wraps(f) |
| 85 | + def wrapper(*args: Any, **kwargs: Any) -> Any: |
| 86 | + track_cli(CliTrackingEvents.CommandStarted, {"command": command, "arguments": kwargs}) |
| 87 | + try: |
| 88 | + return f(*args, **kwargs) |
| 89 | + except click.Abort: |
| 90 | + # Doesn't seem like this is working any more |
| 91 | + track_cli( |
| 92 | + CliTrackingEvents.CommandUserAborted, |
| 93 | + {"command": command, "arguments": kwargs}, |
| 94 | + ) |
| 95 | + except Exception as e: |
| 96 | + track_cli(CliTrackingEvents.CommandFailed, {"command": command, "arguments": kwargs, "error": str(e)}) |
| 97 | + raise e |
| 98 | + finally: |
| 99 | + track_cli(CliTrackingEvents.CommandCompleted, {"command": command, "arguments": kwargs}) |
| 100 | + |
| 101 | + return wrapper # type: ignore |
| 102 | + |
| 103 | + return decorator # type: ignore |
0 commit comments