Skip to content

Commit 265971c

Browse files
committed
PYTHON-5846 Refine _CmapTelemetry and _HeartbeatTelemetry API
- Rename _CmapTelemetry._log -> _emit_log for consistency with _CommandTelemetry - Rename _HeartbeatTelemetry.apm_started -> started to match started/succeeded/failed lifecycle - Rename _HeartbeatTelemetry.log_started -> emit_started_log to signal it is the deferred log-only half - Replace is_sdam flag with separate publish/log bool parameters on _CmapTelemetry
1 parent 8c0f314 commit 265971c

5 files changed

Lines changed: 28 additions & 23 deletions

File tree

pymongo/_telemetry.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,16 @@ def __init__(
205205
client_id: Optional[ObjectId],
206206
address: _Address,
207207
listeners: Optional[_EventListeners],
208-
is_sdam: bool,
208+
publish: bool,
209+
log: bool,
209210
) -> None:
210211
self._client_id = client_id
211212
self._address = address
212213
self._listeners = listeners
213-
self._should_publish = not is_sdam and listeners is not None and listeners.enabled_for_cmap
214-
self._should_log = not is_sdam
214+
self._should_publish = publish and listeners is not None and listeners.enabled_for_cmap
215+
self._should_log = log
215216

216-
def _log(self, message: _ConnectionStatusMessage, **extra: Any) -> None:
217+
def _emit_log(self, message: _ConnectionStatusMessage, **extra: Any) -> None:
217218
if self._should_log and _CONNECTION_LOGGER.isEnabledFor(logging.DEBUG):
218219
_debug_log(
219220
_CONNECTION_LOGGER,
@@ -226,21 +227,21 @@ def _log(self, message: _ConnectionStatusMessage, **extra: Any) -> None:
226227

227228
def pool_created(self, non_default_options: dict[str, Any]) -> None:
228229
# Log before publishing to prevent potential listener preemption in tests.
229-
self._log(_ConnectionStatusMessage.POOL_CREATED, **non_default_options)
230+
self._emit_log(_ConnectionStatusMessage.POOL_CREATED, **non_default_options)
230231
if self._should_publish:
231232
assert self._listeners is not None
232233
self._listeners.publish_pool_created(self._address, non_default_options)
233234

234235
def pool_ready(self) -> None:
235236
# Log before publishing to prevent potential listener preemption in tests.
236-
self._log(_ConnectionStatusMessage.POOL_READY)
237+
self._emit_log(_ConnectionStatusMessage.POOL_READY)
237238
if self._should_publish:
238239
assert self._listeners is not None
239240
self._listeners.publish_pool_ready(self._address)
240241

241242
def pool_cleared(self, service_id: Optional[ObjectId], interrupt_connections: bool) -> None:
242243
# Log before publishing to prevent potential listener preemption in tests.
243-
self._log(_ConnectionStatusMessage.POOL_CLEARED, serviceId=service_id)
244+
self._emit_log(_ConnectionStatusMessage.POOL_CLEARED, serviceId=service_id)
244245
if self._should_publish:
245246
assert self._listeners is not None
246247
self._listeners.publish_pool_cleared(
@@ -251,21 +252,21 @@ def pool_cleared(self, service_id: Optional[ObjectId], interrupt_connections: bo
251252

252253
def pool_closed(self) -> None:
253254
# Log before publishing to prevent potential listener preemption in tests.
254-
self._log(_ConnectionStatusMessage.POOL_CLOSED)
255+
self._emit_log(_ConnectionStatusMessage.POOL_CLOSED)
255256
if self._should_publish:
256257
assert self._listeners is not None
257258
self._listeners.publish_pool_closed(self._address)
258259

259260
def connection_created(self, conn_id: int) -> None:
260261
# Log before publishing to prevent potential listener preemption in tests.
261-
self._log(_ConnectionStatusMessage.CONN_CREATED, driverConnectionId=conn_id)
262+
self._emit_log(_ConnectionStatusMessage.CONN_CREATED, driverConnectionId=conn_id)
262263
if self._should_publish:
263264
assert self._listeners is not None
264265
self._listeners.publish_connection_created(self._address, conn_id)
265266

266267
def connection_ready(self, conn_id: int, duration: float) -> None:
267268
# Log before publishing to prevent potential listener preemption in tests.
268-
self._log(
269+
self._emit_log(
269270
_ConnectionStatusMessage.CONN_READY,
270271
driverConnectionId=conn_id,
271272
durationMS=duration,
@@ -278,7 +279,7 @@ def connection_closed(self, conn_id: int, reason: str) -> None:
278279
if self._should_publish:
279280
assert self._listeners is not None
280281
self._listeners.publish_connection_closed(self._address, conn_id, reason)
281-
self._log(
282+
self._emit_log(
282283
_ConnectionStatusMessage.CONN_CLOSED,
283284
driverConnectionId=conn_id,
284285
reason=_verbose_connection_error_reason(reason),
@@ -289,13 +290,13 @@ def checkout_started(self) -> None:
289290
if self._should_publish:
290291
assert self._listeners is not None
291292
self._listeners.publish_connection_check_out_started(self._address)
292-
self._log(_ConnectionStatusMessage.CHECKOUT_STARTED)
293+
self._emit_log(_ConnectionStatusMessage.CHECKOUT_STARTED)
293294

294295
def checkout_succeeded(self, conn_id: int, duration: float) -> None:
295296
if self._should_publish:
296297
assert self._listeners is not None
297298
self._listeners.publish_connection_checked_out(self._address, conn_id, duration)
298-
self._log(
299+
self._emit_log(
299300
_ConnectionStatusMessage.CHECKOUT_SUCCEEDED,
300301
driverConnectionId=conn_id,
301302
durationMS=duration,
@@ -305,7 +306,7 @@ def checkout_failed(self, reason: str, error: str, duration: float) -> None:
305306
if self._should_publish:
306307
assert self._listeners is not None
307308
self._listeners.publish_connection_check_out_failed(self._address, error, duration)
308-
self._log(
309+
self._emit_log(
309310
_ConnectionStatusMessage.CHECKOUT_FAILED,
310311
reason=reason,
311312
error=error,
@@ -316,7 +317,7 @@ def checked_in(self, conn_id: int) -> None:
316317
if self._should_publish:
317318
assert self._listeners is not None
318319
self._listeners.publish_connection_checked_in(self._address, conn_id)
319-
self._log(_ConnectionStatusMessage.CHECKEDIN, driverConnectionId=conn_id)
320+
self._emit_log(_ConnectionStatusMessage.CHECKEDIN, driverConnectionId=conn_id)
320321

321322

322323
class _HeartbeatTelemetry:
@@ -344,13 +345,13 @@ def __init__(
344345
self._publish = publish
345346
self._awaited = awaited
346347

347-
def apm_started(self) -> None:
348+
def started(self) -> None:
348349
"""Publish the APM heartbeat-started event (before connection checkout)."""
349350
if self._publish:
350351
assert self._listeners is not None
351352
self._listeners.publish_server_heartbeat_started(self._address, self._awaited)
352353

353-
def log_started(self, conn_id: int, server_conn_id: Optional[int]) -> None:
354+
def emit_started_log(self, conn_id: int, server_conn_id: Optional[int]) -> None:
354355
"""Emit the log entry for heartbeat started (after connection checkout)."""
355356
if _SDAM_LOGGER.isEnabledFor(logging.DEBUG):
356357
_debug_log(

pymongo/asynchronous/monitor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,12 @@ async def _check_once(self) -> ServerDescription:
294294
self._topology._topology_id, address, self._listeners, self._publish, awaited
295295
)
296296
self._current_hb = hb
297-
hb.apm_started()
297+
hb.started()
298298

299299
if self._cancel_context and self._cancel_context.cancelled:
300300
await self._reset_connection()
301301
async with self._pool.checkout() as conn:
302-
hb.log_started(conn.id, conn.server_connection_id)
302+
hb.emit_started_log(conn.id, conn.server_connection_id)
303303
self._cancel_context = conn.cancel_context
304304
# Record the connection id so we can later attach it to the failed log message.
305305
self._conn_id = conn.id

pymongo/asynchronous/pool.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,9 @@ def __init__(
683683
self._ssl_session_cache: Optional[list[Any]] = (
684684
[None] if self.opts._ssl_context is not None else None
685685
)
686-
self._telemetry = _CmapTelemetry(client_id, address, options._event_listeners, is_sdam)
686+
self._telemetry = _CmapTelemetry(
687+
client_id, address, options._event_listeners, publish=not is_sdam, log=not is_sdam
688+
)
687689
self._telemetry.pool_created(self.opts.non_default_options)
688690
# Similar to active_sockets but includes threads in the wait queue.
689691
self.operation_count: int = 0

pymongo/synchronous/monitor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,12 @@ def _check_once(self) -> ServerDescription:
292292
self._topology._topology_id, address, self._listeners, self._publish, awaited
293293
)
294294
self._current_hb = hb
295-
hb.apm_started()
295+
hb.started()
296296

297297
if self._cancel_context and self._cancel_context.cancelled:
298298
self._reset_connection()
299299
with self._pool.checkout() as conn:
300-
hb.log_started(conn.id, conn.server_connection_id)
300+
hb.emit_started_log(conn.id, conn.server_connection_id)
301301
self._cancel_context = conn.cancel_context
302302
# Record the connection id so we can later attach it to the failed log message.
303303
self._conn_id = conn.id

pymongo/synchronous/pool.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,9 @@ def __init__(
681681
self._ssl_session_cache: Optional[list[Any]] = (
682682
[None] if self.opts._ssl_context is not None else None
683683
)
684-
self._telemetry = _CmapTelemetry(client_id, address, options._event_listeners, is_sdam)
684+
self._telemetry = _CmapTelemetry(
685+
client_id, address, options._event_listeners, publish=not is_sdam, log=not is_sdam
686+
)
685687
self._telemetry.pool_created(self.opts.non_default_options)
686688
# Similar to active_sockets but includes threads in the wait queue.
687689
self.operation_count: int = 0

0 commit comments

Comments
 (0)