Skip to content

Commit b11a461

Browse files
committed
fix: Update all Error raises and test calls to use host_url
Changes: 1. client.py: Changed all error raises from session_id_hex to host_url - Connection class: session_id_hex=self.get_session_id_hex() -> host_url=self.session.host - Cursor class: session_id_hex=self.connection.get_session_id_hex() -> host_url=self.connection.session.host 2. test_telemetry.py: Updated get_telemetry_client() and close() calls - get_telemetry_client(session_id) -> get_telemetry_client(host_url) - close(session_id) -> close(host_url=host_url) 3. test_telemetry_push_client.py: Changed logger.warning to logger.debug - Updated test assertion to match debug logging level These changes complete the migration from session-level to host-level telemetry client management.
1 parent ffc7b27 commit b11a461

File tree

3 files changed

+44
-44
lines changed

3 files changed

+44
-44
lines changed

src/databricks/sql/client.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def cursor(
494494
if not self.open:
495495
raise InterfaceError(
496496
"Cannot create cursor from closed connection",
497-
session_id_hex=self.get_session_id_hex(),
497+
host_url=self.session.host,
498498
)
499499

500500
cursor = Cursor(
@@ -546,7 +546,7 @@ def autocommit(self) -> bool:
546546
if not self.open:
547547
raise InterfaceError(
548548
"Cannot get autocommit on closed connection",
549-
session_id_hex=self.get_session_id_hex(),
549+
host_url=self.session.host,
550550
)
551551

552552
if self._fetch_autocommit_from_server:
@@ -578,7 +578,7 @@ def autocommit(self, value: bool) -> None:
578578
if not self.open:
579579
raise InterfaceError(
580580
"Cannot set autocommit on closed connection",
581-
session_id_hex=self.get_session_id_hex(),
581+
host_url=self.session.host,
582582
)
583583

584584
# Create internal cursor for transaction control
@@ -600,7 +600,7 @@ def autocommit(self, value: bool) -> None:
600600
"operation": "set_autocommit",
601601
"autocommit_value": value,
602602
},
603-
session_id_hex=self.get_session_id_hex(),
603+
host_url=self.session.host,
604604
) from e
605605
finally:
606606
if cursor:
@@ -627,7 +627,7 @@ def _fetch_autocommit_state_from_server(self) -> bool:
627627
raise TransactionError(
628628
"No result returned from SET AUTOCOMMIT query",
629629
context={"operation": "fetch_autocommit"},
630-
session_id_hex=self.get_session_id_hex(),
630+
host_url=self.session.host,
631631
)
632632

633633
# Parse value (first column should be "true" or "false")
@@ -647,7 +647,7 @@ def _fetch_autocommit_state_from_server(self) -> bool:
647647
raise TransactionError(
648648
f"Failed to fetch autocommit state from server: {e.message}",
649649
context={**e.context, "operation": "fetch_autocommit"},
650-
session_id_hex=self.get_session_id_hex(),
650+
host_url=self.session.host,
651651
) from e
652652
finally:
653653
if cursor:
@@ -680,7 +680,7 @@ def commit(self) -> None:
680680
if not self.open:
681681
raise InterfaceError(
682682
"Cannot commit on closed connection",
683-
session_id_hex=self.get_session_id_hex(),
683+
host_url=self.session.host,
684684
)
685685

686686
cursor = None
@@ -692,7 +692,7 @@ def commit(self) -> None:
692692
raise TransactionError(
693693
f"Failed to commit transaction: {e.message}",
694694
context={**e.context, "operation": "commit"},
695-
session_id_hex=self.get_session_id_hex(),
695+
host_url=self.session.host,
696696
) from e
697697
finally:
698698
if cursor:
@@ -725,13 +725,13 @@ def rollback(self) -> None:
725725
if self.ignore_transactions:
726726
raise NotSupportedError(
727727
"Transactions are not supported on Databricks",
728-
session_id_hex=self.get_session_id_hex(),
728+
host_url=self.session.host,
729729
)
730730

731731
if not self.open:
732732
raise InterfaceError(
733733
"Cannot rollback on closed connection",
734-
session_id_hex=self.get_session_id_hex(),
734+
host_url=self.session.host,
735735
)
736736

