Skip to content

Commit e448aa3

Browse files
committed
removed redundant try/catch blocks, added try/catch block to initialize and get telemetry client
Signed-off-by: Sai Shree Pradhan <saishree.pradhan@databricks.com>
1 parent 2522751 commit e448aa3

File tree

2 files changed

+59
-59
lines changed

2 files changed

+59
-59
lines changed

src/databricks/sql/exc.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@ def __init__(
2323

2424
error_name = self.__class__.__name__
2525
if self.connection_uuid:
26-
try:
27-
telemetry_client = TelemetryClientFactory.get_telemetry_client(
28-
self.connection_uuid
29-
)
30-
telemetry_client.export_failure_log(error_name, self.message)
31-
except Exception as telemetry_error:
32-
logger.debug(f"Failed to send error to telemetry: {telemetry_error}")
26+
telemetry_client = TelemetryClientFactory.get_telemetry_client(
27+
self.connection_uuid
28+
)
29+
telemetry_client.export_failure_log(error_name, self.message)
3330

3431
def __str__(self):
3532
return self.message

src/databricks/sql/telemetry/telemetry_client.py

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,9 @@ def close(self):
301301
logger.debug("Closing TelemetryClient for connection %s", self._connection_uuid)
302302
try:
303303
self.flush()
304-
except Exception as e:
305-
logger.debug("Failed to flush telemetry during close: %s", e)
306-
307-
try:
308304
TelemetryClientFactory.close(self._connection_uuid)
309305
except Exception as e:
310-
logger.debug(
311-
"Failed to remove telemetry client from telemetry clientfactory: %s", e
312-
)
306+
logger.debug("Failed to close telemetry client: %s", e)
313307

314308

315309
class TelemetryClientFactory:
@@ -358,31 +352,27 @@ def _handle_unhandled_exception(cls, exc_type, exc_value, exc_traceback):
358352
"""Handle unhandled exceptions by sending telemetry and flushing thread pool"""
359353
logger.debug("Handling unhandled exception: %s", exc_type.__name__)
360354

361-
try:
362-
# Flush existing thread pool work and wait for completion
363-
logger.debug(
364-
"Flushing pending telemetry and waiting for thread pool completion..."
365-
)
366-
for uuid, client in cls._clients.items():
367-
if hasattr(client, "flush"):
368-
try:
369-
client.flush() # Submit any pending events
370-
except Exception as e:
371-
logger.debug(
372-
"Failed to flush telemetry for connection %s: %s", uuid, e
373-
)
374-
375-
if cls._executor:
355+
# Flush existing thread pool work and wait for completion
356+
logger.debug(
357+
"Flushing pending telemetry and waiting for thread pool completion..."
358+
)
359+
for uuid, client in cls._clients.items():
360+
if hasattr(client, "flush"):
376361
try:
377-
cls._executor.shutdown(
378-
wait=True
379-
) # This waits for all submitted work to complete
380-
logger.debug("Thread pool shutdown completed successfully")
362+
client.flush() # Submit any pending events
381363
except Exception as e:
382-
logger.debug("Thread pool shutdown failed: %s", e)
364+
logger.debug(
365+
"Failed to flush telemetry for connection %s: %s", uuid, e
366+
)
383367

384-
except Exception as e:
385-
logger.debug("Exception in excepthook telemetry handler: %s", e)
368+
if cls._executor:
369+
try:
370+
cls._executor.shutdown(
371+
wait=True
372+
) # This waits for all submitted work to complete
373+
logger.debug("Thread pool shutdown completed successfully")
374+
except Exception as e:
375+
logger.debug("Thread pool shutdown failed: %s", e)
386376

387377
# Call the original exception handler to maintain normal behavior
388378
if cls._original_excepthook:
@@ -396,35 +386,48 @@ def initialize_telemetry_client(
396386
host_url,
397387
):
398388
"""Initialize a telemetry client for a specific connection if telemetry is enabled"""
399-
TelemetryClientFactory._initialize()
389+
try:
390+
TelemetryClientFactory._initialize()
400391

401-
with TelemetryClientFactory._lock:
402-
if connection_uuid not in TelemetryClientFactory._clients:
403-
logger.debug(
404-
"Creating new TelemetryClient for connection %s", connection_uuid
405-
)
406-
if telemetry_enabled:
407-
TelemetryClientFactory._clients[connection_uuid] = TelemetryClient(
408-
telemetry_enabled=telemetry_enabled,
409-
connection_uuid=connection_uuid,
410-
auth_provider=auth_provider,
411-
host_url=host_url,
412-
executor=TelemetryClientFactory._executor,
392+
with TelemetryClientFactory._lock:
393+
if connection_uuid not in TelemetryClientFactory._clients:
394+
logger.debug(
395+
"Creating new TelemetryClient for connection %s",
396+
connection_uuid,
413397
)
414-
else:
415-
TelemetryClientFactory._clients[
416-
connection_uuid
417-
] = NoopTelemetryClient()
398+
if telemetry_enabled:
399+
TelemetryClientFactory._clients[
400+
connection_uuid
401+
] = TelemetryClient(
402+
telemetry_enabled=telemetry_enabled,
403+
connection_uuid=connection_uuid,
404+
auth_provider=auth_provider,
405+
host_url=host_url,
406+
executor=TelemetryClientFactory._executor,
407+
)
408+
else:
409+
TelemetryClientFactory._clients[
410+
connection_uuid
411+
] = NoopTelemetryClient()
412+
except Exception as e:
413+
logger.debug("Failed to initialize telemetry client: %s", e)
414+
# Fallback to NoopTelemetryClient to ensure connection doesn't fail
415+
TelemetryClientFactory._clients[connection_uuid] = NoopTelemetryClient()
418416

419417
@staticmethod
420418
def get_telemetry_client(connection_uuid):
421419
"""Get the telemetry client for a specific connection"""
422-
if connection_uuid in TelemetryClientFactory._clients:
423-
return TelemetryClientFactory._clients[connection_uuid]
424-
else:
425-
logger.error(
426-
"Telemetry client not initialized for connection %s", connection_uuid
427-
)
420+
try:
421+
if connection_uuid in TelemetryClientFactory._clients:
422+
return TelemetryClientFactory._clients[connection_uuid]
423+
else:
424+
logger.error(
425+
"Telemetry client not initialized for connection %s",
426+
connection_uuid,
427+
)
428+
return NoopTelemetryClient()
429+
except Exception as e:
430+
logger.debug("Failed to get telemetry client: %s", e)
428431
return NoopTelemetryClient()
429432

430433
@staticmethod

0 commit comments

Comments
 (0)