44import logging
55from concurrent .futures import ThreadPoolExecutor
66from typing import Dict , Optional
7+ from databricks .sql .common .http import TelemetryHttpClient
78from databricks .sql .telemetry .models .event import (
89 TelemetryEvent ,
910 DriverSystemConfiguration ,
2526 DatabricksOAuthProvider ,
2627 ExternalAuthProvider ,
2728)
28- from requests .adapters import HTTPAdapter
29- from databricks .sql .auth .retry import DatabricksRetryPolicy , CommandType
3029import sys
3130import platform
3231import uuid
3635logger = logging .getLogger (__name__ )
3736
3837
39- class TelemetryHTTPAdapter (HTTPAdapter ):
40- """
41- Custom HTTP adapter to prepare our DatabricksRetryPolicy before each request.
42- This ensures the retry timer is started and the command type is set correctly,
43- allowing the policy to manage its state for the duration of the request retries.
44- """
45-
46- def send (self , request , ** kwargs ):
47- self .max_retries .command_type = CommandType .OTHER
48- self .max_retries .start_retry_timer ()
49- return super ().send (request , ** kwargs )
50-
51-
5238class TelemetryHelper :
5339 """Helper class for getting telemetry related information."""
5440
@@ -143,11 +129,6 @@ class TelemetryClient(BaseTelemetryClient):
143129 It uses a thread pool to handle asynchronous operations, that it gets from the TelemetryClientFactory.
144130 """
145131
146- TELEMETRY_RETRY_STOP_AFTER_ATTEMPTS_COUNT = 3
147- TELEMETRY_RETRY_DELAY_MIN = 1.0
148- TELEMETRY_RETRY_DELAY_MAX = 10.0
149- TELEMETRY_RETRY_STOP_AFTER_ATTEMPTS_DURATION = 30.0
150-
151132 # Telemetry endpoint paths
152133 TELEMETRY_AUTHENTICATED_PATH = "/telemetry-ext"
153134 TELEMETRY_UNAUTHENTICATED_PATH = "/telemetry-unauth"
@@ -173,21 +154,7 @@ def __init__(
173154 self ._driver_connection_params = None
174155 self ._host_url = host_url
175156 self ._executor = executor
176-
177- self ._telemetry_retry_policy = DatabricksRetryPolicy (
178- delay_min = self .TELEMETRY_RETRY_DELAY_MIN ,
179- delay_max = self .TELEMETRY_RETRY_DELAY_MAX ,
180- stop_after_attempts_count = self .TELEMETRY_RETRY_STOP_AFTER_ATTEMPTS_COUNT ,
181- stop_after_attempts_duration = self .TELEMETRY_RETRY_STOP_AFTER_ATTEMPTS_DURATION ,
182- delay_default = 1.0 ,
183- force_dangerous_codes = [],
184- )
185- self ._session = (
186- requests .Session ()
187- ) # TODO: Use DatabricksHttpClient instead (unify all http clients)
188- adapter = TelemetryHTTPAdapter (max_retries = self ._telemetry_retry_policy )
189- self ._session .mount ("https://" , adapter )
190- self ._session .mount ("http://" , adapter )
157+ self ._http_client = TelemetryHttpClient .get_instance ()
191158
192159 def _export_event (self , event ):
193160 """Add an event to the batch queue and flush if batch is full"""
@@ -236,7 +203,7 @@ def _send_telemetry(self, events):
236203 try :
237204 logger .debug ("Submitting telemetry request to thread pool" )
238205 future = self ._executor .submit (
239- self ._session .post ,
206+ self ._http_client .post ,
240207 url ,
241208 data = request .to_json (),
242209 headers = headers ,
@@ -341,7 +308,6 @@ def close(self):
341308 """Flush remaining events before closing"""
342309 logger .debug ("Closing TelemetryClient for connection %s" , self ._session_id_hex )
343310 self ._flush ()
344- self ._session .close ()
345311
346312
347313class TelemetryClientFactory :
@@ -463,6 +429,7 @@ def close(session_id_hex):
463429 )
464430 try :
465431 TelemetryClientFactory ._executor .shutdown (wait = True )
432+ TelemetryHttpClient .get_instance ().close ()
466433 except Exception as e :
467434 logger .debug ("Failed to shutdown thread pool executor: %s" , e )
468435 TelemetryClientFactory ._executor = None
0 commit comments