737737
cursor = None
@@ -743,7 +743,7 @@ def rollback(self) -> None:
743743
raise TransactionError(
744744
f"Failed to rollback transaction: {e.message}",
745745
context={**e.context, "operation": "rollback"},
746-
session_id_hex=self.get_session_id_hex(),
746+
host_url=self.session.host,
747747
) from e
748748
finally:
749749
if cursor:
@@ -767,7 +767,7 @@ def get_transaction_isolation(self) -> str:
767767
if not self.open:
768768
raise InterfaceError(
769769
"Cannot get transaction isolation on closed connection",
770-
session_id_hex=self.get_session_id_hex(),
770+
host_url=self.session.host,
771771
)
772772

773773
return TRANSACTION_ISOLATION_LEVEL_REPEATABLE_READ
@@ -793,7 +793,7 @@ def set_transaction_isolation(self, level: str) -> None:
793793
if not self.open:
794794
raise InterfaceError(
795795
"Cannot set transaction isolation on closed connection",
796-
session_id_hex=self.get_session_id_hex(),
796+
host_url=self.session.host,
797797
)
798798

799799
# Normalize and validate isolation level
@@ -805,7 +805,7 @@ def set_transaction_isolation(self, level: str) -> None:
805805
raise NotSupportedError(
806806
f"Setting transaction isolation level '{level}' is not supported. "
807807
f"Only {TRANSACTION_ISOLATION_LEVEL_REPEATABLE_READ} is supported.",
808-
session_id_hex=self.get_session_id_hex(),
808+
host_url=self.session.host,
809809
)
810810

811811

@@ -857,7 +857,7 @@ def __iter__(self):
857857
else:
858858
raise ProgrammingError(
859859
"There is no active result set",
860-
session_id_hex=self.connection.get_session_id_hex(),
860+
host_url=self.connection.session.host,
861861
)
862862

