Skip to content

Commit 77c71cd

Browse files
committed
Tag potentially expensive queries with their job id for easier debugging
1 parent 0e95156 commit 77c71cd

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

mrq/monkey.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import time
33
import random
44
import re
5+
import copy
56

67

78
def patch_method(base_class, method_name, method):
@@ -43,15 +44,6 @@ def gen_monkey_patch(base_object, method):
4344
base_method = getattr(base_object, method)
4445

4546
def mrq_monkey_patched(self, *args, **kwargs):
46-
if config["print_mongodb"]:
47-
if self.full_name in config.get(
48-
"print_mongodb_hidden_collections",
49-
[]):
50-
cprint("[MONGO] %s.%s%s %s" % (
51-
self.full_name, method, "-hidden-", kwargs), "magenta")
52-
else:
53-
cprint("[MONGO] %s.%s%s %s" %
54-
(self.full_name, method, args, kwargs), "magenta")
5547

5648
if config["trace_io"]:
5749
job = get_current_job()
@@ -62,9 +54,26 @@ def mrq_monkey_patched(self, *args, **kwargs):
6254
"collection": self.full_name
6355
}
6456
# Perf issue? All MongoDB data will get jsonified!
65-
#"data": json.dumps(args)[0:300]
57+
# "data": json.dumps(args)[0:300]
6658
})
6759

60+
# Tag potentially expensive queries with their job id for easier debugging
61+
if method in ["find", "find_and_modify", "count", "update_many", "update", "delete_many"]:
62+
if len(args) > 0 and type(args[0]) == dict and "$comment" not in args[0]:
63+
query = copy.copy(args[0])
64+
query["$comment"] = {"job": job.id}
65+
args = (query, ) + args[1:]
66+
67+
if config["print_mongodb"]:
68+
if self.full_name in config.get("print_mongodb_hidden_collections", []):
69+
cprint("[MONGO] %s.%s%s %s" % (
70+
self.full_name, method, "-hidden-", kwargs
71+
), "magenta")
72+
else:
73+
cprint("[MONGO] %s.%s%s %s" % (
74+
self.full_name, method, args, kwargs
75+
), "magenta")
76+
6877
try:
6978
ret = base_method(self, *args, **kwargs)
7079
finally:

0 commit comments

Comments
 (0)