Skip to content

Commit 2b193cd

Browse files
committed
up
1 parent cae5b56 commit 2b193cd

File tree

1 file changed

+126
-117
lines changed

1 file changed

+126
-117
lines changed

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

Lines changed: 126 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void simplifySql(String original, Function<String, SqlStatementInfo> expectedFun
4040
assertThat(result.getQueryText()).isEqualTo(expected.getQueryText());
4141
assertThat(result.getOperationName()).isEqualTo(expected.getOperationName());
4242
assertThat(result.getCollectionName()).isEqualToIgnoringCase(expected.getCollectionName());
43+
assertThat(result.getQuerySummary()).isEqualTo(expected.getQuerySummary());
4344
}
4445

4546
@Test
@@ -69,6 +70,7 @@ void checkDdlOperationStatementsAreOk(
6970
assertThat(result.getQueryText()).isEqualTo(expected.getQueryText());
7071
assertThat(result.getOperationName()).isEqualTo(expected.getOperationName());
7172
assertThat(result.getCollectionName()).isEqualTo(expected.getCollectionName());
73+
assertThat(result.getQuerySummary()).isEqualTo(expected.getQuerySummary());
7274
}
7375

7476
@Test
@@ -158,13 +160,6 @@ public void largeStatementCached() {
158160
assertThat(SqlStatementSanitizer.isCached(largeStatement)).isFalse();
159161
}
160162

