Skip to content

Commit d88596c

Browse files
NoahStappJibola
andauthored
PYTHON-5218 - Add logging statement when SRV polling fails (#2463)
Co-authored-by: Jib <Jibzade@gmail.com>
1 parent ad16d6e commit d88596c

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

pymongo/asynchronous/monitor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,13 @@ async def _get_seedlist(self) -> Optional[list[tuple[str, Any]]]:
423423
if len(seedlist) == 0:
424424
# As per the spec: this should be treated as a failure.
425425
raise Exception
426-
except Exception:
426+
except Exception as exc:
427427
# As per the spec, upon encountering an error:
428428
# - An error must not be raised
429429
# - SRV records must be rescanned every heartbeatFrequencyMS
430430
# - Topology must be left unchanged
431431
self.request_check()
432+
_debug_log(_SDAM_LOGGER, message="SRV monitor check failed", failure=repr(exc))
432433
return None
433434
else:
434435
self._executor.update_interval(max(ttl, common.MIN_SRV_RESCAN_INTERVAL))

pymongo/synchronous/monitor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,13 @@ def _get_seedlist(self) -> Optional[list[tuple[str, Any]]]:
421421
if len(seedlist) == 0:
422422
# As per the spec: this should be treated as a failure.
423423
raise Exception
424-
except Exception:
424+
except Exception as exc:
425425
# As per the spec, upon encountering an error:
426426
# - An error must not be raised
427427
# - SRV records must be rescanned every heartbeatFrequencyMS
428428
# - Topology must be left unchanged
429429
self.request_check()
430+
_debug_log(_SDAM_LOGGER, message="SRV monitor check failed", failure=repr(exc))
430431
return None
431432
else:
432433
self._executor.update_interval(max(ttl, common.MIN_SRV_RESCAN_INTERVAL))

test/asynchronous/test_srv_polling.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,19 @@ def response_callback(*args):
225225

226226
await self.run_scenario(response_callback, False)
227227

228+
async def test_dns_failures_logging(self):
229+
from dns import exception
230+
231+
with self.assertLogs("pymongo.topology", level="DEBUG") as cm:
232+
233+
def response_callback(*args):
234+
raise exception.Timeout("DNS Failure!")
235+
236+
await self.run_scenario(response_callback, False)
237+
238+
srv_failure_logs = [r for r in cm.records if "SRV monitor check failed" in r.getMessage()]
239+
self.assertEqual(len(srv_failure_logs), 1)
240+
228241
async def test_dns_record_lookup_empty(self):
229242
response: list = []
230243
await self.run_scenario(response, False)

test/test_srv_polling.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,19 @@ def response_callback(*args):
225225

226226
self.run_scenario(response_callback, False)
227227

228+
def test_dns_failures_logging(self):
229+
from dns import exception
230+
231+
with self.assertLogs("pymongo.topology", level="DEBUG") as cm:
232+
233+
def response_callback(*args):
234+
raise exception.Timeout("DNS Failure!")
235+
236+
self.run_scenario(response_callback, False)
237+
238+
srv_failure_logs = [r for r in cm.records if "SRV monitor check failed" in r.getMessage()]
239+
self.assertEqual(len(srv_failure_logs), 1)
240+
228241
def test_dns_record_lookup_empty(self):
229242
response: list = []
230243
self.run_scenario(response, False)

0 commit comments

Comments
 (0)