Skip to content

Commit

Permalink
fix(instrumentation-mysql2): instrument connection on 3.11.5 (#2579)
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc authored Dec 4, 2024
1 parent 8bfa21f commit e674b1b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
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 @@ 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;
}
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

0 comments on commit e674b1b

Please sign in to comment.