From ebe8be26fc9c11d5f76fc0731b69f1c6b4a018a1 Mon Sep 17 00:00:00 2001 From: Ran Nozik Date: Thu, 24 Feb 2022 08:18:36 +0200 Subject: [PATCH] fix mysql2 span name --- .../opentelemetry-instrumentation-mysql2/src/utils.ts | 7 +++---- .../test/mysql.test.ts | 10 +++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts b/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts index a71560014e..0d42e7dc67 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts @@ -103,10 +103,9 @@ export function getDbStatement( * @returns SQL statement without variable arguments or SQL verb */ export function getSpanName(query: string | Query | QueryOptions): string { - if (typeof query === 'object') { - return query.sql; - } - return query.split(' ')[0]; + const rawQuery = typeof query === 'object' ? query.sql : query; + // Extract the SQL verb + return rawQuery.split(' ')[0]; } export const once = (fn: Function) => { diff --git a/plugins/node/opentelemetry-instrumentation-mysql2/test/mysql.test.ts b/plugins/node/opentelemetry-instrumentation-mysql2/test/mysql.test.ts index f456ba368c..28f1ba8bd0 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql2/test/mysql.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mysql2/test/mysql.test.ts @@ -146,6 +146,10 @@ describe('mysql@2.x', () => { query.on('end', () => { const spans = memoryExporter.getFinishedSpans(); assert.strictEqual(spans[0].name, 'SELECT'); + assert.strictEqual( + spans[0].attributes[SemanticAttributes.DB_STATEMENT], + query + ); done(); }); }); @@ -161,7 +165,11 @@ describe('mysql@2.x', () => { query.on('end', () => { const spans = memoryExporter.getFinishedSpans(); - assert.strictEqual(spans[0].name, sql); + assert.strictEqual(spans[0].name, 'SELECT'); + assert.strictEqual( + spans[0].attributes[SemanticAttributes.DB_STATEMENT], + query + ); done(); }); });