Skip to content

Commit e674b1b

Browse files
authored
fix(instrumentation-mysql2): instrument connection on 3.11.5 (#2579)
1 parent 8bfa21f commit e674b1b

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

package-lock.json

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/node/opentelemetry-instrumentation-mysql2/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"@types/mocha": "7.0.2",
5151
"@types/node": "18.18.14",
5252
"@types/semver": "7.5.8",
53-
"mysql2": "3.11.3",
53+
"mysql2": "3.11.5",
5454
"nyc": "15.1.0",
5555
"rimraf": "5.0.10",
5656
"semver": "7.6.3",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import type * as mysqlTypes from 'mysql2';
3131
import { MySQL2InstrumentationConfig } from './types';
3232
import {
3333
getConnectionAttributes,
34+
getConnectionPrototypeToInstrument,
3435
getDbStatement,
3536
getSpanName,
3637
once,
@@ -56,7 +57,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
5657
['>=1.4.2 <4'],
5758
(moduleExports: any) => {
5859
const ConnectionPrototype: mysqlTypes.Connection =
59-
moduleExports.Connection.prototype;
60+
getConnectionPrototypeToInstrument(moduleExports.Connection);
6061
if (isWrapped(ConnectionPrototype.query)) {
6162
this._unwrap(ConnectionPrototype, 'query');
6263
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,22 @@ export const once = (fn: Function) => {
146146
return fn(...args);
147147
};
148148
};
149+
150+
export function getConnectionPrototypeToInstrument(connection: any) {
151+
const connectionPrototype = connection.prototype;
152+
const basePrototype = Object.getPrototypeOf(connectionPrototype);
153+
154+
// [email protected] included a refactoring, where most code was moved out of the `Connection` class and into a shared base
155+
// so we need to instrument that instead, see https://github.com/sidorares/node-mysql2/pull/3081
156+
// This checks if the functions we're instrumenting are there on the base - we cannot use the presence of a base
157+
// prototype since EventEmitter is the base for mysql2@<=3.11.4
158+
if (
159+
typeof basePrototype?.query === 'function' &&
160+
typeof basePrototype?.execute === 'function'
161+
) {
162+
return basePrototype;
163+
}
164+
165+
// otherwise instrument the connection directly.
166+
return connectionPrototype;
167+
}

plugins/node/opentelemetry-instrumentation-mysql2/test/mysql.test.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,14 @@ describe('mysql2', () => {
223223
});
224224

225225
query.on('end', () => {
226-
assert.strictEqual(rows, 1);
227-
const spans = memoryExporter.getFinishedSpans();
228-
assert.strictEqual(spans.length, 1);
229-
assertSpan(spans[0], sql);
226+
try {
227+
assert.strictEqual(rows, 1);
228+
const spans = memoryExporter.getFinishedSpans();
229+
assert.strictEqual(spans.length, 1);
230+
assertSpan(spans[0], sql);
231+
} catch (e) {
232+
done(e);
233+
}
230234
done();
231235
});
232236
});
@@ -340,8 +344,12 @@ describe('mysql2', () => {
340344
const spans = memoryExporter.getFinishedSpans();
341345
assert.strictEqual(spans.length, 1);
342346
getLastQueries(1).then(([query]) => {
343-
assert.doesNotMatch(query, /.*traceparent.*/);
344-
done();
347+
try {
348+
assert.doesNotMatch(query, /.*traceparent.*/);
349+
done();
350+
} catch (e) {
351+
done(e);
352+
}
345353
});
346354
});
347355
});

0 commit comments

Comments
 (0)