Skip to content

Commit a10beca

Browse files
committed
preserve quotes
1 parent 0416062 commit a10beca

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

instrumentation-api-incubator/src/main/jflex/SqlSanitizer.jflex

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ WHITESPACE = [ \t\r\n]+
238238
boolean handleIdentifier() {
239239
if (shouldHandleIdentifier()) {
240240
mainIdentifier = readIdentifierName();
241-
appendTargetToSummary(mainIdentifier);
241+
// Use yytext() to preserve quotes in query summary per semantic conventions
242+
appendTargetToSummary(yytext());
242243
}
243244
return true;
244245
}
@@ -303,8 +304,8 @@ WHITESPACE = [ \t\r\n]+
303304
++identifiersAfterComma;
304305
// First identifier after comma is the table name - add it to summary
305306
if (identifiersAfterComma == 1) {
306-
String tableName = readIdentifierName();
307-
appendTargetToSummary(tableName);
307+
// Use yytext() to preserve quotes in query summary per semantic conventions
308+
appendTargetToSummary(yytext());
308309
}
309310
return false;
310311
}
@@ -326,7 +327,8 @@ WHITESPACE = [ \t\r\n]+
326327
}
327328

328329
mainIdentifier = readIdentifierName();
329-
appendTargetToSummary(mainIdentifier);
330+
// Use yytext() to preserve quotes in query summary per semantic conventions
331+
appendTargetToSummary(yytext());
330332
mainTableSetAlready = true;
331333
expectingTableName = false;
332334
// start counting identifiers after encountering main from clause
@@ -370,7 +372,8 @@ WHITESPACE = [ \t\r\n]+
370372
}
371373

372374
mainIdentifier = readIdentifierName();
373-
appendTargetToSummary(mainIdentifier);
375+
// Use yytext() to preserve quotes in query summary per semantic conventions
376+
appendTargetToSummary(yytext());
374377
return true;
375378
}
376379
}
@@ -389,23 +392,26 @@ WHITESPACE = [ \t\r\n]+
389392
}
390393

391394
mainIdentifier = readIdentifierName();
392-
appendTargetToSummary(mainIdentifier);
395+
// Use yytext() to preserve quotes in query summary per semantic conventions
396+
appendTargetToSummary(yytext());
393397
return true;
394398
}
395399
}
396400

397401
private class Update extends Operation {
398402
boolean handleIdentifier() {
399403
mainIdentifier = readIdentifierName();
400-
appendTargetToSummary(mainIdentifier);
404+
// Use yytext() to preserve quotes in query summary per semantic conventions
405+
appendTargetToSummary(yytext());
401406
return true;
402407
}
403408
}
404409

405410
private class Call extends Operation {
406411
boolean handleIdentifier() {
407412
mainIdentifier = readIdentifierName();
408-
appendTargetToSummary(mainIdentifier);
413+
// Use yytext() to preserve quotes in query summary per semantic conventions
414+
appendTargetToSummary(yytext());
409415
return true;
410416
}
411417

@@ -418,7 +424,8 @@ WHITESPACE = [ \t\r\n]+
418424
private class Merge extends Operation {
419425
boolean handleIdentifier() {
420426
mainIdentifier = readIdentifierName();
421-
appendTargetToSummary(mainIdentifier);
427+
// Use yytext() to preserve quotes in query summary per semantic conventions
428+
appendTargetToSummary(yytext());
422429
return true;
423430
}
424431
}

instrumentation-api-incubator/src/test/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlStatementSanitizerTest.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,13 @@ private static Stream<Arguments> simplifyArgs() {
306306
expect("SELECT", "schema.table", "SELECT schema.table")),
307307
Arguments.of(
308308
"SELECT x, y, z FROM `schema table`",
309-
expect("SELECT", "schema table", "SELECT schema table")),
309+
expect("SELECT", "schema table", "SELECT `schema table`")),
310310
Arguments.of(
311311
"SELECT x, y, z FROM `schema`.`table`",
312312
expect("SELECT", "`schema`.`table`", "SELECT `schema`.`table`")),
313313
Arguments.of(
314314
"SELECT x, y, z FROM \"schema table\"",
315-
expect("SELECT", "schema table", "SELECT schema table")),
315+
expect("SELECT", "schema table", "SELECT \"schema table\"")),
316316
Arguments.of(
317317
"SELECT x, y, z FROM \"schema\".\"table\"",
318318
expect("SELECT", "\"schema\".\"table\"", "SELECT \"schema\".\"table\"")),
@@ -392,10 +392,10 @@ private static Stream<Arguments> simplifyArgs() {
392392
Arguments.of(
393393
"insert into db.table where lalala", expect("INSERT", "db.table", "INSERT db.table")),
394394
Arguments.of(
395-
"insert into `db table` where lalala", expect("INSERT", "db table", "INSERT db table")),
395+
"insert into `db table` where lalala", expect("INSERT", "db table", "INSERT `db table`")),
396396
Arguments.of(
397397
"insert into \"db table\" where lalala",
398-
expect("INSERT", "db table", "INSERT db table")),
398+
expect("INSERT", "db table", "INSERT \"db table\"")),
399399
Arguments.of("insert without i-n-t-o", expect("INSERT", null, "INSERT")),
400400

