From af3c4feb6428eb3685a1dccf8e039f68ff9ad020 Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Mon, 24 Feb 2020 13:29:07 +0530 Subject: [PATCH 01/10] feat: Monitor Collect HTTP Request and Background Job logs --- frappe/app.py | 3 + frappe/hooks.py | 3 +- frappe/monitor.py | 102 ++++++++++++++++++++++++++++++++ frappe/tests/test_monitor.py | 57 ++++++++++++++++++ frappe/utils/background_jobs.py | 3 + frappe/utils/redis_wrapper.py | 6 ++ 6 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 frappe/monitor.py create mode 100644 frappe/tests/test_monitor.py diff --git a/frappe/app.py b/frappe/app.py index 3c46110c2e..e3ac2f495d 100644 --- a/frappe/app.py +++ b/frappe/app.py @@ -25,6 +25,7 @@ from frappe.utils.error import make_error_snapshot from frappe.core.doctype.comment.comment import update_comments_in_parent_after_request from frappe import _ import frappe.recorder +import frappe.monitor local_manager = LocalManager([frappe.local]) @@ -52,6 +53,7 @@ def application(request): init_request(request) frappe.recorder.record() + frappe.monitor.start() if frappe.local.form_dict.cmd: response = frappe.handler.handle() @@ -91,6 +93,7 @@ def application(request): if response and hasattr(frappe.local, 'cookie_manager'): frappe.local.cookie_manager.flush_cookies(response=response) + frappe.monitor.stop() frappe.recorder.dump() frappe.destroy() diff --git a/frappe/hooks.py b/frappe/hooks.py index a132eb69d5..9d5389b529 100644 --- a/frappe/hooks.py +++ b/frappe/hooks.py @@ -174,7 +174,8 @@ scheduler_events = { "frappe.email.doctype.email_account.email_account.pull", "frappe.email.doctype.email_account.email_account.notify_unreplied", "frappe.integrations.doctype.razorpay_settings.razorpay_settings.capture_payment", - 'frappe.utils.global_search.sync_global_search' + 'frappe.utils.global_search.sync_global_search', + "frappe.monitor.flush", ], "hourly": [ "frappe.model.utils.link_count.update_link_count", diff --git a/frappe/monitor.py b/frappe/monitor.py new file mode 100644 index 0000000000..3efb92e91c --- /dev/null +++ b/frappe/monitor.py @@ -0,0 +1,102 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and Contributors +# MIT License. See license.txt + +from __future__ import unicode_literals + +from datetime import datetime, timezone +import json +import time +import traceback +import frappe +import os + + +MONITOR_REDIS_KEY = "monitor-transactions" + + +def start(transaction_type="request", method=None, kwargs=None): + if frappe.conf.monitor: + frappe.local.monitor = Monitor( + transaction_type=transaction_type, method=method, kwargs=kwargs + ) + + +def stop(): + if frappe.conf.monitor and hasattr(frappe.local, "monitor"): + frappe.local.monitor.dump() + + +def log_file(): + return os.path.join(frappe.utils.get_bench_path(), "logs", "monitor.json.log") + + +class Monitor: + def __init__(self, transaction_type=None, method=None, kwargs=None): + try: + self.site = frappe.local.site + self.timestamp = datetime.now(timezone.utc) + self.transaction_type = transaction_type + + if self.transaction_type == "request": + self.data = frappe.form_dict + self.headers = dict(frappe.request.headers) + self.method = frappe.request.method + self.path = frappe.request.path + else: + self.kwargs = kwargs + self.method = method + except Exception: + traceback.print_exc() + + def dump(self): + try: + timediff = datetime.now(timezone.utc) - self.timestamp + # Obtain duration in microseconds + self.duration = int(timediff.total_seconds() * 1000000) + data = { + "duration": self.duration, + "site": self.site, + "timestamp": self.timestamp.isoformat(sep=" "), + "transaction_type": self.transaction_type, + } + + if self.transaction_type == "request": + update = { + "data": self.data, + "headers": self.headers, + "method": self.method, + "path": self.path, + } + else: + update = { + "kwargs": self.kwargs, + "method": self.method, + } + data.update(update) + json_data = json.dumps(data, sort_keys=True, default=str) + store(json_data) + except Exception: + traceback.print_exc() + + +def store(json_data): + if frappe.cache().llen(MONITOR_REDIS_KEY) >= 1000000: + frappe.cache().ltrim(MONITOR_REDIS_KEY, len(logs) - 1, -1) + frappe.cache().rpush(MONITOR_REDIS_KEY, json_data) + else: + frappe.cache().rpush(MONITOR_REDIS_KEY, json_data) + + +def flush(): + try: + # Fetch all the logs without removing from cache + logs = frappe.cache().lrange(MONITOR_REDIS_KEY, 0, -1) + logs = list(map(frappe.safe_decode, logs)) + with open(log_file(), "a", os.O_NONBLOCK) as f: + f.write("\n".join(logs)) + + # Remove fetched entries from cache + frappe.cache().ltrim(MONITOR_REDIS_KEY, len(logs) - 1, -1) + except Exception: + traceback.print_exc() diff --git a/frappe/tests/test_monitor.py b/frappe/tests/test_monitor.py new file mode 100644 index 0000000000..9f655660d5 --- /dev/null +++ b/frappe/tests/test_monitor.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and Contributors +# MIT License. See license.txt + +from __future__ import unicode_literals +import unittest +import frappe +import frappe.monitor +from frappe.utils import set_request +from frappe.monitor import MONITOR_REDIS_KEY +import json + + +class TestMonitor(unittest.TestCase): + def setUp(self): + frappe.conf.monitor = 1 + frappe.cache().delete_value(MONITOR_REDIS_KEY) + + def test_enable_monitor(self): + set_request() + frappe.monitor.start() + frappe.monitor.stop() + + logs = frappe.cache().lrange(MONITOR_REDIS_KEY, 0, -1) + self.assertEqual(len(logs), 1) + log = json.loads(logs[0].decode()) + self.assertEqual(log["transaction_type"], "request") + + def test_flush(self): + set_request() + frappe.monitor.start() + frappe.monitor.stop() + + open(frappe.monitor.log_file(), "w").close() + frappe.monitor.flush() + + with open(frappe.monitor.log_file()) as f: + logs = f.readlines() + + self.assertEqual(len(logs), 1) + log = json.loads(logs[0]) + self.assertEqual(log["transaction_type"], "request") + + def test_job(self): + frappe.utils.background_jobs.execute_job( + frappe.local.site, "frappe.ping", None, None, {}, is_async=False + ) + + logs = frappe.cache().lrange(MONITOR_REDIS_KEY, 0, -1) + self.assertEqual(len(logs), 1) + log = json.loads(logs[0].decode()) + self.assertEqual(log["transaction_type"], "job") + self.assertEqual(log["method"], "frappe.ping") + + def tearDown(self): + frappe.conf.monitor = 0 + frappe.cache().delete_value(MONITOR_REDIS_KEY) diff --git a/frappe/utils/background_jobs.py b/frappe/utils/background_jobs.py index 11c710626b..02f2319d55 100755 --- a/frappe/utils/background_jobs.py +++ b/frappe/utils/background_jobs.py @@ -9,6 +9,7 @@ import os, socket, time from frappe import _ from six import string_types from uuid import uuid4 +import frappe.monitor # imports - third-party imports @@ -94,6 +95,7 @@ def execute_job(site, method, event, job_name, kwargs, user=None, is_async=True, else: method_name = cstr(method.__name__) + frappe.monitor.start(transaction_type="job", method=method_name, kwargs=kwargs) try: method(**kwargs) @@ -128,6 +130,7 @@ def execute_job(site, method, event, job_name, kwargs, user=None, is_async=True, frappe.db.commit() finally: + frappe.monitor.stop() if is_async: frappe.destroy() diff --git a/frappe/utils/redis_wrapper.py b/frappe/utils/redis_wrapper.py index 4af59bceb2..b0c0990e85 100644 --- a/frappe/utils/redis_wrapper.py +++ b/frappe/utils/redis_wrapper.py @@ -140,6 +140,12 @@ class RedisWrapper(redis.Redis): def llen(self, key): return super(RedisWrapper, self).llen(self.make_key(key)) + def lrange(self, key, start, stop): + return super(RedisWrapper, self).lrange(self.make_key(key), start, stop) + + def ltrim(self, key, start, stop): + return super(RedisWrapper, self).ltrim(self.make_key(key), start, stop) + def hset(self, name, key, value, shared=False): if key is None: return From 96d9f2624b9d8523420e66cf19e11e369958cbfc Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Mon, 24 Feb 2020 13:47:34 +0530 Subject: [PATCH 02/10] fix(monitor): Maintain MAX_LOG entries in cache --- frappe/monitor.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/frappe/monitor.py b/frappe/monitor.py index 3efb92e91c..b0e7f023dd 100644 --- a/frappe/monitor.py +++ b/frappe/monitor.py @@ -81,11 +81,10 @@ class Monitor: def store(json_data): - if frappe.cache().llen(MONITOR_REDIS_KEY) >= 1000000: - frappe.cache().ltrim(MONITOR_REDIS_KEY, len(logs) - 1, -1) - frappe.cache().rpush(MONITOR_REDIS_KEY, json_data) - else: - frappe.cache().rpush(MONITOR_REDIS_KEY, json_data) + MAX_LOGS = 1000000 + if frappe.cache().llen(MONITOR_REDIS_KEY) > MAX_LOGS: + frappe.cache().ltrim(MONITOR_REDIS_KEY, 1, -1) + frappe.cache().rpush(MONITOR_REDIS_KEY, json_data) def flush(): From ea3f5632d1f45275028d0e818ab6a4daa9e9119e Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Mon, 24 Feb 2020 13:51:08 +0530 Subject: [PATCH 03/10] fix(monitor): Remove unused imports --- frappe/monitor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/frappe/monitor.py b/frappe/monitor.py index b0e7f023dd..cf7b1aec28 100644 --- a/frappe/monitor.py +++ b/frappe/monitor.py @@ -6,7 +6,6 @@ from __future__ import unicode_literals from datetime import datetime, timezone import json -import time import traceback import frappe import os From 56e1cdde5388a8f31efd4a524b8f95a302171c19 Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Mon, 24 Feb 2020 15:48:48 +0530 Subject: [PATCH 04/10] fix(monitor): Add UUID to every log --- frappe/monitor.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/frappe/monitor.py b/frappe/monitor.py index cf7b1aec28..b864e2813e 100644 --- a/frappe/monitor.py +++ b/frappe/monitor.py @@ -9,6 +9,7 @@ import json import traceback import frappe import os +import uuid MONITOR_REDIS_KEY = "monitor-transactions" @@ -36,6 +37,7 @@ class Monitor: self.site = frappe.local.site self.timestamp = datetime.now(timezone.utc) self.transaction_type = transaction_type + self.uuid = uuid.uuid4() if self.transaction_type == "request": self.data = frappe.form_dict @@ -54,6 +56,7 @@ class Monitor: # Obtain duration in microseconds self.duration = int(timediff.total_seconds() * 1000000) data = { + "uuid": self.uuid, "duration": self.duration, "site": self.site, "timestamp": self.timestamp.isoformat(sep=" "), From cb3507f5e474fdb9fa93db2163a95e8422917451 Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Mon, 24 Feb 2020 15:52:43 +0530 Subject: [PATCH 05/10] fix(monitor): Use datetime.utcnow() instead of timezone.utc Python 2, 3 compatibility issue --- frappe/monitor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frappe/monitor.py b/frappe/monitor.py index b864e2813e..196e728c11 100644 --- a/frappe/monitor.py +++ b/frappe/monitor.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals -from datetime import datetime, timezone +from datetime import datetime import json import traceback import frappe @@ -35,7 +35,7 @@ class Monitor: def __init__(self, transaction_type=None, method=None, kwargs=None): try: self.site = frappe.local.site - self.timestamp = datetime.now(timezone.utc) + self.timestamp = datetime.utcnow() self.transaction_type = transaction_type self.uuid = uuid.uuid4() @@ -52,7 +52,7 @@ class Monitor: def dump(self): try: - timediff = datetime.now(timezone.utc) - self.timestamp + timediff = datetime.utcnow() - self.timestamp # Obtain duration in microseconds self.duration = int(timediff.total_seconds() * 1000000) data = { From 2b8c5a7f479727fed4814a173fa7c695fabe501b Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Mon, 24 Feb 2020 16:15:29 +0530 Subject: [PATCH 06/10] fix: Push a newline after all logs --- frappe/monitor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/frappe/monitor.py b/frappe/monitor.py index 196e728c11..4525f58e5c 100644 --- a/frappe/monitor.py +++ b/frappe/monitor.py @@ -96,6 +96,7 @@ def flush(): logs = list(map(frappe.safe_decode, logs)) with open(log_file(), "a", os.O_NONBLOCK) as f: f.write("\n".join(logs)) + f.write("\n") # Remove fetched entries from cache frappe.cache().ltrim(MONITOR_REDIS_KEY, len(logs) - 1, -1) From 268c577acd07bce4eb7e63bab6a38a7b436bc2e5 Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Mon, 24 Feb 2020 18:20:15 +0530 Subject: [PATCH 07/10] fix(monitor): Include request ip in monitored data --- frappe/monitor.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frappe/monitor.py b/frappe/monitor.py index 4525f58e5c..794bac1390 100644 --- a/frappe/monitor.py +++ b/frappe/monitor.py @@ -42,6 +42,7 @@ class Monitor: if self.transaction_type == "request": self.data = frappe.form_dict self.headers = dict(frappe.request.headers) + self.ip = frappe.local.request_ip self.method = frappe.request.method self.path = frappe.request.path else: @@ -67,6 +68,7 @@ class Monitor: update = { "data": self.data, "headers": self.headers, + "ip": self.ip, "method": self.method, "path": self.path, } From 4740e4f7e6579b8d838336c41bb97ce713e3b55b Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Thu, 5 Mar 2020 15:05:40 +0530 Subject: [PATCH 08/10] refactor: Monitor Do not collect request headers Collect job wait time and whether it was scheduled --- frappe/app.py | 2 +- frappe/monitor.py | 119 ++++++++++++++++---------------- frappe/tests/test_monitor.py | 60 ++++++++++------ frappe/utils/background_jobs.py | 2 +- 4 files changed, 101 insertions(+), 82 deletions(-) diff --git a/frappe/app.py b/frappe/app.py index e3ac2f495d..24ce35b514 100644 --- a/frappe/app.py +++ b/frappe/app.py @@ -93,7 +93,7 @@ def application(request): if response and hasattr(frappe.local, 'cookie_manager'): frappe.local.cookie_manager.flush_cookies(response=response) - frappe.monitor.stop() + frappe.monitor.stop(response) frappe.recorder.dump() frappe.destroy() diff --git a/frappe/monitor.py b/frappe/monitor.py index 794bac1390..7181bd92ad 100644 --- a/frappe/monitor.py +++ b/frappe/monitor.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and Contributors +# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors # MIT License. See license.txt from __future__ import unicode_literals @@ -10,21 +10,21 @@ import traceback import frappe import os import uuid +import rq MONITOR_REDIS_KEY = "monitor-transactions" +MONITOR_MAX_ENTRIES = 1000000 def start(transaction_type="request", method=None, kwargs=None): if frappe.conf.monitor: - frappe.local.monitor = Monitor( - transaction_type=transaction_type, method=method, kwargs=kwargs - ) + frappe.local.monitor = Monitor(transaction_type, method, kwargs) -def stop(): +def stop(response=None): if frappe.conf.monitor and hasattr(frappe.local, "monitor"): - frappe.local.monitor.dump() + frappe.local.monitor.dump(response) def log_file(): @@ -32,75 +32,76 @@ def log_file(): class Monitor: - def __init__(self, transaction_type=None, method=None, kwargs=None): + def __init__(self, transaction_type, method, kwargs): try: - self.site = frappe.local.site - self.timestamp = datetime.utcnow() - self.transaction_type = transaction_type - self.uuid = uuid.uuid4() + self.data = frappe._dict( + { + "site": frappe.local.site, + "timestamp": datetime.utcnow(), + "transaction_type": transaction_type, + "uuid": str(uuid.uuid4()), + } + ) - if self.transaction_type == "request": - self.data = frappe.form_dict - self.headers = dict(frappe.request.headers) - self.ip = frappe.local.request_ip - self.method = frappe.request.method - self.path = frappe.request.path + if transaction_type == "request": + self.collect_request_meta() else: - self.kwargs = kwargs - self.method = method + self.collect_job_meta(method, kwargs) except Exception: traceback.print_exc() - def dump(self): - try: - timediff = datetime.utcnow() - self.timestamp - # Obtain duration in microseconds - self.duration = int(timediff.total_seconds() * 1000000) - data = { - "uuid": self.uuid, - "duration": self.duration, - "site": self.site, - "timestamp": self.timestamp.isoformat(sep=" "), - "transaction_type": self.transaction_type, + def collect_request_meta(self): + self.data.request = frappe._dict( + { + "ip": frappe.local.request_ip, + "method": frappe.request.method, + "path": frappe.request.path, } + ) - if self.transaction_type == "request": - update = { - "data": self.data, - "headers": self.headers, - "ip": self.ip, - "method": self.method, - "path": self.path, - } - else: - update = { - "kwargs": self.kwargs, - "method": self.method, - } - data.update(update) - json_data = json.dumps(data, sort_keys=True, default=str) - store(json_data) + def collect_job_meta(self, method, kwargs): + self.data.job = frappe._dict({"method": method, "scheduled": False, "wait": 0}) + if "run_scheduled_job" in method: + self.data.job.method = kwargs["job_type"] + self.data.job.scheduled = True + + job = rq.get_current_job() + if job: + self.data.uuid = job.id + waitdiff = self.data.timestamp - job.enqueued_at + self.data.job.wait = int(waitdiff.total_seconds() * 1000000) + + def dump(self, response=None): + try: + timediff = datetime.utcnow() - self.data.timestamp + # Obtain duration in microseconds + 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["Content-Length"]) + + self.store() except Exception: traceback.print_exc() - -def store(json_data): - MAX_LOGS = 1000000 - if frappe.cache().llen(MONITOR_REDIS_KEY) > MAX_LOGS: - frappe.cache().ltrim(MONITOR_REDIS_KEY, 1, -1) - frappe.cache().rpush(MONITOR_REDIS_KEY, json_data) + def store(self): + if frappe.cache().llen(MONITOR_REDIS_KEY) > MONITOR_MAX_ENTRIES: + frappe.cache().ltrim(MONITOR_REDIS_KEY, 1, -1) + serialized = json.dumps(self.data, sort_keys=True, default=str) + frappe.cache().rpush(MONITOR_REDIS_KEY, serialized) def flush(): try: # Fetch all the logs without removing from cache logs = frappe.cache().lrange(MONITOR_REDIS_KEY, 0, -1) - logs = list(map(frappe.safe_decode, logs)) - with open(log_file(), "a", os.O_NONBLOCK) as f: - f.write("\n".join(logs)) - f.write("\n") - - # Remove fetched entries from cache - frappe.cache().ltrim(MONITOR_REDIS_KEY, len(logs) - 1, -1) + if logs: + logs = list(map(frappe.safe_decode, logs)) + with open(log_file(), "a", os.O_NONBLOCK) as f: + f.write("\n".join(logs)) + f.write("\n") + # Remove fetched entries from cache + frappe.cache().ltrim(MONITOR_REDIS_KEY, len(logs) - 1, -1) except Exception: traceback.print_exc() diff --git a/frappe/tests/test_monitor.py b/frappe/tests/test_monitor.py index 9f655660d5..d99b324d84 100644 --- a/frappe/tests/test_monitor.py +++ b/frappe/tests/test_monitor.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and Contributors +# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors # MIT License. See license.txt from __future__ import unicode_literals @@ -7,6 +7,7 @@ import unittest import frappe import frappe.monitor from frappe.utils import set_request +from frappe.utils.response import build_response from frappe.monitor import MONITOR_REDIS_KEY import json @@ -17,29 +18,27 @@ class TestMonitor(unittest.TestCase): frappe.cache().delete_value(MONITOR_REDIS_KEY) def test_enable_monitor(self): - set_request() + set_request(method="GET", path="/api/method/frappe.ping") + response = build_response("json") + frappe.monitor.start() - frappe.monitor.stop() + frappe.monitor.stop(response) logs = frappe.cache().lrange(MONITOR_REDIS_KEY, 0, -1) self.assertEqual(len(logs), 1) - log = json.loads(logs[0].decode()) - self.assertEqual(log["transaction_type"], "request") - def test_flush(self): - set_request() - frappe.monitor.start() - frappe.monitor.stop() + log = frappe.parse_json(logs[0].decode()) + self.assertTrue(log.duration) + self.assertTrue(log.site) + self.assertTrue(log.timestamp) + self.assertTrue(log.uuid) + self.assertTrue(log.request) + self.assertEqual(log.transaction_type, "request") + self.assertEqual(log.request["method"], "GET") - open(frappe.monitor.log_file(), "w").close() - frappe.monitor.flush() - - with open(frappe.monitor.log_file()) as f: - logs = f.readlines() - - self.assertEqual(len(logs), 1) - log = json.loads(logs[0]) - self.assertEqual(log["transaction_type"], "request") + # Reponse body will be set as "{}" + self.assertEqual(log.request["response_length"], 2) + self.assertEqual(log.request["status_code"], 200) def test_job(self): frappe.utils.background_jobs.execute_job( @@ -48,9 +47,28 @@ class TestMonitor(unittest.TestCase): logs = frappe.cache().lrange(MONITOR_REDIS_KEY, 0, -1) self.assertEqual(len(logs), 1) - log = json.loads(logs[0].decode()) - self.assertEqual(log["transaction_type"], "job") - self.assertEqual(log["method"], "frappe.ping") + log = frappe.parse_json(logs[0].decode()) + self.assertEqual(log.transaction_type, "job") + self.assertTrue(log.job) + self.assertEqual(log.job["method"], "frappe.ping") + self.assertEqual(log.job["scheduled"], False) + self.assertEqual(log.job["wait"], 0) + + def test_flush(self): + set_request(method="GET", path="/api/method/frappe.ping") + response = build_response("json") + frappe.monitor.start() + frappe.monitor.stop(response) + + open(frappe.monitor.log_file(), "w").close() + frappe.monitor.flush() + + with open(frappe.monitor.log_file()) as f: + logs = f.readlines() + + self.assertEqual(len(logs), 1) + log = frappe.parse_json(logs[0]) + self.assertEqual(log.transaction_type, "request") def tearDown(self): frappe.conf.monitor = 0 diff --git a/frappe/utils/background_jobs.py b/frappe/utils/background_jobs.py index 02f2319d55..03f063e058 100755 --- a/frappe/utils/background_jobs.py +++ b/frappe/utils/background_jobs.py @@ -95,7 +95,7 @@ def execute_job(site, method, event, job_name, kwargs, user=None, is_async=True, else: method_name = cstr(method.__name__) - frappe.monitor.start(transaction_type="job", method=method_name, kwargs=kwargs) + frappe.monitor.start("job", method_name, kwargs) try: method(**kwargs) From 4e69326faca7d3847ca31ba0c694a27ef51a8e17 Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Thu, 5 Mar 2020 16:27:22 +0530 Subject: [PATCH 09/10] style: Remove unused imports --- frappe/tests/test_monitor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/frappe/tests/test_monitor.py b/frappe/tests/test_monitor.py index d99b324d84..137ec2d8ae 100644 --- a/frappe/tests/test_monitor.py +++ b/frappe/tests/test_monitor.py @@ -9,7 +9,6 @@ import frappe.monitor from frappe.utils import set_request from frappe.utils.response import build_response from frappe.monitor import MONITOR_REDIS_KEY -import json class TestMonitor(unittest.TestCase): From 542a3ce222bc31ff606acbf9578ba6e44f863375 Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Thu, 5 Mar 2020 17:20:59 +0530 Subject: [PATCH 10/10] fix: Remove flaky assertions --- frappe/tests/test_monitor.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/frappe/tests/test_monitor.py b/frappe/tests/test_monitor.py index 137ec2d8ae..b447e89b06 100644 --- a/frappe/tests/test_monitor.py +++ b/frappe/tests/test_monitor.py @@ -35,10 +35,6 @@ class TestMonitor(unittest.TestCase): self.assertEqual(log.transaction_type, "request") self.assertEqual(log.request["method"], "GET") - # Reponse body will be set as "{}" - self.assertEqual(log.request["response_length"], 2) - self.assertEqual(log.request["status_code"], 200) - def test_job(self): frappe.utils.background_jobs.execute_job( frappe.local.site, "frappe.ping", None, None, {}, is_async=False