sycommon-python-lib 0.1.24__py3-none-any.whl → 0.1.25__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of sycommon-python-lib might be problematic. Click here for more details.
- sycommon/database/database_service.py +6 -1
- sycommon/logging/kafka_log.py +1 -1
- sycommon/logging/sql_logger.py +59 -0
- {sycommon_python_lib-0.1.24.dist-info → sycommon_python_lib-0.1.25.dist-info}/METADATA +1 -1
- {sycommon_python_lib-0.1.24.dist-info → sycommon_python_lib-0.1.25.dist-info}/RECORD +8 -7
- {sycommon_python_lib-0.1.24.dist-info → sycommon_python_lib-0.1.25.dist-info}/WHEEL +0 -0
- {sycommon_python_lib-0.1.24.dist-info → sycommon_python_lib-0.1.25.dist-info}/entry_points.txt +0 -0
- {sycommon_python_lib-0.1.24.dist-info → sycommon_python_lib-0.1.25.dist-info}/top_level.txt +0 -0
|
@@ -3,6 +3,7 @@ from sqlalchemy import create_engine, text
|
|
|
3
3
|
from sycommon.config.Config import SingletonMeta
|
|
4
4
|
from sycommon.config.DatabaseConfig import DatabaseConfig, convert_dict_keys
|
|
5
5
|
from sycommon.logging.kafka_log import SYLogger
|
|
6
|
+
from sycommon.logging.sql_logger import SQLTraceLogger
|
|
6
7
|
from sycommon.synacos.nacos_service import NacosService
|
|
7
8
|
|
|
8
9
|
|
|
@@ -62,9 +63,13 @@ class DatabaseConnector(metaclass=SingletonMeta):
|
|
|
62
63
|
max_overflow=20, # 最大溢出连接数
|
|
63
64
|
pool_timeout=30, # 连接超时时间(秒)
|
|
64
65
|
pool_recycle=3600, # 连接回收时间(秒)
|
|
65
|
-
pool_pre_ping=True # 每次获取连接前检查连接是否有效
|
|
66
|
+
pool_pre_ping=True, # 每次获取连接前检查连接是否有效
|
|
67
|
+
echo=False, # 打印 SQL 语句
|
|
66
68
|
)
|
|
67
69
|
|
|
70
|
+
# 注册 SQL 日志拦截器
|
|
71
|
+
SQLTraceLogger.setup_sql_logging(self.engine)
|
|
72
|
+
|
|
68
73
|
# 测试
|
|
69
74
|
if not self.test_connection():
|
|
70
75
|
raise Exception("Database connection test failed")
|
sycommon/logging/kafka_log.py
CHANGED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
from sqlalchemy import event
|
|
2
|
+
from sqlalchemy.engine import Engine
|
|
3
|
+
from sycommon.logging.kafka_log import SYLogger
|
|
4
|
+
import time
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
from decimal import Decimal
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SQLTraceLogger:
|
|
10
|
+
@staticmethod
|
|
11
|
+
def setup_sql_logging(engine: Engine):
|
|
12
|
+
"""为 SQLAlchemy 引擎注册事件监听器,绑定 trace_id 到 SQL 日志"""
|
|
13
|
+
def serialize_params(params):
|
|
14
|
+
"""处理特殊类型参数的序列化"""
|
|
15
|
+
if isinstance(params, (list, tuple)):
|
|
16
|
+
return [SQLTraceLogger.serialize_params(p) for p in params]
|
|
17
|
+
elif isinstance(params, dict):
|
|
18
|
+
return {k: SQLTraceLogger.serialize_params(v) for k, v in params.items()}
|
|
19
|
+
elif isinstance(params, datetime):
|
|
20
|
+
return params.isoformat()
|
|
21
|
+
elif isinstance(params, Decimal):
|
|
22
|
+
return float(params)
|
|
23
|
+
else:
|
|
24
|
+
return params
|
|
25
|
+
|
|
26
|
+
# 监听 SQL 语句执行后事件(计算耗时并输出日志)
|
|
27
|
+
@event.listens_for(Engine, "after_cursor_execute")
|
|
28
|
+
def after_cursor_execute(
|
|
29
|
+
conn, cursor, statement, parameters, context, executemany
|
|
30
|
+
):
|
|
31
|
+
try:
|
|
32
|
+
# 从连接的 execution_options 中获取开始时间
|
|
33
|
+
start_time = conn._execution_options.get(
|
|
34
|
+
"_start_time", time.time())
|
|
35
|
+
execution_time = (time.time() - start_time) * 1000
|
|
36
|
+
|
|
37
|
+
# 构建 SQL 日志信息
|
|
38
|
+
sql_log = {
|
|
39
|
+
"type": "SQL",
|
|
40
|
+
"statement": statement,
|
|
41
|
+
"parameters": serialize_params(parameters),
|
|
42
|
+
"execution_time_ms": round(execution_time, 2),
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
SYLogger.info(f"SQL 执行: {sql_log}")
|
|
46
|
+
except Exception as e:
|
|
47
|
+
SYLogger.error(f"SQL 日志处理失败: {str(e)}")
|
|
48
|
+
|
|
49
|
+
# 监听 SQL 执行开始事件(记录开始时间到连接的 execution_options)
|
|
50
|
+
@event.listens_for(Engine, "before_cursor_execute")
|
|
51
|
+
def before_cursor_execute(
|
|
52
|
+
conn, cursor, statement, parameters, context, executemany
|
|
53
|
+
):
|
|
54
|
+
try:
|
|
55
|
+
# 通过连接对象的 execution_options 设置开始时间
|
|
56
|
+
# 这里会创建一个新的执行选项副本,避免修改不可变对象
|
|
57
|
+
conn = conn.execution_options(_start_time=time.time())
|
|
58
|
+
except Exception as e:
|
|
59
|
+
SYLogger.error(f"SQL 开始时间记录失败: {str(e)}")
|
|
@@ -9,14 +9,15 @@ sycommon/config/MQConfig.py,sha256=_RDcmIdyWKjmgM5ZnriOoI-DpaxgXs7CD0awdAD6z88,2
|
|
|
9
9
|
sycommon/config/RerankerConfig.py,sha256=dohekaY_eTynmMimIuKHBYGXXQO6rJjSkm94OPLuMik,322
|
|
10
10
|
sycommon/config/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
11
|
sycommon/database/base_db_service.py,sha256=J5ELHMNeGfzA6zVcASPSPZ0XNKrRY3_gdGmVkZw3Mto,946
|
|
12
|
-
sycommon/database/database_service.py,sha256=
|
|
12
|
+
sycommon/database/database_service.py,sha256=mun5vgM7nkuH6_UyHLHqQ2Qk_5gRgMxJu4_obIKLT6o,3253
|
|
13
13
|
sycommon/health/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
14
14
|
sycommon/health/health_check.py,sha256=EhfbhspRpQiKJaxdtE-PzpKQO_ucaFKtQxIm16F5Mpk,391
|
|
15
15
|
sycommon/health/metrics.py,sha256=fHqO73JuhoZkNPR-xIlxieXiTCvttq-kG-tvxag1s1s,268
|
|
16
16
|
sycommon/health/ping.py,sha256=FTlnIKk5y1mPfS1ZGOeT5IM_2udF5aqVLubEtuBp18M,250
|
|
17
17
|
sycommon/logging/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
18
|
-
sycommon/logging/kafka_log.py,sha256=
|
|
18
|
+
sycommon/logging/kafka_log.py,sha256=3HjBg3WoDd3DmjtAlpyLcAVTKI-AHKrKlKmmPerq9tU,20751
|
|
19
19
|
sycommon/logging/logger_wrapper.py,sha256=TiHsrIIHiQMzXgXK12-0KIpU9GhwQJOoHslakzmq2zc,357
|
|
20
|
+
sycommon/logging/sql_logger.py,sha256=sNfWnlqwKGDCSYNktBao03k_Z0o_D4xplMokBqXeh8E,2541
|
|
20
21
|
sycommon/middleware/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
21
22
|
sycommon/middleware/context.py,sha256=_5ghpv4u_yAvjkgM-XDx8OnO-YI1XtntHrXsHJHZkwo,88
|
|
22
23
|
sycommon/middleware/cors.py,sha256=0B5d_ovD56wcH9TfktRs88Q09R9f8Xx5h5ALWYvE8Iw,600
|
|
@@ -47,8 +48,8 @@ sycommon/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
|
47
48
|
sycommon/tools/docs.py,sha256=OPj2ETheuWjXLyaXtaZPbwmJKfJaYXV5s4XMVAUNrms,1607
|
|
48
49
|
sycommon/tools/snowflake.py,sha256=DdEj3T5r5OEvikp3puxqmmmz6BrggxomoSlnsRFb5dM,1174
|
|
49
50
|
sycommon/tools/timing.py,sha256=OiiE7P07lRoMzX9kzb8sZU9cDb0zNnqIlY5pWqHcnkY,2064
|
|
50
|
-
sycommon_python_lib-0.1.
|
|
51
|
-
sycommon_python_lib-0.1.
|
|
52
|
-
sycommon_python_lib-0.1.
|
|
53
|
-
sycommon_python_lib-0.1.
|
|
54
|
-
sycommon_python_lib-0.1.
|
|
51
|
+
sycommon_python_lib-0.1.25.dist-info/METADATA,sha256=xNQjkWiZ4PpKIk8UqDa09HVjYyX4W0oWg_4OcMcPD3A,7038
|
|
52
|
+
sycommon_python_lib-0.1.25.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
53
|
+
sycommon_python_lib-0.1.25.dist-info/entry_points.txt,sha256=q_h2nbvhhmdnsOUZEIwpuoDjaNfBF9XqppDEmQn9d_A,46
|
|
54
|
+
sycommon_python_lib-0.1.25.dist-info/top_level.txt,sha256=98CJ-cyM2WIKxLz-Pf0AitWLhJyrfXvyY8slwjTXNuc,17
|
|
55
|
+
sycommon_python_lib-0.1.25.dist-info/RECORD,,
|
|
File without changes
|
{sycommon_python_lib-0.1.24.dist-info → sycommon_python_lib-0.1.25.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
|
File without changes
|