401401
// Delete
@@ -404,10 +404,10 @@ private static Stream<Arguments> simplifyArgs() {
404404
expect("DELETE", "table", "DELETE table")),
405405
Arguments.of(
406406
"delete from `my table` where something something",
407-
expect("DELETE", "my table", "DELETE my table")),
407+
expect("DELETE", "my table", "DELETE `my table`")),
408408
Arguments.of(
409409
"delete from \"my table\" where something something",
410-
expect("DELETE", "my table", "DELETE my table")),
410+
expect("DELETE", "my table", "DELETE \"my table\"")),
411411
Arguments.of(
412412
"delete from foo where x IN (1,2,3)",
413413
expect("delete from foo where x IN (?)", "DELETE", "foo", "DELETE foo")),
@@ -420,17 +420,17 @@ private static Stream<Arguments> simplifyArgs() {
420420
expect("update table set answer=?", "UPDATE", "table", "UPDATE table")),
421421
Arguments.of(
422422
"update `my table` set answer=42",
423-
expect("update `my table` set answer=?", "UPDATE", "my table", "UPDATE my table")),
423+
expect("update `my table` set answer=?", "UPDATE", "my table", "UPDATE `my table`")),
424424
Arguments.of(
425425
"update `my table` set answer=42 where x IN('a', 'b') AND y In ('a', 'b')",
426426
expect(
427427
"update `my table` set answer=? where x IN(?) AND y In (?)",
428428
"UPDATE",
429429
"my table",
430-
"UPDATE my table")),
430+
"UPDATE `my table`")),
431431
Arguments.of(
432432
"update \"my table\" set answer=42",
433-
expect("update \"my table\" set answer=?", "UPDATE", "my table", "UPDATE my table")),
433+
expect("update \"my table\" set answer=?", "UPDATE", "my table", "UPDATE \"my table\"")),
434434
Arguments.of("update /*table", expect("UPDATE", null, "UPDATE")),
435435

436436
// Call
@@ -441,8 +441,8 @@ private static Stream<Arguments> simplifyArgs() {
441441

442442
// Merge
443443
Arguments.of("merge into table", expect("MERGE", "table", "MERGE table")),
444-
Arguments.of("merge into `my table`", expect("MERGE", "my table", "MERGE my table")),
445-
Arguments.of("merge into \"my table\"", expect("MERGE", "my table", "MERGE my table")),
444+
Arguments.of("merge into `my table`", expect("MERGE", "my table", "MERGE `my table`")),
445+
Arguments.of("merge into \"my table\"", expect("MERGE", "my table", "MERGE \"my table\"")),
446446
Arguments.of(
447447
"merge table (into is optional in some dbs)", expect("MERGE", "table", "MERGE table")),
448448
Arguments.of("merge (into )))", expect("MERGE", null, "MERGE")),
@@ -455,11 +455,12 @@ private static Stream<Arguments> simplifyArgs() {
455455

456456
private static Stream<Arguments> ddlArgs() {
457457
return Stream.of(
458-
Arguments.of("CREATE TABLE `table`", expect("CREATE TABLE", "table", "CREATE TABLE table")),
458+
Arguments.of(
459+
"CREATE TABLE `table`", expect("CREATE TABLE", "table", "CREATE TABLE `table`")),
459460
Arguments.of(
460461
"CREATE TABLE IF NOT EXISTS table",
461462
expect("CREATE TABLE", "table", "CREATE TABLE table")),
462-
Arguments.of("DROP TABLE `if`", expect("DROP TABLE", "if", "DROP TABLE if")),
463+
Arguments.of("DROP TABLE `if`", expect("DROP TABLE", "if", "DROP TABLE `if`")),
463464
Arguments.of(
464465
"ALTER TABLE table ADD CONSTRAINT c FOREIGN KEY (foreign_id) REFERENCES ref (id)",
465466
expect("ALTER TABLE", "table", "ALTER TABLE table")),

0 commit comments

Comments
 (0)