Skip to content

Commit 1102cfa

Browse files
authored
fix(instrumentation-pg): normalize leading/trailing spaces in database operation name (#3270)
Signed-off-by: Ezequiel Carrizo <[email protected]>
1 parent e0ccefa commit 1102cfa

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

packages/instrumentation-pg/src/utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ export function getQuerySpanName(
104104
}
105105

106106
export function parseNormalizedOperationName(queryText: string) {
107-
const indexOfFirstSpace = queryText.indexOf(' ');
107+
// Trim the query text to handle leading/trailing whitespace
108+
const trimmedQuery = queryText.trim();
109+
const indexOfFirstSpace = trimmedQuery.indexOf(' ');
108110
let sqlCommand =
109111
indexOfFirstSpace === -1
110-
? queryText
111-
: queryText.slice(0, indexOfFirstSpace);
112+
? trimmedQuery
113+
: trimmedQuery.slice(0, indexOfFirstSpace);
112114
sqlCommand = sqlCommand.toUpperCase();
113115

114116
// Handle query text being "COMMIT;", which has an extra semicolon before the space.

packages/instrumentation-pg/test/utils.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,27 @@ describe('utils.ts', () => {
108108
);
109109
});
110110

111+
it('remove leading whitespaces when parsing operation names', () => {
112+
assert.strictEqual(
113+
utils.getQuerySpanName('dbName', { text: ' SELECT $1' }),
114+
'pg.query:SELECT dbName'
115+
);
116+
});
117+
118+
it('remove trailing whitespaces when parsing operation names', () => {
119+
assert.strictEqual(
120+
utils.getQuerySpanName('dbName', { text: 'SELECT $1 ' }),
121+
'pg.query:SELECT dbName'
122+
);
123+
});
124+
125+
it('remove leading and trailing whitespace when parsing operation names', () => {
126+
assert.strictEqual(
127+
utils.getQuerySpanName('dbName', { text: ' SELECT $1 ' }),
128+
'pg.query:SELECT dbName'
129+
);
130+
});
131+
111132
it('omits db name if missing', () => {
112133
assert.strictEqual(
113134
utils.getQuerySpanName(undefined, dummyQuery),

0 commit comments

Comments
 (0)