Skip to content

Commit

Permalink
refactor(instrumentation-mysql2): improve performance of getSpanName …
Browse files Browse the repository at this point in the history
…using substring (#2470)
  • Loading branch information
geotry authored Oct 23, 2024
1 parent d056d21 commit 25e53d6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
9 changes: 6 additions & 3 deletions plugins/node/opentelemetry-instrumentation-mysql/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown> | undefined): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('[email protected]', () => {

query.on('end', () => {
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans[0].name, sql);
assert.strictEqual(spans[0].name, 'SELECT');
done();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 25e53d6

Please sign in to comment.