Skip to content

Commit 1ebae57

Browse files
committed
unauth telemetry client
Signed-off-by: Sai Shree Pradhan <saishree.pradhan@databricks.com>
1 parent f69866d commit 1ebae57

File tree

3 files changed

+31
-95
lines changed

3 files changed

+31
-95
lines changed

src/databricks/sql/client.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,14 @@ def read(self) -> Optional[OAuthToken]:
250250
server_hostname, **kwargs
251251
)
252252
except Exception as e:
253-
raise AuthenticationError(
254-
message=f"Failed to create authentication provider: {str(e)}",
255-
host_url=server_hostname,
253+
TelemetryClientFactory.connection_failure_log(
254+
error_name=e.__class__.__name__,
255+
error_message=str(e),
256+
host_url=self.host,
256257
http_path=http_path,
257258
port=self.port,
258-
original_exception=e,
259-
) from e
259+
)
260+
raise e
260261

261262
self.server_telemetry_enabled = True
262263
self.client_telemetry_enabled = kwargs.get("enable_telemetry", False)
@@ -316,14 +317,15 @@ def read(self) -> Optional[OAuthToken]:
316317
session_configuration, catalog, schema
317318
)
318319
except Exception as e:
319-
raise ConnectionError(
320-
message=f"Failed to establish connection: {str(e)}",
320+
TelemetryClientFactory.connection_failure_log(
321+
error_name=e.__class__.__name__,
322+
error_message=str(e),
321323
host_url=self.host,
322324
http_path=http_path,
323325
port=self.port,
324326
user_agent=useragent_header,
325-
original_exception=e,
326-
) from e
327+
)
328+
raise e
327329

328330
self._session_handle = self._open_session_resp.sessionHandle
329331
self.protocol_version = self.get_protocol_version(self._open_session_resp)
@@ -1418,7 +1420,6 @@ def _fill_results_buffer(self):
14181420
self.results = results
14191421
self.has_more_rows = has_more_rows
14201422

1421-
@log_latency()
14221423
def _convert_columnar_table(self, table):
14231424
column_names = [c[0] for c in self.description]
14241425
ResultRow = Row(*column_names)
@@ -1431,7 +1432,6 @@ def _convert_columnar_table(self, table):
14311432

14321433
return result
14331434

1434-
@log_latency()
14351435
def _convert_arrow_table(self, table):
14361436
column_names = [c[0] for c in self.description]
14371437
ResultRow = Row(*column_names)
@@ -1474,7 +1474,6 @@ def _convert_arrow_table(self, table):
14741474
def rownumber(self):
14751475
return self._next_row_index
14761476

1477-
@log_latency()
14781477
def fetchmany_arrow(self, size: int) -> "pyarrow.Table":
14791478
"""
14801479
Fetch the next set of rows of a query result, returning a PyArrow table.
@@ -1517,7 +1516,6 @@ def merge_columnar(self, result1, result2):
15171516
]
15181517
return ColumnTable(merged_result, result1.column_names)
15191518

1520-
@log_latency()
15211519
def fetchmany_columnar(self, size: int):
15221520
"""
15231521
Fetch the next set of rows of a query result, returning a Columnar Table.
@@ -1543,7 +1541,6 @@ def fetchmany_columnar(self, size: int):
15431541

15441542
return results
15451543

1546-
@log_latency()
15471544
def fetchall_arrow(self) -> "pyarrow.Table":
15481545
"""Fetch all (remaining) rows of a query result, returning them as a PyArrow table."""
15491546
results = self.results.remaining_rows()
@@ -1570,7 +1567,6 @@ def fetchall_arrow(self) -> "pyarrow.Table":
15701567
return pyarrow.Table.from_pydict(data)
15711568
return results
15721569

1573-
@log_latency()
15741570
def fetchall_columnar(self):
15751571
"""Fetch all (remaining) rows of a query result, returning them as a Columnar table."""
15761572
results = self.results.remaining_rows()

src/databricks/sql/exc.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import json
22
import logging
33

4+
logger = logging.getLogger(__name__)
45
from databricks.sql.telemetry.telemetry_client import TelemetryClientFactory
56

6-
logger = logging.getLogger(__name__)
77

