diff --git a/package-lock.json b/package-lock.json index f50df50692..976a703a94 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26284,9 +26284,9 @@ } }, "node_modules/mysql2": { - "version": "3.11.3", - "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.11.3.tgz", - "integrity": "sha512-Qpu2ADfbKzyLdwC/5d4W7+5Yz7yBzCU05YWt5npWzACST37wJsB23wgOSo00qi043urkiRwXtEvJc9UnuLX/MQ==", + "version": "3.11.5", + "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.11.5.tgz", + "integrity": "sha512-0XFu8rUmFN9vC0ME36iBvCUObftiMHItrYFhlCRvFWbLgpNqtC4Br/NmZX1HNCszxT0GGy5QtP+k3Q3eCJPaYA==", "dev": true, "license": "MIT", "dependencies": { @@ -26309,6 +26309,7 @@ "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", "dev": true, + "license": "MIT", "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" }, @@ -37853,7 +37854,7 @@ "@types/mocha": "7.0.2", "@types/node": "18.18.14", "@types/semver": "7.5.8", - "mysql2": "3.11.3", + "mysql2": "3.11.5", "nyc": "15.1.0", "rimraf": "5.0.10", "semver": "7.6.3", @@ -48554,7 +48555,7 @@ "@types/mocha": "7.0.2", "@types/node": "18.18.14", "@types/semver": "7.5.8", - "mysql2": "3.11.3", + "mysql2": "3.11.5", "nyc": "15.1.0", "rimraf": "5.0.10", "semver": "7.6.3", @@ -63019,9 +63020,9 @@ } }, "mysql2": { - "version": "3.11.3", - "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.11.3.tgz", - "integrity": "sha512-Qpu2ADfbKzyLdwC/5d4W7+5Yz7yBzCU05YWt5npWzACST37wJsB23wgOSo00qi043urkiRwXtEvJc9UnuLX/MQ==", + "version": "3.11.5", + "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.11.5.tgz", + "integrity": "sha512-0XFu8rUmFN9vC0ME36iBvCUObftiMHItrYFhlCRvFWbLgpNqtC4Br/NmZX1HNCszxT0GGy5QtP+k3Q3eCJPaYA==", "dev": true, "requires": { "aws-ssl-profiles": "^1.1.1", diff --git a/plugins/node/opentelemetry-instrumentation-mysql2/package.json b/plugins/node/opentelemetry-instrumentation-mysql2/package.json index dcf3806e56..20de228c25 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql2/package.json +++ b/plugins/node/opentelemetry-instrumentation-mysql2/package.json @@ -50,7 +50,7 @@ "@types/mocha": "7.0.2", "@types/node": "18.18.14", "@types/semver": "7.5.8", - "mysql2": "3.11.3", + "mysql2": "3.11.5", "nyc": "15.1.0", "rimraf": "5.0.10", "semver": "7.6.3", diff --git a/plugins/node/opentelemetry-instrumentation-mysql2/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-mysql2/src/instrumentation.ts index 7557cf7a01..8c90080980 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql2/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-mysql2/src/instrumentation.ts @@ -31,6 +31,7 @@ import type * as mysqlTypes from 'mysql2'; import { MySQL2InstrumentationConfig } from './types'; import { getConnectionAttributes, + getConnectionPrototypeToInstrument, getDbStatement, getSpanName, once, @@ -56,7 +57,7 @@ export class MySQL2Instrumentation extends InstrumentationBase=1.4.2 <4'], (moduleExports: any) => { const ConnectionPrototype: mysqlTypes.Connection = - moduleExports.Connection.prototype; + getConnectionPrototypeToInstrument(moduleExports.Connection); if (isWrapped(ConnectionPrototype.query)) { this._unwrap(ConnectionPrototype, 'query'); } diff --git a/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts b/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts index 0a045ff45a..bb0fb763d3 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts @@ -146,3 +146,22 @@ export const once = (fn: Function) => { return fn(...args); }; }; + +export function getConnectionPrototypeToInstrument(connection: any) { + const connectionPrototype = connection.prototype; + const basePrototype = Object.getPrototypeOf(connectionPrototype); + + // mysql2@3.11.5 included a refactoring, where most code was moved out of the `Connection` class and into a shared base + // so we need to instrument that instead, see https://github.com/sidorares/node-mysql2/pull/3081 + // This checks if the functions we're instrumenting are there on the base - we cannot use the presence of a base + // prototype since EventEmitter is the base for mysql2@<=3.11.4 + if ( + typeof basePrototype?.query === 'function' && + typeof basePrototype?.execute === 'function' + ) { + return basePrototype; + } + + // otherwise instrument the connection directly. + return connectionPrototype; +} diff --git a/plugins/node/opentelemetry-instrumentation-mysql2/test/mysql.test.ts b/plugins/node/opentelemetry-instrumentation-mysql2/test/mysql.test.ts index a11480418c..ef14d7d982 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql2/test/mysql.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mysql2/test/mysql.test.ts @@ -223,10 +223,14 @@ describe('mysql2', () => { }); query.on('end', () => { - assert.strictEqual(rows, 1); - const spans = memoryExporter.getFinishedSpans(); - assert.strictEqual(spans.length, 1); - assertSpan(spans[0], sql); + try { + assert.strictEqual(rows, 1); + const spans = memoryExporter.getFinishedSpans(); + assert.strictEqual(spans.length, 1); + assertSpan(spans[0], sql); + } catch (e) { + done(e); + } done(); }); }); @@ -340,8 +344,12 @@ describe('mysql2', () => { const spans = memoryExporter.getFinishedSpans(); assert.strictEqual(spans.length, 1); getLastQueries(1).then(([query]) => { - assert.doesNotMatch(query, /.*traceparent.*/); - done(); + try { + assert.doesNotMatch(query, /.*traceparent.*/); + done(); + } catch (e) { + done(e); + } }); }); });