Skip to content

Commit

Permalink
fix(instrumentation-mysql2): do not include parameterized values to d…
Browse files Browse the repository at this point in the history
…b.statement span attribute (#1758)
  • Loading branch information
macno committed Nov 18, 2024
1 parent 5eb61d8 commit a2fe940
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,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 @@ -63,7 +61,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 @@ -72,7 +70,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 @@ -88,7 +86,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
];
}

private _patchQuery(format: formatType, isPrepared: boolean) {
private _patchQuery(isPrepared: boolean) {
return (originalQuery: Function): Function => {
const thisPlugin = this;
return function query(
Expand All @@ -109,7 +107,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 @@ -149,11 +147,13 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
);
}
}

span.end();
});

if (arguments.length === 1) {
if (
arguments.length === 1 ||
(arguments.length === 2 && typeof arguments[1] !== 'function')
) {
if (typeof (query as any).onResult === 'function') {
thisPlugin._wrap(
query as any,
Expand Down
31 changes: 14 additions & 17 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 @@ -102,24 +99,24 @@ function getJDBCString(
* @returns the database statement being executed.
*/
export function getDbStatement(
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 {
if (!query.values && !values) {
return;
}
// 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;
statement = query.sql;
}
return statement;
}

/**
Expand All @@ -128,7 +125,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

0 comments on commit a2fe940

Please sign in to comment.