File tree Expand file tree Collapse file tree 2 files changed +26
-3
lines changed
packages/instrumentation-pg Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -104,11 +104,13 @@ export function getQuerySpanName(
104104}
105105
106106export 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.
Original file line number Diff line number Diff 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 ) ,
You can’t perform that action at this time.
0 commit comments