@@ -138,19 +138,18 @@ class TelemetryClient(BaseTelemetryClient):
138
138
TELEMETRY_AUTHENTICATED_PATH = "/telemetry-ext"
139
139
TELEMETRY_UNAUTHENTICATED_PATH = "/telemetry-unauth"
140
140
141
- DEFAULT_BATCH_SIZE = 100
142
-
143
141
def __init__ (
144
142
self ,
145
143
telemetry_enabled ,
146
144
session_id_hex ,
147
145
auth_provider ,
148
146
host_url ,
149
147
executor ,
148
+ batch_size ,
150
149
):
151
150
logger .debug ("Initializing TelemetryClient for connection: %s" , session_id_hex )
152
151
self ._telemetry_enabled = telemetry_enabled
153
- self ._batch_size = self . DEFAULT_BATCH_SIZE
152
+ self ._batch_size = batch_size
154
153
self ._session_id_hex = session_id_hex
155
154
self ._auth_provider = auth_provider
156
155
self ._user_agent = None
@@ -318,7 +317,7 @@ def close(self):
318
317
class TelemetryClientFactory :
319
318
"""
320
319
Static factory class for creating and managing telemetry clients.
321
- It uses a thread pool to handle asynchronous operations.
320
+ It uses a thread pool to handle asynchronous operations and a single flush thread for all clients .
322
321
"""
323
322
324
323
_clients : Dict [
@@ -331,6 +330,13 @@ class TelemetryClientFactory:
331
330
_original_excepthook = None
332
331
_excepthook_installed = False
333
332
333
+ # Shared flush thread for all clients
334
+ _flush_thread = None
335
+ _flush_event = threading .Event ()
336
+ _flush_interval_seconds = 90
337
+
338
+ DEFAULT_BATCH_SIZE = 100
339
+
334
340
@classmethod
335
341
def _initialize (cls ):
336
342
"""Initialize the factory if not already initialized"""
@@ -341,11 +347,39 @@ def _initialize(cls):
341
347
max_workers = 10
342
348
) # Thread pool for async operations
343
349
cls ._install_exception_hook ()
350
+ cls ._start_flush_thread ()
344
351
cls ._initialized = True
345
352
logger .debug (
346
353
"TelemetryClientFactory initialized with thread pool (max_workers=10)"
347
354
)
348
355
356
+ @classmethod
357
+ def _start_flush_thread (cls ):
358
+ """Start the shared background thread for periodic flushing of all clients"""
359
+ cls ._flush_event .clear ()
360
+ cls ._flush_thread = threading .Thread (target = cls ._flush_worker , daemon = True )
361
+ cls ._flush_thread .start ()
362
+
363
+ @classmethod
364
+ def _flush_worker (cls ):
365
+ """Background worker thread for periodic flushing of all clients"""
366
+ while not cls ._flush_event .wait (cls ._flush_interval_seconds ):
367
+ logger .debug ("Performing periodic flush for all telemetry clients" )
368
+
369
+ with cls ._lock :
370
+ clients_to_flush = list (cls ._clients .values ())
371
+
372
+ for client in clients_to_flush :
373
+ client ._flush ()
374
+
375
+ @classmethod
376
+ def _stop_flush_thread (cls ):
377
+ """Stop the shared background flush thread"""
378
+ if cls ._flush_thread is not None :
379
+ cls ._flush_event .set ()
380
+ cls ._flush_thread .join (timeout = 1.0 )
381
+ cls ._flush_thread = None
382
+
349
383
@classmethod
350
384
def _install_exception_hook (cls ):
351
385
"""Install global exception handler for unhandled exceptions"""
@@ -374,6 +408,7 @@ def initialize_telemetry_client(
374
408
session_id_hex ,
375
409
auth_provider ,
376
410
host_url ,
411
+ batch_size ,
377
412
):
378
413
"""Initialize a telemetry client for a specific connection if telemetry is enabled"""
379
414
try :
@@ -395,6 +430,7 @@ def initialize_telemetry_client(
395
430
auth_provider = auth_provider ,
396
431
host_url = host_url ,
397
432
executor = TelemetryClientFactory ._executor ,
433
+ batch_size = batch_size ,
398
434
)
399
435
else :
400
436
TelemetryClientFactory ._clients [
@@ -433,6 +469,7 @@ def close(session_id_hex):
433
469
"No more telemetry clients, shutting down thread pool executor"
434
470
)
435
471
try :
472
+ TelemetryClientFactory ._stop_flush_thread ()
436
473
TelemetryClientFactory ._executor .shutdown (wait = True )
437
474
TelemetryHttpClient .close ()
438
475
except Exception as e :
@@ -458,6 +495,7 @@ def connection_failure_log(
458
495
session_id_hex = UNAUTH_DUMMY_SESSION_ID ,
459
496
auth_provider = None ,
460
497
host_url = host_url ,
498
+ batch_size = TelemetryClientFactory .DEFAULT_BATCH_SIZE ,
461
499
)
462
500
463
501
telemetry_client = TelemetryClientFactory .get_telemetry_client (
0 commit comments