Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@
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') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reminds me of #2522
What would you think about checking the presence of the functions in the base class' prototype?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, let's try that, one sec.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@david-luna doing that now (see latest commits)

return basePrototype;
}

// otherwise instrument the connection directly.
return connectionPrototype;

Check warning on line 161 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#L161

Added line #L161 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