Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing telemetry in mysql2 when importing only promise API #2662

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
InstrumentationNodeModuleFile,
isWrapped,
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
Expand All @@ -41,6 +42,8 @@

type formatType = typeof mysqlTypes.format;

const supportedVersions = ['>=1.4.2 <4'];

export class MySQL2Instrumentation extends InstrumentationBase<MySQL2InstrumentationConfig> {
static readonly COMMON_ATTRIBUTES = {
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MYSQL,
Expand All @@ -51,45 +54,75 @@
}

protected init() {
let format: formatType | undefined;
function setFormatFunction(moduleExports: any) {
if (!format && moduleExports.format) {
format = moduleExports.format;
}
}
const patch = (ConnectionPrototype: mysqlTypes.Connection) => {
if (isWrapped(ConnectionPrototype.query)) {
this._unwrap(ConnectionPrototype, 'query');

Check warning on line 65 in plugins/node/opentelemetry-instrumentation-mysql2/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L65 was not covered by tests
}
this._wrap(
ConnectionPrototype,
'query',
this._patchQuery(format, false) as any
);
if (isWrapped(ConnectionPrototype.execute)) {
this._unwrap(ConnectionPrototype, 'execute');

Check warning on line 73 in plugins/node/opentelemetry-instrumentation-mysql2/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L73 was not covered by tests
}
this._wrap(
ConnectionPrototype,
'execute',
this._patchQuery(format, true) as any
);
};
const unpatch = (ConnectionPrototype: mysqlTypes.Connection) => {
this._unwrap(ConnectionPrototype, 'query');
this._unwrap(ConnectionPrototype, 'execute');
};
return [
new InstrumentationNodeModuleDefinition(
'mysql2',
['>=1.4.2 <4'],
supportedVersions,
(moduleExports: any) => {
const ConnectionPrototype: mysqlTypes.Connection =
getConnectionPrototypeToInstrument(moduleExports.Connection);
if (isWrapped(ConnectionPrototype.query)) {
this._unwrap(ConnectionPrototype, 'query');
}
this._wrap(
ConnectionPrototype,
'query',
this._patchQuery(moduleExports.format, false) as any
);

if (isWrapped(ConnectionPrototype.execute)) {
this._unwrap(ConnectionPrototype, 'execute');
}
this._wrap(
ConnectionPrototype,
'execute',
this._patchQuery(moduleExports.format, true) as any
);

setFormatFunction(moduleExports);
return moduleExports;
},
(moduleExports: any) => {
if (moduleExports === undefined) return;
const ConnectionPrototype: mysqlTypes.Connection =
moduleExports.Connection.prototype;
this._unwrap(ConnectionPrototype, 'query');
this._unwrap(ConnectionPrototype, 'execute');
}
() => {},
[
new InstrumentationNodeModuleFile(
'mysql2/promise.js',
supportedVersions,
(moduleExports: any) => {
setFormatFunction(moduleExports);
return moduleExports;
},
() => {}
),
new InstrumentationNodeModuleFile(
'mysql2/lib/connection.js',
supportedVersions,
(moduleExports: any) => {
const ConnectionPrototype: mysqlTypes.Connection =
getConnectionPrototypeToInstrument(moduleExports);
patch(ConnectionPrototype);
return moduleExports;
},
(moduleExports: any) => {
if (moduleExports === undefined) return;
const ConnectionPrototype: mysqlTypes.Connection =
getConnectionPrototypeToInstrument(moduleExports);
unpatch(ConnectionPrototype);
}
),
]
),
];
}

private _patchQuery(format: formatType, isPrepared: boolean) {
private _patchQuery(format: formatType | undefined, isPrepared: boolean) {
return (originalQuery: Function): Function => {
const thisPlugin = this;
return function query(
Expand Down
13 changes: 7 additions & 6 deletions plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
} from '@opentelemetry/semantic-conventions';
import type * as mysqlTypes from 'mysql2';

type formatType = typeof mysqlTypes.format;

/*
Following types declare an expectation on mysql2 types and define a subset we
Expand Down Expand Up @@ -103,14 +106,12 @@
*/
export function getDbStatement(
query: string | Query | QueryOptions,
format: (
sql: string,
values: any[],
stringifyObjects?: boolean,
timeZone?: string
) => string,
format?: formatType,
values?: any[]
): string {
if (!format) {
return typeof query === 'string' ? query : query.sql;

Check warning on line 113 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#L113

Added line #L113 was not covered by tests
}
if (typeof query === 'string') {
return values ? format(query, values) : query;
} else {
Expand Down
Loading
Loading