From a3a9e40aa4f78a1c66457a9c25f4194f42c003f2 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Tue, 11 Apr 2023 07:29:14 +0530 Subject: [PATCH] fix: log requests even if no response (#20638) --- frappe/monitor.py | 7 +++++-- frappe/tests/test_monitor.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/frappe/monitor.py b/frappe/monitor.py index 8db1e25d32..b93ba1d3bb 100644 --- a/frappe/monitor.py +++ b/frappe/monitor.py @@ -89,8 +89,11 @@ class Monitor: self.data.duration = int(timediff.total_seconds() * 1000000) if self.data.transaction_type == "request": - self.data.request.status_code = response.status_code - self.data.request.response_length = int(response.headers.get("Content-Length", 0)) + if response: + self.data.request.status_code = response.status_code + self.data.request.response_length = int(response.headers.get("Content-Length", 0)) + else: + self.data.request.status_code = 500 if hasattr(frappe.local, "rate_limiter"): limiter = frappe.local.rate_limiter diff --git a/frappe/tests/test_monitor.py b/frappe/tests/test_monitor.py index 7536c6a75a..e59ebcde31 100644 --- a/frappe/tests/test_monitor.py +++ b/frappe/tests/test_monitor.py @@ -33,6 +33,20 @@ class TestMonitor(FrappeTestCase): self.assertEqual(log.transaction_type, "request") self.assertEqual(log.request["method"], "GET") + def test_no_response(self): + set_request(method="GET", path="/api/method/frappe.ping") + + frappe.monitor.start() + frappe.monitor.stop(response=None) + + logs = frappe.cache().lrange(MONITOR_REDIS_KEY, 0, -1) + self.assertEqual(len(logs), 1) + + log = frappe.parse_json(logs[0].decode()) + self.assertEqual(log.request["status_code"], 500) + self.assertEqual(log.transaction_type, "request") + self.assertEqual(log.request["method"], "GET") + def test_job(self): frappe.utils.background_jobs.execute_job( frappe.local.site, "frappe.ping", None, None, {}, is_async=False