88
### PEP-249 Mandated ###
99
# https://peps.python.org/pep-0249/#exceptions
@@ -22,29 +22,10 @@ def __init__(
2222

2323
error_name = self.__class__.__name__
2424
if session_id_hex:
25-
# Normal case: we have a session, send to regular telemetry client
2625
telemetry_client = TelemetryClientFactory.get_telemetry_client(
2726
session_id_hex
2827
)
2928
telemetry_client.export_failure_log(error_name, self.message)
30-
elif (
31-
isinstance(self, (ConnectionError, AuthenticationError))
32-
and "host_url" in self.context
33-
):
34-
# Connection error case: no session but we should still send telemetry
35-
self._send_connection_error_telemetry(error_name)
36-
37-
def _send_connection_error_telemetry(self, error_name):
38-
"""Send connection error telemetry to unauthenticated endpoint"""
39-
40-
TelemetryClientFactory.send_connection_error_telemetry(
41-
error_name=error_name,
42-
error_message=self.message or str(self),
43-
host_url=self.context["host_url"],
44-
http_path=self.context.get("http_path", ""),
45-
port=self.context.get("port", 443),
46-
user_agent=self.context.get("user_agent"),
47-
)
4829

4930
def __str__(self):
5031
return self.message

src/databricks/sql/telemetry/telemetry_client.py

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -440,74 +440,33 @@ def close(session_id_hex):
440440
TelemetryClientFactory._initialized = False
441441

442442
@staticmethod
443-
def send_connection_error_telemetry(
443+
def connection_failure_log(
444444
error_name: str,
445445
error_message: str,
446446
host_url: str,
447447
http_path: str,
448-
port: int = 443,
448+
port: int,
449449
user_agent: Optional[str] = None,
450450
):
451451
"""Send error telemetry when connection creation fails, without requiring a session"""
452-
try:
453-
logger.debug("Sending connection error telemetry for host: %s", host_url)
454-
455-
# Initialize factory if needed (with proper locking)
456-
with TelemetryClientFactory._lock:
457-
TelemetryClientFactory._initialize()
458452

459-
# Create driver connection params for the failed connection
460-
driver_connection_params = DriverConnectionParameters(
461-
http_path=http_path,
462-
mode=DatabricksClientType.THRIFT,
463-
host_info=HostDetails(host_url=host_url, port=port),
464-
)
465-
466-
error_info = DriverErrorInfo(
467-
error_name=error_name, stack_trace=error_message
468-
)
453+
UNAUTH_SESSION_ID = "Unauth_session_id"
469454

470-
telemetry_frontend_log = TelemetryFrontendLog(
471-
frontend_log_event_id=str(uuid.uuid4()),
472-
context=FrontendLogContext(
473-
client_context=TelemetryClientContext(
474-
timestamp_millis=int(time.time() * 1000),
475-
user_agent=user_agent or "PyDatabricksSqlConnector",
476-
)
477-
),
478-
entry=FrontendLogEntry(
479-
sql_driver_log=TelemetryEvent(
480-
system_configuration=TelemetryHelper.get_driver_system_configuration(),
481-
driver_connection_params=driver_connection_params,
482-
error_info=error_info,
483-
)
484-
),
485-
)
486-
487-
# Send to unauthenticated endpoint since we don't have working auth
488-
request = {
489-
"uploadTime": int(time.time() * 1000),
490-
"items": [],
491-
"protoLogs": [telemetry_frontend_log.to_json()],
492-
}
493-
494-
url = f"https://{host_url}/telemetry-unauth"
495-
headers = {"Accept": "application/json", "Content-Type": "application/json"}
455+
TelemetryClientFactory.initialize_telemetry_client(
456+
telemetry_enabled=True,
457+
session_id_hex=UNAUTH_SESSION_ID,
458+
auth_provider=None,
459+
host_url=host_url,
460+
)
496461

497-
# Send synchronously for connection errors since we're probably about to exit
498-
response = requests.post(
499-
url,
500-
data=json.dumps(request),
501-
headers=headers,
502-
timeout=5,
503-
)
504-
if response.status_code == 200:
505-
logger.debug("Connection error telemetry sent successfully")
506-
else:
507-
logger.debug(
508-
"Connection error telemetry failed with status: %s",
509-
response.status_code,
510-
)
462+
telemetry_client = TelemetryClientFactory.get_telemetry_client(
463+
UNAUTH_SESSION_ID
464+
)
465+
telemetry_client._driver_connection_params = DriverConnectionParameters(
466+
http_path=http_path,
467+
mode=DatabricksClientType.THRIFT,
468+
host_info=HostDetails(host_url=host_url, port=port),
469+
)
470+
telemetry_client._user_agent = user_agent
511471

512-
except Exception as e:
513-
logger.debug("Failed to send connection error telemetry: %s", e)
472+
telemetry_client.export_failure_log(error_name, error_message)

0 commit comments

Comments
 (0)