863863
def _determine_parameter_approach(
@@ -997,7 +997,7 @@ def _check_not_closed(self):
997997
if not self.open:
998998
raise InterfaceError(
999999
"Attempting operation on closed cursor",
1000-
session_id_hex=self.connection.get_session_id_hex(),
1000+
host_url=self.connection.session.host,
10011001
)
10021002

10031003
def _handle_staging_operation(
@@ -1041,7 +1041,7 @@ def _handle_staging_operation(
10411041
else:
10421042
raise ProgrammingError(
10431043
"You must provide at least one staging_allowed_local_path when initialising a connection to perform ingestion commands",
1044-
session_id_hex=self.connection.get_session_id_hex(),
1044+
host_url=self.connection.session.host,
10451045
)
10461046

10471047
abs_staging_allowed_local_paths = [
@@ -1067,7 +1067,7 @@ def _handle_staging_operation(
10671067
if not allow_operation:
10681068
raise ProgrammingError(
10691069
"Local file operations are restricted to paths within the configured staging_allowed_local_path",
1070-
session_id_hex=self.connection.get_session_id_hex(),
1070+
host_url=self.connection.session.host,
10711071
)
10721072

10731073
handler_args = {
@@ -1095,7 +1095,7 @@ def _handle_staging_operation(
10951095
raise ProgrammingError(
10961096
f"Operation {row.operation} is not supported. "
10971097
+ "Supported operations are GET, PUT, and REMOVE",
1098-
session_id_hex=self.connection.get_session_id_hex(),
1098+
host_url=self.connection.session.host,
10991099
)
11001100

11011101
@log_latency(StatementType.SQL)
@@ -1110,7 +1110,7 @@ def _handle_staging_put(
11101110
if local_file is None:
11111111
raise ProgrammingError(
11121112
"Cannot perform PUT without specifying a local_file",
1113-
session_id_hex=self.connection.get_session_id_hex(),
1113+
host_url=self.connection.session.host,
11141114
)
11151115

11161116
with open(local_file, "rb") as fh:
@@ -1135,7 +1135,7 @@ def _handle_staging_http_response(self, r):
11351135
error_text = r.data.decode() if r.data else ""
11361136
raise OperationalError(
11371137
f"Staging operation over HTTP was unsuccessful: {r.status}-{error_text}",
1138-
session_id_hex=self.connection.get_session_id_hex(),
1138+
host_url=self.connection.session.host,
11391139
)
11401140

11411141
if r.status == ACCEPTED:
@@ -1166,7 +1166,7 @@ def _handle_staging_put_stream(
11661166
if not stream:
11671167
raise ProgrammingError(
11681168
"No input stream provided for streaming operation",
1169-
session_id_hex=self.connection.get_session_id_hex(),
1169+
host_url=self.connection.session.host,
11701170
)
11711171

11721172
r = self.connection.http_client.request(
@@ -1187,7 +1187,7 @@ def _handle_staging_get(
11871187
if local_file is None:
11881188
raise ProgrammingError(
11891189
"Cannot perform GET without specifying a local_file",
1190-
session_id_hex=self.connection.get_session_id_hex(),
1190+
host_url=self.connection.session.host,
11911191
)
11921192

11931193
r = self.connection.http_client.request(
@@ -1201,7 +1201,7 @@ def _handle_staging_get(
12011201
error_text = r.data.decode() if r.data else ""
12021202
raise OperationalError(
12031203
f"Staging operation over HTTP was unsuccessful: {r.status}-{error_text}",
1204-
session_id_hex=self.connection.get_session_id_hex(),
1204+
host_url=self.connection.session.host,
12051205
)
12061206

12071207
with open(local_file, "wb") as fp:
@@ -1222,7 +1222,7 @@ def _handle_staging_remove(
12221222
error_text = r.data.decode() if r.data else ""
12231223
raise OperationalError(
12241224
f"Staging operation over HTTP was unsuccessful: {r.status}-{error_text}",
1225-
session_id_hex=self.connection.get_session_id_hex(),
1225+
host_url=self.connection.session.host,
12261226
)
12271227

12281228
@log_latency(StatementType.QUERY)
@@ -1413,7 +1413,7 @@ def get_async_execution_result(self):
14131413
else:
14141414
raise OperationalError(
14151415
f"get_execution_result failed with Operation status {operation_state}",
1416-
session_id_hex=self.connection.get_session_id_hex(),
1416+
host_url=self.connection.session.host,
14171417
)
14181418

14191419
def executemany(self, operation, seq_of_parameters):
@@ -1541,7 +1541,7 @@ def fetchall(self) -> List[Row]:
15411541
else:
15421542
raise ProgrammingError(
15431543
"There is no active result set",
1544-
session_id_hex=self.connection.get_session_id_hex(),
1544+
host_url=self.connection.session.host,
15451545
)
15461546

15471547
def fetchone(self) -> Optional[Row]:
@@ -1558,7 +1558,7 @@ def fetchone(self) -> Optional[Row]:
15581558
else:
15591559
raise ProgrammingError(
15601560
"There is no active result set",
1561-
session_id_hex=self.connection.get_session_id_hex(),
1561+
host_url=self.connection.session.host,
15621562
)
15631563

15641564
def fetchmany(self, size: int) -> List[Row]:
@@ -1583,7 +1583,7 @@ def fetchmany(self, size: int) -> List[Row]:
15831583
else:
15841584
raise ProgrammingError(
15851585
"There is no active result set",
1586-
session_id_hex=self.connection.get_session_id_hex(),
1586+
host_url=self.connection.session.host,
15871587
)
15881588

15891589
def fetchall_arrow(self) -> "pyarrow.Table":
@@ -1593,7 +1593,7 @@ def fetchall_arrow(self) -> "pyarrow.Table":
15931593
else:
15941594
raise ProgrammingError(
15951595
"There is no active result set",
1596-
session_id_hex=self.connection.get_session_id_hex(),
1596+
host_url=self.connection.session.host,
15971597
)
15981598

15991599
def fetchmany_arrow(self, size) -> "pyarrow.Table":
@@ -1603,7 +1603,7 @@ def fetchmany_arrow(self, size) -> "pyarrow.Table":
16031603
else:
16041604
raise ProgrammingError(
16051605
"There is no active result set",
1606-
session_id_hex=self.connection.get_session_id_hex(),
1606+
host_url=self.connection.session.host,
16071607
)
16081608

16091609
def cancel(self) -> None:

tests/unit/test_telemetry.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,13 @@ def test_client_lifecycle_flow(self):
249249
client_context=client_context,
250250
)
251251

252-
client = TelemetryClientFactory.get_telemetry_client(session_id_hex)
252+
client = TelemetryClientFactory.get_telemetry_client("test-host.com")
253253
assert isinstance(client, TelemetryClient)
254254
assert client._session_id_hex == session_id_hex
255255

256256
# Close client
257257
with patch.object(client, "close") as mock_close:
258-
TelemetryClientFactory.close(session_id_hex)
258+
TelemetryClientFactory.close(host_url="test-host.com")
259259
mock_close.assert_called_once()
260260

261261
# Should get NoopTelemetryClient after close
@@ -274,7 +274,7 @@ def test_disabled_telemetry_creates_noop_client(self):
274274
client_context=client_context,
275275
)
276276

277-
client = TelemetryClientFactory.get_telemetry_client(session_id_hex)
277+
client = TelemetryClientFactory.get_telemetry_client("test-host.com")
278278
assert isinstance(client, NoopTelemetryClient)
279279

280280
def test_factory_error_handling(self):
@@ -297,7 +297,7 @@ def test_factory_error_handling(self):
297297
)
298298

299299
# Should fall back to NoopTelemetryClient
300-
client = TelemetryClientFactory.get_telemetry_client(session_id)
300+
client = TelemetryClientFactory.get_telemetry_client("test-host.com")
301301
assert isinstance(client, NoopTelemetryClient)
302302

303303
def test_factory_shutdown_flow(self):
@@ -325,11 +325,11 @@ def test_factory_shutdown_flow(self):
325325
assert TelemetryClientFactory._executor is not None
326326

327327
# Close first client - factory should stay initialized
328-
TelemetryClientFactory.close(session1)
328+
TelemetryClientFactory.close(host_url="test-host.com")
329329
assert TelemetryClientFactory._initialized is True
330330

331331
# Close second client - factory should shut down
332-
TelemetryClientFactory.close(session2)
332+
TelemetryClientFactory.close(host_url="test-host.com")
333333
assert TelemetryClientFactory._initialized is False
334334
assert TelemetryClientFactory._executor is None
335335

@@ -410,7 +410,7 @@ def test_telemetry_enabled_when_flag_is_true(self, mock_http_request, MockSessio
410410

411411
assert conn.telemetry_enabled is True
412412
mock_http_request.assert_called_once()
413-
client = TelemetryClientFactory.get_telemetry_client("test-session-ff-true")
413+
client = TelemetryClientFactory.get_telemetry_client("test")
414414
assert isinstance(client, TelemetryClient)
415415

416416
@patch("databricks.sql.common.unified_http_client.UnifiedHttpClient.request")
@@ -440,7 +440,7 @@ def test_telemetry_disabled_when_flag_is_false(
440440

441441
assert conn.telemetry_enabled is False
442442
mock_http_request.assert_called_once()
443-
client = TelemetryClientFactory.get_telemetry_client("test-session-ff-false")
443+
client = TelemetryClientFactory.get_telemetry_client("test")
444444
assert isinstance(client, NoopTelemetryClient)
445445

446446
@patch("databricks.sql.common.unified_http_client.UnifiedHttpClient.request")
@@ -470,7 +470,7 @@ def test_telemetry_disabled_when_flag_request_fails(
470470

471471
assert conn.telemetry_enabled is False
472472
mock_http_request.assert_called_once()
473-
client = TelemetryClientFactory.get_telemetry_client("test-session-ff-fail")
473+
client = TelemetryClientFactory.get_telemetry_client("test")
474474
assert isinstance(client, NoopTelemetryClient)
475475

476476

tests/unit/test_telemetry_push_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ def test_rate_limit_error_logging(self):
114114
with pytest.raises(TelemetryRateLimitError):
115115
self.client.request(HttpMethod.POST, "https://test.com", {})
116116

117-
mock_logger.warning.assert_called()
118-
warning_args = mock_logger.warning.call_args[0]
119-
assert "429" in str(warning_args)
120-
assert "circuit breaker" in warning_args[0]
117+
mock_logger.debug.assert_called()
118+
debug_args = mock_logger.debug.call_args[0]
119+
assert "429" in str(debug_args)
120+
assert "circuit breaker" in debug_args[0]
121121

122122
def test_other_error_logging(self):
123123
"""Test that other errors are logged during wrapping/unwrapping."""

0 commit comments

Comments
 (0)