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.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8f7a0c5..962083a 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 9adbc5b..bf86ba8 100644
--- a/skywalking/plugins/sw_mysqlclient.py
+++ b/skywalking/plugins/sw_mysqlclient.py
@@ -32,6 +32,9 @@
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):