161-
@ParameterizedTest
162-
@MethodSource("querySummaryArgs")
163-
void querySummary(String sql, String expectedSummary) {
164-
SqlStatementInfo result = SqlStatementSanitizer.create(true).sanitize(sql);
165-
assertThat(result.getQuerySummary()).isEqualTo(expectedSummary);
166-
}
167-
168163
@Test
169164
void querySummaryIsTruncated() {
170165
// Build a query with many tables to exceed 255 character limit
@@ -183,47 +178,6 @@ void querySummaryIsTruncated() {
183178
assertThat(result).isEqualTo("SELECT very_long_table_name_0");
184179
}
185180

186-
private static Stream<Arguments> querySummaryArgs() {
187-
return Stream.of(
188-
// Basic SELECT
189-
Arguments.of("SELECT * FROM wuser_table", "SELECT wuser_table"),
190-
Arguments.of("SELECT * FROM wuser_table WHERE username = ?", "SELECT wuser_table"),
191-
// INSERT with SELECT subquery - INSERT target + SELECT operation + SELECT target
192-
Arguments.of(
193-
"INSERT INTO shipping_details (order_id, address) SELECT order_id, address FROM orders WHERE order_id = ?",
194-
"INSERT shipping_details SELECT orders"),
195-
// SELECT with multiple tables (implicit join) - only first table tracked since extraction
196-
// stops on comma
197-
Arguments.of(
198-
"SELECT * FROM songs, artists WHERE songs.artist_id == artists.id", "SELECT songs"),
199-
// SELECT with subquery in FROM - outer SELECT + inner SELECT + inner table
200-
Arguments.of(
201-
"SELECT order_date FROM (SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id)",
202-
"SELECT SELECT orders"),
203-
// SELECT with JOIN - first table tracked, extraction stops on JOIN
204-
Arguments.of("SELECT * FROM table1 JOIN table2 ON table1.id = table2.id", "SELECT table1"),
205-
// DELETE
206-
Arguments.of("DELETE FROM users WHERE id = ?", "DELETE users"),
207-
// UPDATE
208-
Arguments.of("UPDATE users SET name = ? WHERE id = ?", "UPDATE users"),
209-
// CALL stored procedure
210-
Arguments.of("CALL some_stored_procedure", "CALL some_stored_procedure"),
211-
// MERGE
212-
Arguments.of("MERGE INTO target USING source ON target.id = source.id", "MERGE target"),
213-
// CREATE TABLE
214-
Arguments.of("CREATE TABLE users (id INT, name VARCHAR(100))", "CREATE TABLE users"),
215-
// DROP TABLE
216-
Arguments.of("DROP TABLE users", "DROP TABLE users"),
217-
// ALTER TABLE
218-
Arguments.of("ALTER TABLE users ADD COLUMN email VARCHAR(100)", "ALTER TABLE users"),
219-
// CREATE INDEX (no table in summary)
220-
Arguments.of("CREATE INDEX idx_name ON users (name)", "CREATE INDEX"),
221-
// Unknown operation
222-
Arguments.of("and now for something completely different", null),
223-
Arguments.of("", null),
224-
Arguments.of(null, null));
225-
}
226-
227181
private static Stream<Arguments> sqlArgs() {
228182
return Stream.of(
229183
Arguments.of("SELECT * FROM TABLE WHERE FIELD=1234", "SELECT * FROM TABLE WHERE FIELD=?"),
@@ -334,125 +288,180 @@ private static Stream<Arguments> couchbaseArgs() {
334288
"SELECT * FROM TABLE WHERE FIELD = ?"));
335289
}
336290

337-
private static Function<String, SqlStatementInfo> expect(String operation, String identifier) {
338-
return sql -> SqlStatementInfo.create(sql, operation, identifier, null);
291+
private static Function<String, SqlStatementInfo> expect(
292+
String operation, String identifier, String querySummary) {
293+
return sql -> SqlStatementInfo.create(sql, operation, identifier, querySummary);
339294
}
340295

341296
private static Function<String, SqlStatementInfo> expect(
342-
String sql, String operation, String identifier) {
343-
return ignored -> SqlStatementInfo.create(sql, operation, identifier, null);
297+
String sql, String operation, String identifier, String querySummary) {
298+
return ignored -> SqlStatementInfo.create(sql, operation, identifier, querySummary);
344299
}
345300

346301
private static Stream<Arguments> simplifyArgs() {
347302
return Stream.of(
348303
// Select
349-
Arguments.of("SELECT x, y, z FROM schema.table", expect("SELECT", "schema.table")),
350-
Arguments.of("SELECT x, y, z FROM `schema table`", expect("SELECT", "schema table")),
351-
Arguments.of("SELECT x, y, z FROM `schema`.`table`", expect("SELECT", "`schema`.`table`")),
352-
Arguments.of("SELECT x, y, z FROM \"schema table\"", expect("SELECT", "schema table")),
353304
Arguments.of(
354-
"SELECT x, y, z FROM \"schema\".\"table\"", expect("SELECT", "\"schema\".\"table\"")),
305+
"SELECT x, y, z FROM schema.table",
306+
expect("SELECT", "schema.table", "SELECT schema.table")),
307+
Arguments.of(
308+
"SELECT x, y, z FROM `schema table`",
309+
expect("SELECT", "schema table", "SELECT schema table")),
310+
Arguments.of(
311+
"SELECT x, y, z FROM `schema`.`table`",
312+
expect("SELECT", "`schema`.`table`", "SELECT `schema`.`table`")),
313+
Arguments.of(
314+
"SELECT x, y, z FROM \"schema table\"",
315+
expect("SELECT", "schema table", "SELECT schema table")),
316+
Arguments.of(
317+
"SELECT x, y, z FROM \"schema\".\"table\"",
318+
expect("SELECT", "\"schema\".\"table\"", "SELECT \"schema\".\"table\"")),
319+
Arguments.of(
320+
"WITH subquery as (select a from b) SELECT x, y, z FROM table",
321+
expect("SELECT", null, "SELECT b SELECT")),
322+
Arguments.of(
323+
"SELECT x, y, (select a from b) as z FROM table",
324+
expect("SELECT", null, "SELECT SELECT b")),
325+
Arguments.of(
326+
"select delete, insert into, merge, update from table",
327+
expect("SELECT", "table", "SELECT DELETE INSERT MERGE UPDATE table")),
328+
Arguments.of(
329+
"select col /* from table2 */ from table", expect("SELECT", "table", "SELECT table")),
330+
Arguments.of(
331+
"select col from table join anotherTable", expect("SELECT", null, "SELECT table")),
355332
Arguments.of(
356-
"WITH subquery as (select a from b) SELECT x, y, z FROM table", expect("SELECT", null)),
357-
Arguments.of("SELECT x, y, (select a from b) as z FROM table", expect("SELECT", null)),
333+
"select col from (select * from anotherTable)",
334+
expect("SELECT", null, "SELECT SELECT anotherTable")),
358335
Arguments.of(
359-
"select delete, insert into, merge, update from table", expect("SELECT", "table")),
360-
Arguments.of("select col /* from table2 */ from table", expect("SELECT", "table")),
361-
Arguments.of("select col from table join anotherTable", expect("SELECT", null)),
362-
Arguments.of("select col from (select * from anotherTable)", expect("SELECT", null)),
363-
Arguments.of("select col from (select * from anotherTable) alias", expect("SELECT", null)),
364-
Arguments.of("select col from table1 union select col from table2", expect("SELECT", null)),
336+
"select col from (select * from anotherTable) alias",
337+
expect("SELECT", null, "SELECT SELECT anotherTable")),
338+
Arguments.of(
339+
"select col from table1 union select col from table2",
340+
expect("SELECT", null, "SELECT table1 SELECT")),
365341
Arguments.of(
366342
"select col from table where col in (select * from anotherTable)",
367-
expect("SELECT", null)),
368-
Arguments.of("select col from table1, table2", expect("SELECT", null)),
369-
Arguments.of("select col from table1 t1, table2 t2", expect("SELECT", null)),
370-
Arguments.of("select col from table1 as t1, table2 as t2", expect("SELECT", null)),
343+
expect("SELECT", null, "SELECT table SELECT anotherTable")),
344+
Arguments.of("select col from table1, table2", expect("SELECT", null, "SELECT table1")),
345+
Arguments.of(
346+
"select col from table1 t1, table2 t2", expect("SELECT", null, "SELECT table1")),
347+
Arguments.of(
348+
"select col from table1 as t1, table2 as t2", expect("SELECT", null, "SELECT table1")),
371349
Arguments.of(
372350
"select col from table where col in (1, 2, 3)",
373-
expect("select col from table where col in (?)", "SELECT", "table")),
351+
expect("select col from table where col in (?)", "SELECT", "table", "SELECT table")),
374352
Arguments.of(
375353
"select 'a' IN(x, 'b') from table where col in (1) and z IN( '3', '4' )",
376-
expect("select ? IN(x, ?) from table where col in (?) and z IN(?)", "SELECT", "table")),
377-
Arguments.of("select col from table order by col, col2", expect("SELECT", "table")),
378-
Arguments.of("select ąś∂ń© from źćļńĶ order by col, col2", expect("SELECT", "źćļńĶ")),
379-
Arguments.of("select 12345678", expect("select ?", "SELECT", null)),
380-
Arguments.of("/* update comment */ select * from table1", expect("SELECT", "table1")),
381-
Arguments.of("select /*((*/abc from table", expect("SELECT", "table")),
382-
Arguments.of("SeLeCT * FrOm TAblE", expect("SELECT", "table")),
383-
Arguments.of("select next value in hibernate_sequence", expect("SELECT", null)),
354+
expect(
355+
"select ? IN(x, ?) from table where col in (?) and z IN(?)",
356+
"SELECT",
357+
"table",
358+
"SELECT table")),
359+
Arguments.of(
360+
"select col from table order by col, col2", expect("SELECT", "table", "SELECT table")),
361+
Arguments.of(
362+
"select ąś∂ń© from źćļńĶ order by col, col2",
363+
expect("SELECT", "źćļńĶ", "SELECT źćļńĶ")),
364+
Arguments.of("select 12345678", expect("select ?", "SELECT", null, "SELECT")),
365+
Arguments.of(
366+
"/* update comment */ select * from table1",
367+
expect("SELECT", "table1", "SELECT table1")),
368+
Arguments.of("select /*((*/abc from table", expect("SELECT", "table", "SELECT table")),
369+
Arguments.of("SeLeCT * FrOm TAblE", expect("SELECT", "table", "SELECT TAblE")),
370+
Arguments.of("select next value in hibernate_sequence", expect("SELECT", null, "SELECT")),
384371

