Skip to content

Commit 497024f

Browse files
committed
fix(instrumentation-mysql2): do not include parameterized values to db.statement span attribute (#1758)
1 parent 5eb61d8 commit 497024f

File tree

3 files changed

+129
-94
lines changed

3 files changed

+129
-94
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ import {
3838
/** @knipignore */
3939
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
4040

41-
type formatType = typeof mysqlTypes.format;
42-
4341
export class MySQL2Instrumentation extends InstrumentationBase<MySQL2InstrumentationConfig> {
4442
static readonly COMMON_ATTRIBUTES = {
4543
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MYSQL,
@@ -63,7 +61,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
6361
this._wrap(
6462
ConnectionPrototype,
6563
'query',
66-
this._patchQuery(moduleExports.format, false) as any
64+
this._patchQuery(false) as any
6765
);
6866

6967
if (isWrapped(ConnectionPrototype.execute)) {
@@ -72,7 +70,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
7270
this._wrap(
7371
ConnectionPrototype,
7472
'execute',
75-
this._patchQuery(moduleExports.format, true) as any
73+
this._patchQuery(true) as any
7674
);
7775

7876
return moduleExports;
@@ -88,7 +86,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
8886
];
8987
}
9088

91-
private _patchQuery(format: formatType, isPrepared: boolean) {
89+
private _patchQuery(isPrepared: boolean) {
9290
return (originalQuery: Function): Function => {
9391
const thisPlugin = this;
9492
return function query(
@@ -109,7 +107,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
109107
attributes: {
110108
...MySQL2Instrumentation.COMMON_ATTRIBUTES,
111109
...getConnectionAttributes(this.config),
112-
[SEMATTRS_DB_STATEMENT]: getDbStatement(query, format, values),
110+
[SEMATTRS_DB_STATEMENT]: getDbStatement(query, values),
113111
},
114112
});
115113

@@ -149,11 +147,13 @@ export class MySQL2Instrumentation extends InstrumentationBase<MySQL2Instrumenta
149147
);
150148
}
151149
}
152-
153150
span.end();
154151
});
155152

156-
if (arguments.length === 1) {
153+
if (
154+
arguments.length === 1 ||
155+
(arguments.length === 2 && typeof arguments[1] !== 'function')
156+
) {
157157
if (typeof (query as any).onResult === 'function') {
158158
thisPlugin._wrap(
159159
query as any,

plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,14 @@ interface QueryOptions {
3636
values?: any | any[] | { [param: string]: any };
3737
}
3838

39-
interface Query {
40-
sql: string;
41-
}
42-
4339
interface Config {
4440
host?: string;
4541
port?: number;
4642
database?: string;
4743
user?: string;
4844
connectionConfig?: Config;
4945
}
46+
5047
/**
5148
* Get an Attributes map from a mysql connection config object
5249
*
@@ -102,24 +99,24 @@ function getJDBCString(
10299
* @returns the database statement being executed.
103100
*/
104101
export function getDbStatement(
105-
query: string | Query | QueryOptions,
106-
format: (
107-
sql: string,
108-
values: any[],
109-
stringifyObjects?: boolean,
110-
timeZone?: string
111-
) => string,
102+
query: string | QueryOptions,
112103
values?: any[]
113-
): string {
104+
): string | undefined {
105+
let statement = '';
114106
if (typeof query === 'string') {
115-
return values ? format(query, values) : query;
107+
if (!values) {
108+
return;
109+
}
110+
statement = query;
116111
} else {
112+
if (!query.values && !values) {
113+
return;
114+
}
117115
// According to https://github.com/mysqljs/mysql#performing-queries
118116
// The values argument will override the values in the option object.
119-
return values || (query as QueryOptions).values
120-
? format(query.sql, values || (query as QueryOptions).values)
121-
: query.sql;
117+
statement = query.sql;
122118
}
119+
return statement;
123120
}
124121

125122
/**
@@ -128,7 +125,7 @@ export function getDbStatement(
128125
*
129126
* @returns SQL statement without variable arguments or SQL verb
130127
*/
131-
export function getSpanName(query: string | Query | QueryOptions): string {
128+
export function getSpanName(query: string | QueryOptions): string {
132129
const rawQuery = typeof query === 'object' ? query.sql : query;
133130
// Extract the SQL verb
134131
const firstSpace = rawQuery?.indexOf(' ');

0 commit comments

Comments
 (0)