@@ -204,9 +204,7 @@ class _CmapTelemetry:
204204
205205 __slots__ = (
206206 "_address" ,
207- "_checkout_start" ,
208207 "_client_id" ,
209- "_conn_created_start" ,
210208 "_listeners" ,
211209 "_log" ,
212210 "_publish" ,
@@ -283,22 +281,19 @@ def pool_closed(self) -> None:
283281 self ._listeners .publish_pool_closed (self ._address )
284282
285283 def connection_created (self , conn_id : int ) -> None :
286- # Always record start time: logging or publishing may be enabled by the time
287- # connection_ready is called to compute the duration.
288- self ._conn_created_start = time .monotonic ()
289284 # Log before publishing to prevent potential listener preemption in tests.
290285 if self ._should_log :
291286 self ._emit_log (_ConnectionStatusMessage .CONN_CREATED , driverConnectionId = conn_id )
292287 if self ._should_publish :
293288 assert self ._listeners is not None
294289 self ._listeners .publish_connection_created (self ._address , conn_id )
295290
296- def connection_ready (self , conn_id : int ) -> None :
291+ def connection_ready (self , conn_id : int , creation_time : float ) -> None :
297292 should_log = self ._should_log
298293 should_publish = self ._should_publish
299294 if not should_log and not should_publish :
300295 return
301- duration = max (0.0 , time .monotonic () - self . _conn_created_start )
296+ duration = max (0.0 , time .monotonic () - creation_time )
302297 # Log before publishing to prevent potential listener preemption in tests.
303298 if should_log :
304299 self ._emit_log (
@@ -324,24 +319,22 @@ def connection_closed(self, conn_id: int, reason: str) -> None:
324319 error = reason ,
325320 )
326321
327- def checkout_started (self ) -> None :
328- should_log = self ._should_log
329- should_publish = self ._should_publish
330- # Always record start time: logging or publishing may be enabled by the time
331- # checkout_succeeded or checkout_failed is called to compute the duration.
332- self ._checkout_start = time .monotonic ()
333- if should_publish :
322+ def checkout_started (self ) -> float :
323+ """Emit the checkout started event/log and return the start time for duration tracking."""
324+ start = time .monotonic ()
325+ if self ._should_publish :
334326 assert self ._listeners is not None
335327 self ._listeners .publish_connection_check_out_started (self ._address )
336- if should_log :
328+ if self . _should_log :
337329 self ._emit_log (_ConnectionStatusMessage .CHECKOUT_STARTED )
330+ return start
338331
339- def checkout_succeeded (self , conn_id : int ) -> None :
332+ def checkout_succeeded (self , conn_id : int , start : float ) -> None :
340333 should_log = self ._should_log
341334 should_publish = self ._should_publish
342335 if not should_log and not should_publish :
343336 return
344- duration = max (0.0 , time .monotonic () - self . _checkout_start )
337+ duration = max (0.0 , time .monotonic () - start )
345338 if should_publish :
346339 assert self ._listeners is not None
347340 self ._listeners .publish_connection_checked_out (self ._address , conn_id , duration )
@@ -352,12 +345,12 @@ def checkout_succeeded(self, conn_id: int) -> None:
352345 durationMS = duration * 1000 ,
353346 )
354347
355- def checkout_failed (self , reason : str , error : str ) -> None :
348+ def checkout_failed (self , reason : str , error : str , start : float ) -> None :
356349 should_log = self ._should_log
357350 should_publish = self ._should_publish
358351 if not should_log and not should_publish :
359352 return
360- duration = max (0.0 , time .monotonic () - self . _checkout_start )
353+ duration = max (0.0 , time .monotonic () - start )
361354 if should_publish :
362355 assert self ._listeners is not None
363356 self ._listeners .publish_connection_check_out_failed (self ._address , error , duration )
@@ -632,43 +625,62 @@ def __init__(
632625 # logging level is stable for its lifetime.
633626 self ._should_log = _SERVER_SELECTION_LOGGER .isEnabledFor (logging .DEBUG )
634627
635- def _emit_log (self , message : _ServerSelectionStatusMessage , ** extra : Any ) -> None :
628+ def _emit_log (
629+ self , message : _ServerSelectionStatusMessage , topology_description : Any , ** extra : Any
630+ ) -> None :
636631 _debug_log (
637632 _SERVER_SELECTION_LOGGER ,
638633 message = message ,
639634 clientId = self ._topology_id ,
640635 selector = self ._selector ,
641636 operation = self ._operation ,
642637 operationId = self ._operation_id ,
643- topologyDescription = self . _topology_description ,
638+ topologyDescription = topology_description ,
644639 ** extra ,
645640 )
646641
647642 def started (self ) -> None :
648643 """Emit the server selection STARTED log entry."""
649644 if self ._should_log :
650- self ._emit_log (_ServerSelectionStatusMessage .STARTED )
645+ self ._emit_log (_ServerSelectionStatusMessage .STARTED , self . _topology_description )
651646
652647 def waiting (self , remaining_time_ms : int ) -> None :
653648 """Emit the server selection WAITING log entry."""
654649 if self ._should_log :
655- self ._emit_log (_ServerSelectionStatusMessage .WAITING , remainingTimeMS = remaining_time_ms )
650+ self ._emit_log (
651+ _ServerSelectionStatusMessage .WAITING ,
652+ self ._topology_description ,
653+ remainingTimeMS = remaining_time_ms ,
654+ )
656655
657- def failed (self , failure : str ) -> None :
658- """Emit the server selection FAILED log entry."""
656+ def failed (self , failure : str , topology_description : Any = None ) -> None :
657+ """Emit the server selection FAILED log entry with the current topology description ."""
659658 if self ._should_log :
660- self ._emit_log (_ServerSelectionStatusMessage .FAILED , failure = failure )
659+ self ._emit_log (
660+ _ServerSelectionStatusMessage .FAILED ,
661+ topology_description
662+ if topology_description is not None
663+ else self ._topology_description ,
664+ failure = failure ,
665+ )
661666
662667 def succeeded (self , server_host : str , server_port : Optional [int ]) -> None :
663668 """Emit the server selection SUCCEEDED log entry."""
664669 if self ._should_log :
665670 self ._emit_log (
666671 _ServerSelectionStatusMessage .SUCCEEDED ,
672+ self ._topology_description ,
667673 serverHost = server_host ,
668674 serverPort = server_port ,
669675 )
670676
671677
678+ def log_srv_monitor_failure (failure : Exception ) -> None :
679+ """Emit a log entry when the SRV monitor fails to poll DNS records."""
680+ if _SDAM_LOGGER .isEnabledFor (logging .DEBUG ):
681+ _debug_log (_SDAM_LOGGER , message = "SRV monitor check failed" , failure = repr (failure ))
682+
683+
672684def log_command_retry (
673685 topology_id : Any ,
674686 command_name : str ,
0 commit comments