385372
// hibernate/jpa
386-
Arguments.of("FROM schema.table", expect("SELECT", "schema.table")),
387-
Arguments.of("/* update comment */ from table1", expect("SELECT", "table1")),
373+
Arguments.of("FROM schema.table", expect("SELECT", "schema.table", "schema.table")),
374+
Arguments.of("/* update comment */ from table1", expect("SELECT", "table1", "table1")),
388375

389376
// Insert
390-
Arguments.of(" insert into table where lalala", expect("INSERT", "table")),
391-
Arguments.of("insert insert into table where lalala", expect("INSERT", "table")),
392-
Arguments.of("insert into db.table where lalala", expect("INSERT", "db.table")),
393-
Arguments.of("insert into `db table` where lalala", expect("INSERT", "db table")),
394-
Arguments.of("insert into \"db table\" where lalala", expect("INSERT", "db table")),
395-
Arguments.of("insert without i-n-t-o", expect("INSERT", null)),
377+
Arguments.of(" insert into table where lalala", expect("INSERT", "table", "INSERT table")),
378+
Arguments.of(
379+
"insert insert into table where lalala",
380+
expect("INSERT", "table", "INSERT INSERT table")),
381+
Arguments.of(
382+
"insert into db.table where lalala", expect("INSERT", "db.table", "INSERT db.table")),
383+
Arguments.of(
384+
"insert into `db table` where lalala", expect("INSERT", "db table", "INSERT db table")),
385+
Arguments.of(
386+
"insert into \"db table\" where lalala",
387+
expect("INSERT", "db table", "INSERT db table")),
388+
Arguments.of("insert without i-n-t-o", expect("INSERT", null, "INSERT")),
396389

