Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instr-mysql2): Fix mysql2 instrumentation connection prototype #2578

Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@
'mysql2',
['>=1.4.2 <4'],
(moduleExports: any) => {
const ConnectionPrototype: mysqlTypes.Connection =
moduleExports.Connection.prototype;
const ConnectionPrototype =
this._getConnectionPrototype(moduleExports);

if (isWrapped(ConnectionPrototype.query)) {
this._unwrap(ConnectionPrototype, 'query');
}
Expand All @@ -79,8 +80,8 @@
},
(moduleExports: any) => {
if (moduleExports === undefined) return;
const ConnectionPrototype: mysqlTypes.Connection =
moduleExports.Connection.prototype;
const ConnectionPrototype =
this._getConnectionPrototype(moduleExports);
this._unwrap(ConnectionPrototype, 'query');
this._unwrap(ConnectionPrototype, 'execute');
}
Expand Down Expand Up @@ -210,4 +211,17 @@
};
};
}

private _getConnectionPrototype(moduleExports: any): mysqlTypes.Connection {
const baseClass = Object.getPrototypeOf(moduleExports.Connection);

// if `baseClass` has no prototype means connection is not extending
// another class so the functions we need to patch are already there
if (typeof baseClass.prototype === 'undefined') {
Copy link
Member

@pichlermarc pichlermarc Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I think in older versions you get EventEmitter as a base. That may be why the TAV tests are failing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reverted the check to make it more explicit. It will return the base class prototype if it has the query function (execute goes together). Otherwise it falls back to the Connection prototype.

Note: I had trouble running it locally since docker image mysql:5.7 has no support for linux/arm64/v8 platform. Is it possible to update the image version in @opentelemetry/contrib-test-utils?

return moduleExports.Connection.prototype;

Check warning on line 221 in plugins/node/opentelemetry-instrumentation-mysql2/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-mysql2/src/instrumentation.ts#L221

Added line #L221 was not covered by tests
}

// return the BaseConnection prototype (mysql2@3.11.5+)
return baseClass.prototype;
}
}
Loading