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(instrumentation-mysql2)!: instrumentation should not include values in db.statement #1857

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -39,8 +39,6 @@ import {
/** @knipignore */
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';

type formatType = typeof mysqlTypes.format;

export class MySQL2Instrumentation extends InstrumentationBase<MySQL2InstrumentationConfig> {
static readonly COMMON_ATTRIBUTES = {
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MYSQL,
Expand All @@ -64,7 +62,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
this._wrap(
ConnectionPrototype,
'query',
this._patchQuery(moduleExports.format, false) as any
this._patchQuery(false) as any
);

if (isWrapped(ConnectionPrototype.execute)) {
Expand All @@ -73,7 +71,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
this._wrap(
ConnectionPrototype,
'execute',
this._patchQuery(moduleExports.format, true) as any
this._patchQuery(true) as any
);

return moduleExports;
Expand All @@ -82,14 +80,18 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
if (moduleExports === undefined) return;
const ConnectionPrototype: mysqlTypes.Connection =
moduleExports.Connection.prototype;
this._unwrap(ConnectionPrototype, 'query');
this._unwrap(ConnectionPrototype, 'execute');
if (isWrapped(ConnectionPrototype.query)) {
this._unwrap(ConnectionPrototype, 'query');
}
if (isWrapped(ConnectionPrototype.execute)) {
this._unwrap(ConnectionPrototype, 'execute');
}
Comment on lines +83 to +88
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious, was this added because of your added "initialization" test below? Was that resulting in logger('no original to unwrap to -- has ' + name + ' already been unwrapped?') console errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, correct

}
),
];
}

private _patchQuery(format: formatType, isPrepared: boolean) {
private _patchQuery(isPrepared: boolean) {
return (originalQuery: Function): Function => {
const thisPlugin = this;
return function query(
Expand All @@ -110,7 +112,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
attributes: {
...MySQL2Instrumentation.COMMON_ATTRIBUTES,
...getConnectionAttributes(this.config),
[SEMATTRS_DB_STATEMENT]: getDbStatement(query, format, values),
[SEMATTRS_DB_STATEMENT]: getDbStatement(query, values),
},
});

Expand Down Expand Up @@ -150,11 +152,18 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
);
}
}

span.end();
});

if (arguments.length === 1) {
function findCallback(_arguments: IArguments) {
for (let i = 0; i < _arguments.length; i++) {
if (typeof _arguments[i] === 'function') {
return i;
}
}
return -1;
}
const cb = findCallback(arguments);
if (cb < 0) {
if (typeof (query as any).onResult === 'function') {
thisPlugin._wrap(
query as any,
Expand All @@ -178,22 +187,13 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
});

return streamableQuery;
}

if (typeof arguments[1] === 'function') {
} else {
thisPlugin._wrap(
arguments,
1,
thisPlugin._patchCallbackQuery(endSpan)
);
} else if (typeof arguments[2] === 'function') {
thisPlugin._wrap(
arguments,
2,
cb,
thisPlugin._patchCallbackQuery(endSpan)
);
}

return originalQuery.apply(this, arguments);
};
};
Expand Down
40 changes: 18 additions & 22 deletions plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,14 @@ interface QueryOptions {
values?: any | any[] | { [param: string]: any };
}

interface Query {
sql: string;
}

interface Config {
host?: string;
port?: number;
database?: string;
user?: string;
connectionConfig?: Config;
}

/**
* Get an Attributes map from a mysql connection config object
*
Expand Down Expand Up @@ -97,29 +94,28 @@ function getJDBCString(
}

/**
* Conjures up the value for the db.statement attribute by formatting a SQL query.
*
* @returns the database statement being executed.
* Returns a string representing the SQL query that is appropriate to return
* in telemetry. If there are no `values` then, to be cautious, it is assumed
* the query SQL may include sensitive data and `undefined` is returned, so
* that no DB statement is included in telemetry.
*/
export function getDbStatement(
macno marked this conversation as resolved.
Show resolved Hide resolved
query: string | Query | QueryOptions,
format: (
sql: string,
values: any[],
stringifyObjects?: boolean,
timeZone?: string
) => string,
query: string | QueryOptions,
values?: any[]
): string {
): string | undefined {
let statement = '';
if (typeof query === 'string') {
return values ? format(query, values) : query;
if (!values) {
return;
}
statement = query;
} else {
// According to https://github.com/mysqljs/mysql#performing-queries
// The values argument will override the values in the option object.
return values || (query as QueryOptions).values
? format(query.sql, values || (query as QueryOptions).values)
: query.sql;
if (!query.values && !values) {
return;
}
statement = query.sql;
}
return statement;
}

/**
Expand All @@ -128,7 +124,7 @@ export function getDbStatement(
*
* @returns SQL statement without variable arguments or SQL verb
*/
export function getSpanName(query: string | Query | QueryOptions): string {
export function getSpanName(query: string | QueryOptions): string {
const rawQuery = typeof query === 'object' ? query.sql : query;
// Extract the SQL verb
const firstSpace = rawQuery?.indexOf(' ');
Expand Down
Loading
Loading