397390
// Delete
398-
Arguments.of("delete from table where something something", expect("DELETE", "table")),
399391
Arguments.of(
400-
"delete from `my table` where something something", expect("DELETE", "my table")),
392+
"delete from table where something something",
393+
expect("DELETE", "table", "DELETE table")),
394+
Arguments.of(
395+
"delete from `my table` where something something",
396+
expect("DELETE", "my table", "DELETE my table")),
401397
Arguments.of(
402-
"delete from \"my table\" where something something", expect("DELETE", "my table")),
398+
"delete from \"my table\" where something something",
399+
expect("DELETE", "my table", "DELETE my table")),
403400
Arguments.of(
404401
"delete from foo where x IN (1,2,3)",
405-
expect("delete from foo where x IN (?)", "DELETE", "foo")),
406-
Arguments.of("delete from 12345678", expect("delete from ?", "DELETE", null)),
407-
Arguments.of("delete (((", expect("delete (((", "DELETE", null)),
402+
expect("delete from foo where x IN (?)", "DELETE", "foo", "DELETE foo")),
403+
Arguments.of("delete from 12345678", expect("delete from ?", "DELETE", null, "DELETE")),
404+
Arguments.of("delete (((", expect("delete (((", "DELETE", null, "DELETE")),
408405

409406
// Update
410407
Arguments.of(
411-
"update table set answer=42", expect("update table set answer=?", "UPDATE", "table")),
408+
"update table set answer=42",
409+
expect("update table set answer=?", "UPDATE", "table", "UPDATE table")),
412410
Arguments.of(
413411
"update `my table` set answer=42",
414-
expect("update `my table` set answer=?", "UPDATE", "my table")),
412+
expect("update `my table` set answer=?", "UPDATE", "my table", "UPDATE my table")),
415413
Arguments.of(
416414
"update `my table` set answer=42 where x IN('a', 'b') AND y In ('a', 'b')",
417415
expect(
418-
"update `my table` set answer=? where x IN(?) AND y In (?)", "UPDATE", "my table")),
416+
"update `my table` set answer=? where x IN(?) AND y In (?)",
417+
"UPDATE",
418+
"my table",
419+
"UPDATE my table")),
419420
Arguments.of(
420421
"update \"my table\" set answer=42",
421-
expect("update \"my table\" set answer=?", "UPDATE", "my table")),
422-
Arguments.of("update /*table", expect("UPDATE", null)),
422+
expect("update \"my table\" set answer=?", "UPDATE", "my table", "UPDATE my table")),
423+
Arguments.of("update /*table", expect("UPDATE", null, "UPDATE")),
423424

