diff --git a/plugins/node/opentelemetry-instrumentation-mysql/src/utils.ts b/plugins/node/opentelemetry-instrumentation-mysql/src/utils.ts index 8230052d0e..0d8711e360 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-mysql/src/utils.ts @@ -112,10 +112,13 @@ export function getDbValues( * @returns SQL statement without variable arguments or SQL verb */ export function getSpanName(query: string | Query | QueryOptions): string { - if (typeof query === 'object') { - return query.sql; + const rawQuery = typeof query === 'object' ? query.sql : query; + // Extract the SQL verb + const firstSpace = rawQuery?.indexOf(' '); + if (typeof firstSpace === 'number' && firstSpace !== -1) { + return rawQuery?.substring(0, firstSpace); } - return query.split(' ')[0]; + return rawQuery; } export function arrayStringifyHelper(arr: Array | undefined): string { diff --git a/plugins/node/opentelemetry-instrumentation-mysql/test/index.test.ts b/plugins/node/opentelemetry-instrumentation-mysql/test/index.test.ts index 676081d47e..36b7cd1d5f 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql/test/index.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mysql/test/index.test.ts @@ -153,7 +153,7 @@ describe('mysql@2.x-Tracing', () => { query.on('end', () => { const spans = memoryExporter.getFinishedSpans(); - assert.strictEqual(spans[0].name, sql); + assert.strictEqual(spans[0].name, 'SELECT'); done(); }); }); diff --git a/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts b/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts index cfd9f2ef0f..0a045ff45a 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts @@ -131,7 +131,11 @@ export function getDbStatement( export function getSpanName(query: string | Query | QueryOptions): string { const rawQuery = typeof query === 'object' ? query.sql : query; // Extract the SQL verb - return rawQuery?.split(' ')?.[0]; + const firstSpace = rawQuery?.indexOf(' '); + if (typeof firstSpace === 'number' && firstSpace !== -1) { + return rawQuery?.substring(0, firstSpace); + } + return rawQuery; } export const once = (fn: Function) => {