fix(recorder): patch replica DB connection

This commit is contained in:
Ankush Menat 2025-04-24 10:52:05 +05:30
parent 26b1360c50
commit 360d19741e
2 changed files with 10 additions and 6 deletions

View file

@ -353,6 +353,9 @@ def connect_replica() -> bool:
local.primary_db = local.db
local.db = local.replica_db
if hasattr(frappe.local, "_recorder"):
frappe.local._recorder._patch_sql(local.db)
return True

View file

@ -183,6 +183,7 @@ def record(force=False):
flag_value = frappe.client_cache.get_value(RECORDER_INTERCEPT_FLAG)
if flag_value or force:
frappe.local._recorder = Recorder(force=force)
return frappe.local._recorder
elif flag_value is None:
# Explicitly set it once so next requests can use client-side cache
frappe.client_cache.set_value(RECORDER_INTERCEPT_FLAG, False)
@ -233,8 +234,7 @@ class Recorder:
self.uuid = frappe.generate_hash(length=10)
self.time = now_datetime()
if self.config.record_sql:
self._patch_sql(frappe.db)
self._patch_sql(frappe.db)
if self.config.profile:
self.profiler = cProfile.Profile()
@ -283,10 +283,12 @@ class Recorder:
request_data["profile"] = profiler_output
frappe.cache.hset(RECORDER_REQUEST_HASH, self.uuid, request_data)
if self.config.record_sql:
self._unpatch_sql()
self._unpatch_sql()
def _patch_sql(self, db: "Database"):
if not self.config.record_sql:
return
frappe.db._sql = frappe.db.sql
frappe.db.sql = record_sql
self.patched_databases.append(db)
@ -391,8 +393,7 @@ def record_queries(func: Callable):
@functools.wraps(func)
def wrapped(*args, **kwargs):
record(force=True)
recorder = frappe.local._recorder
recorder = record(force=True)
recorder.path = f"Function call: {func.__module__}.{func.__qualname__}"
ret = func(*args, **kwargs)
dump()