424425
// Call
425-
Arguments.of("call test_proc()", expect("CALL", "test_proc")),
426-
Arguments.of("call test_proc", expect("CALL", "test_proc")),
427-
Arguments.of("call next value in hibernate_sequence", expect("CALL", null)),
428-
Arguments.of("call db.test_proc", expect("CALL", "db.test_proc")),
426+
Arguments.of("call test_proc()", expect("CALL", "test_proc", "CALL test_proc")),
427+
Arguments.of("call test_proc", expect("CALL", "test_proc", "CALL test_proc")),
428+
Arguments.of("call next value in hibernate_sequence", expect("CALL", null, "CALL")),
429+
Arguments.of("call db.test_proc", expect("CALL", "db.test_proc", "CALL db.test_proc")),
429430

430431
// Merge
431-
Arguments.of("merge into table", expect("MERGE", "table")),
432-
Arguments.of("merge into `my table`", expect("MERGE", "my table")),
433-
Arguments.of("merge into \"my table\"", expect("MERGE", "my table")),
434-
Arguments.of("merge table (into is optional in some dbs)", expect("MERGE", "table")),
435-
Arguments.of("merge (into )))", expect("MERGE", null)),
432+
Arguments.of("merge into table", expect("MERGE", "table", "MERGE table")),
433+
Arguments.of("merge into `my table`", expect("MERGE", "my table", "MERGE my table")),
434+
Arguments.of("merge into \"my table\"", expect("MERGE", "my table", "MERGE my table")),
435+
Arguments.of(
436+
"merge table (into is optional in some dbs)", expect("MERGE", "table", "MERGE table")),
437+
Arguments.of("merge (into )))", expect("MERGE", null, "MERGE")),
436438

437439
// Unknown operation
438-
Arguments.of("and now for something completely different", expect(null, null)),
439-
Arguments.of("", expect(null, null)),
440-
Arguments.of(null, expect(null, null)));
440+
Arguments.of("and now for something completely different", expect(null, null, null)),
441+
Arguments.of("", expect(null, null, null)),
442+
Arguments.of(null, expect(null, null, null)));
441443
}
442444

443445
private static Stream<Arguments> ddlArgs() {
444446
return Stream.of(
445-
Arguments.of("CREATE TABLE `table`", expect("CREATE TABLE", "table")),
446-
Arguments.of("CREATE TABLE IF NOT EXISTS table", expect("CREATE TABLE", "table")),
447-
Arguments.of("DROP TABLE `if`", expect("DROP TABLE", "if")),
447+
Arguments.of("CREATE TABLE `table`", expect("CREATE TABLE", "table", "CREATE TABLE table")),
448+
Arguments.of(
449+
"CREATE TABLE IF NOT EXISTS table",
450+
expect("CREATE TABLE", "table", "CREATE TABLE table")),
451+
Arguments.of("DROP TABLE `if`", expect("DROP TABLE", "if", "DROP TABLE if")),
448452
Arguments.of(
449453
"ALTER TABLE table ADD CONSTRAINT c FOREIGN KEY (foreign_id) REFERENCES ref (id)",
450-
expect("ALTER TABLE", "table")),
451-
Arguments.of("CREATE INDEX types_name ON types (name)", expect("CREATE INDEX", null)),
452-
Arguments.of("DROP INDEX types_name ON types (name)", expect("DROP INDEX", null)),
454+
expect("ALTER TABLE", "table", "ALTER TABLE table")),
455+
Arguments.of(
456+
"CREATE INDEX types_name ON types (name)",
457+
expect("CREATE INDEX", null, "CREATE INDEX")),
458+
Arguments.of(
459+
"DROP INDEX types_name ON types (name)", expect("DROP INDEX", null, "DROP INDEX")),
453460
Arguments.of(
454-
"CREATE VIEW tmp AS SELECT type FROM table WHERE id = ?", expect("CREATE VIEW", null)),
461+
"CREATE VIEW tmp AS SELECT type FROM table WHERE id = ?",
462+
expect("CREATE VIEW", null, "CREATE VIEW SELECT WHERE")),
455463
Arguments.of(
456-
"CREATE PROCEDURE p AS SELECT * FROM table GO", expect("CREATE PROCEDURE", null)));
464+
"CREATE PROCEDURE p AS SELECT * FROM table GO",
465+
expect("CREATE PROCEDURE", null, "CREATE PROCEDURE SELECT GO")));
457466
}
458467
}

0 commit comments

Comments
 (0)