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(instrumentation-mysql2): instrument connection on 3.11.5 #2579

Merged
17 changes: 9 additions & 8 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 @@ -31,6 +31,7 @@ import type * as mysqlTypes from 'mysql2';
import { MySQL2InstrumentationConfig } from './types';
import {
getConnectionAttributes,
getConnectionPrototypeToInstrument,
getDbStatement,
getSpanName,
once,
Expand All @@ -56,7 +57,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
['>=1.4.2 <4'],
(moduleExports: any) => {
const ConnectionPrototype: mysqlTypes.Connection =
moduleExports.Connection.prototype;
getConnectionPrototypeToInstrument(moduleExports.Connection);
if (isWrapped(ConnectionPrototype.query)) {
this._unwrap(ConnectionPrototype, 'query');
}
Expand Down
19 changes: 19 additions & 0 deletions plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,22 @@
return fn(...args);
};
};

export function getConnectionPrototypeToInstrument(connection: any) {
const connectionPrototype = connection.prototype;
const basePrototype = Object.getPrototypeOf(connectionPrototype);

// [email protected] 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;

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

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts#L166

Added line #L166 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down Expand Up @@ -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);
}
});
});
});
Expand Down
Loading