feat(config): add live slow-sql threshold setting
This commit is contained in:
49
db_pool.py
49
db_pool.py
@@ -27,6 +27,45 @@ DB_LOCK_RETRY_BASE_MS = max(10, int(getattr(config, "DB_LOCK_RETRY_BASE_MS", 50)
|
||||
DB_SLOW_QUERY_MS = max(0, int(getattr(config, "DB_SLOW_QUERY_MS", 120)))
|
||||
DB_SLOW_QUERY_SQL_MAX_LEN = max(80, int(getattr(config, "DB_SLOW_QUERY_SQL_MAX_LEN", 240)))
|
||||
|
||||
_slow_query_runtime_lock = threading.Lock()
|
||||
_slow_query_runtime_threshold_ms = DB_SLOW_QUERY_MS
|
||||
_slow_query_runtime_sql_max_len = DB_SLOW_QUERY_SQL_MAX_LEN
|
||||
|
||||
|
||||
def _get_slow_query_runtime_values() -> tuple[int, int]:
|
||||
with _slow_query_runtime_lock:
|
||||
return int(_slow_query_runtime_threshold_ms), int(_slow_query_runtime_sql_max_len)
|
||||
|
||||
|
||||
def get_slow_query_runtime() -> dict:
|
||||
threshold_ms, sql_max_len = _get_slow_query_runtime_values()
|
||||
return {"threshold_ms": threshold_ms, "sql_max_len": sql_max_len}
|
||||
|
||||
|
||||
def configure_slow_query_runtime(*, threshold_ms=None, sql_max_len=None) -> dict:
|
||||
global _slow_query_runtime_threshold_ms, _slow_query_runtime_sql_max_len
|
||||
|
||||
with _slow_query_runtime_lock:
|
||||
if threshold_ms is not None:
|
||||
_slow_query_runtime_threshold_ms = max(0, int(threshold_ms))
|
||||
if sql_max_len is not None:
|
||||
_slow_query_runtime_sql_max_len = max(80, int(sql_max_len))
|
||||
|
||||
runtime_threshold_ms = int(_slow_query_runtime_threshold_ms)
|
||||
runtime_sql_max_len = int(_slow_query_runtime_sql_max_len)
|
||||
|
||||
try:
|
||||
from services.slow_sql_metrics import configure_slow_sql_runtime
|
||||
|
||||
configure_slow_sql_runtime(
|
||||
threshold_ms=runtime_threshold_ms,
|
||||
sql_max_len=runtime_sql_max_len,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return {"threshold_ms": runtime_threshold_ms, "sql_max_len": runtime_sql_max_len}
|
||||
|
||||
|
||||
def _is_lock_conflict_error(error: sqlite3.OperationalError) -> bool:
|
||||
message = str(error or "").lower()
|
||||
@@ -34,10 +73,11 @@ def _is_lock_conflict_error(error: sqlite3.OperationalError) -> bool:
|
||||
|
||||
|
||||
def _compact_sql(sql: str) -> str:
|
||||
_, sql_max_len = _get_slow_query_runtime_values()
|
||||
statement = " ".join(str(sql or "").split())
|
||||
if len(statement) <= DB_SLOW_QUERY_SQL_MAX_LEN:
|
||||
if len(statement) <= sql_max_len:
|
||||
return statement
|
||||
return statement[: DB_SLOW_QUERY_SQL_MAX_LEN - 3] + "..."
|
||||
return statement[: sql_max_len - 3] + "..."
|
||||
|
||||
|
||||
def _describe_params(parameters) -> str:
|
||||
@@ -298,9 +338,10 @@ class PooledConnection:
|
||||
return False
|
||||
|
||||
def _on_query_executed(self, sql: str, parameters, elapsed_ms: float) -> None:
|
||||
if DB_SLOW_QUERY_MS <= 0:
|
||||
slow_query_ms, _ = _get_slow_query_runtime_values()
|
||||
if slow_query_ms <= 0:
|
||||
return
|
||||
if elapsed_ms < DB_SLOW_QUERY_MS:
|
||||
if elapsed_ms < slow_query_ms:
|
||||
return
|
||||
|
||||
params_info = _describe_params(parameters)
|
||||
|
||||
Reference in New Issue
Block a user