From 3b2d257baddd935d93c25fb8b1aa877f46348be6 Mon Sep 17 00:00:00 2001 From: Arya Mohanan Date: Tue, 3 Dec 2024 13:19:01 +0530 Subject: [PATCH] fix(mysql2): handled mysql2 v3.11.5 class structure changes --- .../tracing/instrumentation/database/mysql.js | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/core/src/tracing/instrumentation/database/mysql.js b/packages/core/src/tracing/instrumentation/database/mysql.js index 9bac2e00d4..d8bde74f57 100644 --- a/packages/core/src/tracing/instrumentation/database/mysql.js +++ b/packages/core/src/tracing/instrumentation/database/mysql.js @@ -29,9 +29,26 @@ function instrumentMysql(mysql) { } function instrumentMysql2(mysql) { - instrumentConnection(mysql.Connection.prototype, true); - if (mysql.Pool) { - instrumentPool(mysql.Pool.prototype); + /** + * In mysql2 version 3.11.5 and later, the internal structure of the `Connection` and `Pool` classes was reorganized. + * Methods like `query` and `execute` were moved into the `BaseConnection` class located in `lib/base/connection.js`, + * and similar changes were applied to the `Pool` class. See: https://github.com/sidorares/node-mysql2/pull/3081 + * + * Prior to v3.11.5, the `Connection` and `Pool` prototypes were directly used for instrumentation. + */ + const connectionPrototype = + Object.getPrototypeOf(mysql.Connection.prototype)?.constructor?.name === 'BaseConnection' + ? Object.getPrototypeOf(mysql.Connection.prototype) + : mysql.Connection.prototype; + + const poolPrototype = + mysql.Pool && Object.getPrototypeOf(mysql.Pool.prototype)?.constructor?.name === 'BasePool' + ? Object.getPrototypeOf(mysql.Pool.prototype) + : mysql.Pool?.prototype; + + instrumentConnection(connectionPrototype, true); + if (poolPrototype) { + instrumentPool(poolPrototype); } }