Skip to content

Commit 2eb8253

Browse files
committed
fix
1 parent be9e15d commit 2eb8253

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

dev-packages/node-integration-tests/suites/tracing/mysql2/scenario.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ async function run() {
2626
async _ => {
2727
await connection.query('SELECT 1 + 1 AS solution');
2828
await connection.query('SELECT NOW()', ['1', '2']);
29+
// A single, non-array bind value (`query(sql, scalar)`) must still be inlined into `db.statement`.
30+
await connection.query('SELECT ? AS scalar_value', 42);
2931
// `execute` is instrumented the same way as `query`
3032
await connection.execute('SELECT 42 AS answer');
3133
// a failing query should produce a span with an error status

dev-packages/node-integration-tests/suites/tracing/mysql2/test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ describeWithDockerCompose('mysql2 auto instrumentation', { workingDirectory: [__
3838
'db.user': 'root',
3939
}),
4040
}),
41+
// a single non-array bind value is inlined into `db.statement` (not left as a `?` placeholder)
42+
expect.objectContaining({
43+
description: 'SELECT 42 AS scalar_value',
44+
op: 'db',
45+
origin: ORIGIN,
46+
data: expect.objectContaining({
47+
'db.system': 'mysql',
48+
'db.statement': 'SELECT 42 AS scalar_value',
49+
}),
50+
}),
4151
// `execute` is instrumented the same way as `query`
4252
expect.objectContaining({
4353
description: 'SELECT 42 AS answer',

packages/server-utils/src/integrations/tracing-channel/mysql2.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,11 @@ function getQueryText(connection: Mysql2Connection | undefined, args: unknown[])
111111
return undefined;
112112
}
113113

114-
// `query(sql, values, cb)` → values is `args[1]`; `query(sql, cb)` → no values.
115-
const values = Array.isArray(args[1]) ? args[1] : undefined;
114+
// `query(sql, values, cb)` → values is `args[1]`. mysql2 also accepts a single non-array bind value
115+
// (`query(sql, scalar, cb)`); a non-array `args[1]` is only a value when a callback follows it,
116+
// otherwise it is the callback itself (`query(sql, cb)`). Matches `@opentelemetry/instrumentation-mysql2`.
117+
const values = Array.isArray(args[1]) ? args[1] : args[2] !== undefined ? [args[1]] : undefined;
118+
116119
const objectValues =
117120
isObjectLike(args[0]) && 'values' in args[0] ? (args[0] as { values?: unknown }).values : undefined;
118121
const boundValues = values ?? objectValues;

0 commit comments

Comments
 (0)