|
| 1 | +import json |
| 2 | +from dataclasses import dataclass, asdict |
| 3 | +from databricks.sql.telemetry.enums import ( |
| 4 | + AuthMech, |
| 5 | + AuthFlow, |
| 6 | + DatabricksClientType, |
| 7 | + DriverVolumeOperationType, |
| 8 | + StatementType, |
| 9 | + ExecutionResultFormat, |
| 10 | +) |
| 11 | +from typing import Optional |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class HostDetails: |
| 16 | + """ |
| 17 | + Represents the host connection details for a Databricks workspace. |
| 18 | +
|
| 19 | + Attributes: |
| 20 | + host_url (str): The URL of the Databricks workspace (e.g., https://my-workspace.cloud.databricks.com) |
| 21 | + port (int): The port number for the connection (typically 443 for HTTPS) |
| 22 | + """ |
| 23 | + |
| 24 | + host_url: str |
| 25 | + port: int |
| 26 | + |
| 27 | + def to_json(self): |
| 28 | + return json.dumps(asdict(self)) |
| 29 | + |
| 30 | + |
| 31 | +@dataclass |
| 32 | +class DriverConnectionParameters: |
| 33 | + """ |
| 34 | + Contains all connection parameters used to establish a connection to Databricks SQL. |
| 35 | + This includes authentication details, host information, and connection settings. |
| 36 | +
|
| 37 | + Attributes: |
| 38 | + http_path (str): The HTTP path for the SQL endpoint |
| 39 | + mode (DatabricksClientType): The type of client connection (e.g., THRIFT) |
| 40 | + host_info (HostDetails): Details about the host connection |
| 41 | + auth_mech (AuthMech): The authentication mechanism used |
| 42 | + auth_flow (AuthFlow): The authentication flow type |
| 43 | + auth_scope (str): The scope of authentication |
| 44 | + discovery_url (str): URL for service discovery |
| 45 | + allowed_volume_ingestion_paths (str): JSON string of allowed paths for volume operations |
| 46 | + azure_tenant_id (str): Azure tenant ID for Azure authentication |
| 47 | + socket_timeout (int): Connection timeout in milliseconds |
| 48 | + """ |
| 49 | + |
| 50 | + http_path: str |
| 51 | + mode: DatabricksClientType |
| 52 | + host_info: HostDetails |
| 53 | + auth_mech: AuthMech |
| 54 | + auth_flow: AuthFlow |
| 55 | + auth_scope: str |
| 56 | + discovery_url: str |
| 57 | + allowed_volume_ingestion_paths: str |
| 58 | + azure_tenant_id: str |
| 59 | + socket_timeout: int |
| 60 | + |
| 61 | + def to_json(self): |
| 62 | + return json.dumps(asdict(self)) |
| 63 | + |
| 64 | + |
| 65 | +@dataclass |
| 66 | +class DriverSystemConfiguration: |
| 67 | + """ |
| 68 | + Contains system-level configuration information about the client environment. |
| 69 | + This includes details about the operating system, runtime, and driver version. |
| 70 | +
|
| 71 | + Attributes: |
| 72 | + driver_version (str): Version of the Databricks SQL driver |
| 73 | + os_name (str): Name of the operating system |
| 74 | + os_version (str): Version of the operating system |
| 75 | + os_arch (str): Architecture of the operating system |
| 76 | + runtime_name (str): Name of the Python runtime (e.g., CPython) |
| 77 | + runtime_version (str): Version of the Python runtime |
| 78 | + runtime_vendor (str): Vendor of the Python runtime |
| 79 | + client_app_name (str): Name of the client application |
| 80 | + locale_name (str): System locale setting |
| 81 | + driver_name (str): Name of the driver |
| 82 | + char_set_encoding (str): Character set encoding used |
| 83 | + """ |
| 84 | + |
| 85 | + driver_version: str |
| 86 | + os_name: str |
| 87 | + os_version: str |
| 88 | + os_arch: str |
| 89 | + runtime_name: str |
| 90 | + runtime_version: str |
| 91 | + runtime_vendor: str |
| 92 | + client_app_name: str |
| 93 | + locale_name: str |
| 94 | + driver_name: str |
| 95 | + char_set_encoding: str |
| 96 | + |
| 97 | + def to_json(self): |
| 98 | + return json.dumps(asdict(self)) |
| 99 | + |
| 100 | + |
| 101 | +@dataclass |
| 102 | +class DriverVolumeOperation: |
| 103 | + """ |
| 104 | + Represents a volume operation performed by the driver. |
| 105 | + Used for tracking volume-related operations in telemetry. |
| 106 | +
|
| 107 | + Attributes: |
| 108 | + volume_operation_type (DriverVolumeOperationType): Type of volume operation (e.g., LIST) |
| 109 | + volume_path (str): Path to the volume being operated on |
| 110 | + """ |
| 111 | + |
| 112 | + volume_operation_type: DriverVolumeOperationType |
| 113 | + volume_path: str |
| 114 | + |
| 115 | + def to_json(self): |
| 116 | + return json.dumps(asdict(self)) |
| 117 | + |
| 118 | + |
| 119 | +@dataclass |
| 120 | +class DriverErrorInfo: |
| 121 | + """ |
| 122 | + Contains detailed information about errors that occur during driver operations. |
| 123 | + Used for error tracking and debugging in telemetry. |
| 124 | +
|
| 125 | + Attributes: |
| 126 | + error_name (str): Name/type of the error |
| 127 | + stack_trace (str): Full stack trace of the error |
| 128 | + """ |
| 129 | + |
| 130 | + error_name: str |
| 131 | + stack_trace: str |
| 132 | + |
| 133 | + def to_json(self): |
| 134 | + return json.dumps(asdict(self)) |
| 135 | + |
| 136 | + |
| 137 | +@dataclass |
| 138 | +class SqlExecutionEvent: |
| 139 | + """ |
| 140 | + Represents a SQL query execution event. |
| 141 | + Contains details about the query execution, including type, compression, and result format. |
| 142 | +
|
| 143 | + Attributes: |
| 144 | + statement_type (StatementType): Type of SQL statement |
| 145 | + is_compressed (bool): Whether the result is compressed |
| 146 | + execution_result (ExecutionResultFormat): Format of the execution result |
| 147 | + retry_count (int): Number of retry attempts made |
| 148 | + """ |
| 149 | + |
| 150 | + statement_type: StatementType |
| 151 | + is_compressed: bool |
| 152 | + execution_result: ExecutionResultFormat |
| 153 | + retry_count: int |
| 154 | + |
| 155 | + def to_json(self): |
| 156 | + return json.dumps(asdict(self)) |
| 157 | + |
| 158 | + |
| 159 | +@dataclass |
| 160 | +class TelemetryEvent: |
| 161 | + """ |
| 162 | + Main telemetry event class that aggregates all telemetry data. |
| 163 | + Contains information about the session, system configuration, connection parameters, |
| 164 | + and any operations or errors that occurred. |
| 165 | +
|
| 166 | + Attributes: |
| 167 | + session_id (str): Unique identifier for the session |
| 168 | + sql_statement_id (Optional[str]): ID of the SQL statement if applicable |
| 169 | + system_configuration (DriverSystemConfiguration): System configuration details |
| 170 | + driver_connection_params (DriverConnectionParameters): Connection parameters |
| 171 | + auth_type (Optional[str]): Type of authentication used |
| 172 | + vol_operation (Optional[DriverVolumeOperation]): Volume operation details if applicable |
| 173 | + sql_operation (Optional[SqlExecutionEvent]): SQL execution details if applicable |
| 174 | + error_info (Optional[DriverErrorInfo]): Error information if an error occurred |
| 175 | + operation_latency_ms (Optional[int]): Operation latency in milliseconds |
| 176 | + """ |
| 177 | + |
| 178 | + session_id: str |
| 179 | + sql_statement_id: Optional[str] = None |
| 180 | + system_configuration: DriverSystemConfiguration |
| 181 | + driver_connection_params: DriverConnectionParameters |
| 182 | + auth_type: Optional[str] = None |
| 183 | + vol_operation: Optional[DriverVolumeOperation] = None |
| 184 | + sql_operation: Optional[SqlExecutionEvent] = None |
| 185 | + error_info: Optional[DriverErrorInfo] = None |
| 186 | + operation_latency_ms: Optional[int] = None |
| 187 | + |
| 188 | + def to_json(self): |
| 189 | + return json.dumps(asdict(self)) |
0 commit comments