Skip to content

Commit

Permalink
fix(instrumentation-mysql2): properly instrument 3.11.5
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc committed Dec 4, 2024
1 parent 5eb61d8 commit 7fe5964
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 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 @@ -30,7 +30,7 @@ import { addSqlCommenterComment } from '@opentelemetry/sql-common';
import type * as mysqlTypes from 'mysql2';
import { MySQL2InstrumentationConfig } from './types';
import {
getConnectionAttributes,
getConnectionAttributes, getConnectionPrototypeToInstrument,
getDbStatement,
getSpanName,
once,
Expand All @@ -55,8 +55,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
'mysql2',
['>=1.4.2 <4'],
(moduleExports: any) => {
const ConnectionPrototype: mysqlTypes.Connection =
moduleExports.Connection.prototype;
const ConnectionPrototype: mysqlTypes.Connection = getConnectionPrototypeToInstrument(moduleExports.Connection);
if (isWrapped(ConnectionPrototype.query)) {
this._unwrap(ConnectionPrototype, 'query');
}
Expand Down
14 changes: 14 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,17 @@ export const once = (fn: Function) => {
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
if(basePrototype?.constructor?.name == 'BaseConnection'){
return basePrototype;
}

// otherwise instrument the connection directly.
return connectionPrototype;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import * as assert from 'assert';
import { MySQL2Instrumentation, MySQL2InstrumentationConfig } from '../src';

const LIB_VERSION = testUtils.getPackageVersion('mysql2');
const port = Number(process.env.MYSQL_PORT) || 33306;
const port = Number(process.env.MYSQL_PORT) || 3306;
const database = process.env.MYSQL_DATABASE || 'test_db';
const host = process.env.MYSQL_HOST || '127.0.0.1';
const user = process.env.MYSQL_USER || 'otel';
Expand Down 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 7fe5964

Please sign in to comment.