fix: log requests even if no response (#20638)

This commit is contained in:
Ankush Menat 2023-04-11 07:29:14 +05:30 committed by GitHub
parent 822fd4e37e
commit a3a9e40aa4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View file

@ -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

View file

@ -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