From 32fbd3557bd707f73ce3c566228703faacdfdfd7 Mon Sep 17 00:00:00 2001 From: ZEAL Date: Thu, 11 Apr 2024 21:10:26 +0800 Subject: [PATCH] Fix unexpected 'decode' AttributeError when MySQLdb module is mapped by PyMySQL (#336) * Skip if MySQLdb module is mapped by PyMySQL In some use cases, `pymysql.install_as_MySQLdb()` will be used to initialize a 'fake' MySQLdb module, which is actually mapped to pymysql module. Then the sw_mysqlclient plugin will change the data type of `connection.db` from bytes into str, cause the `AttributeError: 'str' object has no attribute 'decode'` exception in sw_pymysql.py line 45, which regard it as bytes. --- CHANGELOG.md | 1 + skywalking/plugins/sw_mysqlclient.py | 3 +++ 2 files changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f7a0c53..962083a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Fix unexpected 'No active span' IllegalStateError (#311) - **Tentative**: Set upper bound <=5.9.5 for psutil package due to test failure. (#326) - Remove `DeprecationWarning` from `pkg_resources` by replace it with `importlib_metadata` (#329) + - Fix unexpected 'decode' AttributeError when MySQLdb module is mapped by PyMySQL (#336) ### 1.0.1 diff --git a/skywalking/plugins/sw_mysqlclient.py b/skywalking/plugins/sw_mysqlclient.py index 9adbc5b3..bf86ba82 100644 --- a/skywalking/plugins/sw_mysqlclient.py +++ b/skywalking/plugins/sw_mysqlclient.py @@ -32,6 +32,9 @@ def install(): import wrapt import MySQLdb + if hasattr(MySQLdb, 'install_as_MySQLdb'): + raise ImportError('SKIP MySQLdb module mapped by PyMySQL', name='MySQLdb', path='') + _connect = MySQLdb.connect def _sw_connect(*args, **kwargs):