Skip to content

Commit 7fe5964

Browse files
committed
fix(instrumentation-mysql2): properly instrument 3.11.5
1 parent 5eb61d8 commit 7fe5964

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { addSqlCommenterComment } from '@opentelemetry/sql-common';
3030
import type * as mysqlTypes from 'mysql2';
3131
import { MySQL2InstrumentationConfig } from './types';
3232
import {
33-
getConnectionAttributes,
33+
getConnectionAttributes, getConnectionPrototypeToInstrument,
3434
getDbStatement,
3535
getSpanName,
3636
once,
@@ -55,8 +55,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
5555
'mysql2',
5656
['>=1.4.2 <4'],
5757
(moduleExports: any) => {
58-
const ConnectionPrototype: mysqlTypes.Connection =
59-
moduleExports.Connection.prototype;
58+
const ConnectionPrototype: mysqlTypes.Connection = getConnectionPrototypeToInstrument(moduleExports.Connection);
6059
if (isWrapped(ConnectionPrototype.query)) {
6160
this._unwrap(ConnectionPrototype, 'query');
6261
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,17 @@ 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+
if(basePrototype?.constructor?.name == 'BaseConnection'){
157+
return basePrototype;
158+
}
159+
160+
// otherwise instrument the connection directly.
161+
return connectionPrototype;
162+
}

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import * as assert from 'assert';
3737
import { MySQL2Instrumentation, MySQL2InstrumentationConfig } from '../src';
3838

3939
const LIB_VERSION = testUtils.getPackageVersion('mysql2');
40-
const port = Number(process.env.MYSQL_PORT) || 33306;
40+
const port = Number(process.env.MYSQL_PORT) || 3306;
4141
const database = process.env.MYSQL_DATABASE || 'test_db';
4242
const host = process.env.MYSQL_HOST || '127.0.0.1';
4343
const user = process.env.MYSQL_USER || 'otel';
@@ -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)