|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import time |
| 4 | +from functools import wraps |
| 5 | +from typing import Any, Callable, Dict, Optional |
| 6 | + |
| 7 | +from uipath.telemetry._track import flush_events, is_telemetry_enabled, track_event |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | +# Telemetry event name templates for Application Insights |
| 12 | +CLI_COMMAND_STARTED = "Cli.{command}.Start.URT" |
| 13 | +CLI_COMMAND_COMPLETED = "Cli.{command}.End.URT" |
| 14 | +CLI_COMMAND_FAILED = "Cli.{command}.Failed.URT" |
| 15 | + |
| 16 | + |
| 17 | +class CliTelemetryTracker: |
| 18 | + """Tracks CLI command execution and sends telemetry to Application Insights. |
| 19 | +
|
| 20 | + This class handles tracking of CLI command lifecycle events: |
| 21 | + - Command start events |
| 22 | + - Command completion events (success) |
| 23 | + - Command failure events (with error details) |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self) -> None: |
| 27 | + self._start_times: Dict[str, float] = {} |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def _get_event_name(command: str, status: str) -> str: |
| 31 | + return f"Cli.{command.capitalize()}.{status}.URT" |
| 32 | + |
| 33 | + def _enrich_properties(self, properties: Dict[str, Any]) -> None: |
| 34 | + """Enrich properties with common context information. |
| 35 | +
|
| 36 | + Args: |
| 37 | + properties: The properties dictionary to enrich. |
| 38 | + """ |
| 39 | + # Add UiPath context |
| 40 | + project_id = os.getenv("UIPATH_PROJECT_ID") |
| 41 | + if project_id: |
| 42 | + properties["ProjectId"] = project_id |
| 43 | + |
| 44 | + org_id = os.getenv("UIPATH_CLOUD_ORGANIZATION_ID") |
| 45 | + if org_id: |
| 46 | + properties["CloudOrganizationId"] = org_id |
| 47 | + |
| 48 | + user_id = os.getenv("UIPATH_CLOUD_USER_ID") |
| 49 | + if user_id: |
| 50 | + properties["CloudUserId"] = user_id |
| 51 | + |
| 52 | + tenant_id = os.getenv("UIPATH_TENANT_ID") |
| 53 | + if tenant_id: |
| 54 | + properties["TenantId"] = tenant_id |
| 55 | + |
| 56 | + # Add source identifier |
| 57 | + properties["Source"] = "uipath-python-cli" |
| 58 | + properties["ApplicationName"] = "UiPath.Cli" |
| 59 | + |
| 60 | + def track_command_start(self, command: str) -> None: |
| 61 | + try: |
| 62 | + self._start_times[command] = time.time() |
| 63 | + |
| 64 | + properties: Dict[str, Any] = {"Command": command} |
| 65 | + self._enrich_properties(properties) |
| 66 | + |
| 67 | + track_event(self._get_event_name(command, "Start"), properties) |
| 68 | + logger.debug(f"Tracked CLI command started: {command}") |
| 69 | + |
| 70 | + except Exception as e: |
| 71 | + logger.debug(f"Error tracking CLI command start: {e}") |
| 72 | + |
| 73 | + def track_command_end( |
| 74 | + self, |
| 75 | + command: str, |
| 76 | + duration_ms: Optional[int] = None, |
| 77 | + ) -> None: |
| 78 | + try: |
| 79 | + if duration_ms is None: |
| 80 | + start_time = self._start_times.pop(command, None) |
| 81 | + if start_time: |
| 82 | + duration_ms = int((time.time() - start_time) * 1000) |
| 83 | + |
| 84 | + properties: Dict[str, Any] = { |
| 85 | + "Command": command, |
| 86 | + "Success": True, |
| 87 | + } |
| 88 | + |
| 89 | + if duration_ms is not None: |
| 90 | + properties["DurationMs"] = duration_ms |
| 91 | + |
| 92 | + self._enrich_properties(properties) |
| 93 | + |
| 94 | + track_event(self._get_event_name(command, "End"), properties) |
| 95 | + logger.debug(f"Tracked CLI command completed: {command}") |
| 96 | + |
| 97 | + except Exception as e: |
| 98 | + logger.debug(f"Error tracking CLI command end: {e}") |
| 99 | + |
| 100 | + def track_command_failed( |
| 101 | + self, |
| 102 | + command: str, |
| 103 | + duration_ms: Optional[int] = None, |
| 104 | + exception: Optional[Exception] = None, |
| 105 | + ) -> None: |
| 106 | + try: |
| 107 | + if duration_ms is None: |
| 108 | + start_time = self._start_times.pop(command, None) |
| 109 | + if start_time: |
| 110 | + duration_ms = int((time.time() - start_time) * 1000) |
| 111 | + |
| 112 | + properties: Dict[str, Any] = { |
| 113 | + "Command": command, |
| 114 | + "Success": False, |
| 115 | + } |
| 116 | + |
| 117 | + if duration_ms is not None: |
| 118 | + properties["DurationMs"] = duration_ms |
| 119 | + |
| 120 | + if exception is not None: |
| 121 | + properties["ErrorType"] = type(exception).__name__ |
| 122 | + properties["ErrorMessage"] = str(exception)[:500] |
| 123 | + |
| 124 | + self._enrich_properties(properties) |
| 125 | + |
| 126 | + track_event(self._get_event_name(command, "Failed"), properties) |
| 127 | + logger.debug(f"Tracked CLI command failed: {command}") |
| 128 | + |
| 129 | + except Exception as e: |
| 130 | + logger.debug(f"Error tracking CLI command failed: {e}") |
| 131 | + |
| 132 | + def flush(self) -> None: |
| 133 | + """Flush any pending telemetry events.""" |
| 134 | + try: |
| 135 | + flush_events() |
| 136 | + except Exception as e: |
| 137 | + logger.debug(f"Error flushing CLI telemetry events: {e}") |
| 138 | + |
| 139 | + |
| 140 | +def track_cli_command(command: str) -> Callable[..., Any]: |
| 141 | + """Decorator to track CLI command execution. |
| 142 | +
|
| 143 | + Tracks the following events to Application Insights: |
| 144 | + - Cli.<Command>.Start.URT - when command begins |
| 145 | + - Cli.<Command>.End.URT - on successful completion |
| 146 | + - Cli.<Command>.Failed.URT - on exception |
| 147 | +
|
| 148 | + Properties tracked include: |
| 149 | + - Command: The command name |
| 150 | + - Success: Whether the command succeeded |
| 151 | + - DurationMs: Execution time in milliseconds |
| 152 | + - ErrorType: Exception type name (on failure) |
| 153 | + - ErrorMessage: Exception message (on failure, truncated to 500 chars) |
| 154 | + - ProjectId, CloudOrganizationId, etc. (if available) |
| 155 | +
|
| 156 | + Telemetry failures are silently ignored to ensure CLI execution |
| 157 | + is never blocked by telemetry issues. |
| 158 | +
|
| 159 | + Args: |
| 160 | + command: The CLI command name (e.g., "pack", "publish", "run"). |
| 161 | +
|
| 162 | + Returns: |
| 163 | + A decorator function that wraps the CLI command. |
| 164 | +
|
| 165 | + Example: |
| 166 | + @click.command() |
| 167 | + @track_cli_command("pack") |
| 168 | + def pack(root, nolock): |
| 169 | + ... |
| 170 | + """ |
| 171 | + |
| 172 | + def decorator(func: Callable[..., Any]) -> Callable[..., Any]: |
| 173 | + @wraps(func) |
| 174 | + def wrapper(*args: Any, **kwargs: Any) -> Any: |
| 175 | + if not is_telemetry_enabled(): |
| 176 | + return func(*args, **kwargs) |
| 177 | + |
| 178 | + tracker = CliTelemetryTracker() |
| 179 | + tracker.track_command_start(command) |
| 180 | + |
| 181 | + try: |
| 182 | + result = func(*args, **kwargs) |
| 183 | + tracker.track_command_end(command) |
| 184 | + return result |
| 185 | + |
| 186 | + except Exception as e: |
| 187 | + tracker.track_command_failed(command, exception=e) |
| 188 | + raise |
| 189 | + |
| 190 | + finally: |
| 191 | + tracker.flush() |
| 192 | + |
| 193 | + return wrapper |
| 194 | + |
| 195 | + return decorator |
0 commit comments