Skip to content

Commit b736229

Browse files
committed
up
1 parent cae5b56 commit b736229

File tree

1 file changed

+119
-117
lines changed

1 file changed

+119
-117
lines changed

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

Lines changed: 119 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,173 @@ 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", expect("SELECT", "schema.table", "SELECT schema.table")),
306+
Arguments.of(
307+
"SELECT x, y, z FROM `schema table`", expect("SELECT", "schema table", "SELECT schema table")),
308+
Arguments.of(
309+
"SELECT x, y, z FROM `schema`.`table`",
310+
expect("SELECT", "`schema`.`table`", "SELECT `schema`.`table`")),
311+
Arguments.of(
312+
"SELECT x, y, z FROM \"schema table\"",
313+
expect("SELECT", "schema table", "SELECT schema table")),
314+
Arguments.of(
315+
"SELECT x, y, z FROM \"schema\".\"table\"",
316+
expect("SELECT", "\"schema\".\"table\"", "SELECT \"schema\".\"table\"")),
317+
Arguments.of(
318+
"WITH subquery as (select a from b) SELECT x, y, z FROM table",
319+
expect("SELECT", null, "SELECT b SELECT")),
320+
Arguments.of(
321+
"SELECT x, y, (select a from b) as z FROM table",
322+
expect("SELECT", null, "SELECT SELECT b")),
323+
Arguments.of(
324+
"select delete, insert into, merge, update from table",
325+
expect("SELECT", "table", "SELECT DELETE INSERT MERGE UPDATE table")),
326+
Arguments.of(
327+
"select col /* from table2 */ from table", expect("SELECT", "table", "SELECT table")),
355328
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)),
329+
"select col from table join anotherTable", expect("SELECT", null, "SELECT table")),
358330
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)),
331+
"select col from (select * from anotherTable)",
332+
expect("SELECT", null, "SELECT SELECT anotherTable")),
333+
Arguments.of(
334+
"select col from (select * from anotherTable) alias",
335+
expect("SELECT", null, "SELECT SELECT anotherTable")),
336+
Arguments.of(
337+
"select col from table1 union select col from table2",
338+
expect("SELECT", null, "SELECT table1 SELECT")),
365339
Arguments.of(
366340
"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)),
341+
expect("SELECT", null, "SELECT table SELECT anotherTable")),
342+
Arguments.of("select col from table1, table2", expect("SELECT", null, "SELECT table1")),
343+
Arguments.of("select col from table1 t1, table2 t2", expect("SELECT", null, "SELECT table1")),
344+
Arguments.of(
345+
"select col from table1 as t1, table2 as t2", expect("SELECT", null, "SELECT table1")),
371346
Arguments.of(
372347
"select col from table where col in (1, 2, 3)",
373-
expect("select col from table where col in (?)", "SELECT", "table")),
348+
expect("select col from table where col in (?)", "SELECT", "table", "SELECT table")),
374349
Arguments.of(
375350
"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)),
351+
expect(
352+
"select ? IN(x, ?) from table where col in (?) and z IN(?)",
353+
"SELECT",
354+
"table",
355+
"SELECT table")),
356+
Arguments.of(
357+
"select col from table order by col, col2", expect("SELECT", "table", "SELECT table")),
358+
Arguments.of(
359+
"select ąś∂ń© from źćļńĶ order by col, col2", expect("SELECT", "źćļńĶ", "SELECT źćļńĶ")),
360+
Arguments.of("select 12345678", expect("select ?", "SELECT", null, "SELECT")),
361+
Arguments.of(
362+
"/* update comment */ select * from table1", expect("SELECT", "table1", "SELECT table1")),
363+
Arguments.of("select /*((*/abc from table", expect("SELECT", "table", "SELECT table")),
364+
Arguments.of("SeLeCT * FrOm TAblE", expect("SELECT", "table", "SELECT TAblE")),
365+
Arguments.of("select next value in hibernate_sequence", expect("SELECT", null, "SELECT")),
384366

385367
// hibernate/jpa
386-
Arguments.of("FROM schema.table", expect("SELECT", "schema.table")),
387-
Arguments.of("/* update comment */ from table1", expect("SELECT", "table1")),
368+
Arguments.of("FROM schema.table", expect("SELECT", "schema.table", "schema.table")),
369+
Arguments.of("/* update comment */ from table1", expect("SELECT", "table1", "table1")),
388370

389371
// 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)),
372+
Arguments.of(" insert into table where lalala", expect("INSERT", "table", "INSERT table")),
373+
Arguments.of(
374+
"insert insert into table where lalala",
375+
expect("INSERT", "table", "INSERT INSERT table")),
376+
Arguments.of(
377+
"insert into db.table where lalala", expect("INSERT", "db.table", "INSERT db.table")),
378+
Arguments.of(
379+
"insert into `db table` where lalala", expect("INSERT", "db table", "INSERT db table")),
380+
Arguments.of(
381+
"insert into \"db table\" where lalala", expect("INSERT", "db table", "INSERT db table")),
382+
Arguments.of("insert without i-n-t-o", expect("INSERT", null, "INSERT")),
396383

397384
// Delete
398-
Arguments.of("delete from table where something something", expect("DELETE", "table")),
399385
Arguments.of(
400-
"delete from `my table` where something something", expect("DELETE", "my table")),
386+
"delete from table where something something",
387+
expect("DELETE", "table", "DELETE table")),
388+
Arguments.of(
389+
"delete from `my table` where something something",
390+
expect("DELETE", "my table", "DELETE my table")),
401391
Arguments.of(
402-
"delete from \"my table\" where something something", expect("DELETE", "my table")),
392+
"delete from \"my table\" where something something",
393+
expect("DELETE", "my table", "DELETE my table")),
403394
Arguments.of(
404395
"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)),
396+
expect("delete from foo where x IN (?)", "DELETE", "foo", "DELETE foo")),
397+
Arguments.of("delete from 12345678", expect("delete from ?", "DELETE", null, "DELETE")),
398+
Arguments.of("delete (((", expect("delete (((", "DELETE", null, "DELETE")),
408399

409400
// Update
410401
Arguments.of(
411-
"update table set answer=42", expect("update table set answer=?", "UPDATE", "table")),
402+
"update table set answer=42",
403+
expect("update table set answer=?", "UPDATE", "table", "UPDATE table")),
412404
Arguments.of(
413405
"update `my table` set answer=42",
414-
expect("update `my table` set answer=?", "UPDATE", "my table")),
406+
expect("update `my table` set answer=?", "UPDATE", "my table", "UPDATE my table")),
415407
Arguments.of(
416408
"update `my table` set answer=42 where x IN('a', 'b') AND y In ('a', 'b')",
417409
expect(
418-
"update `my table` set answer=? where x IN(?) AND y In (?)", "UPDATE", "my table")),
410+
"update `my table` set answer=? where x IN(?) AND y In (?)",
411+
"UPDATE",
412+
"my table",
413+
"UPDATE my table")),
419414
Arguments.of(
420415
"update \"my table\" set answer=42",
421-
expect("update \"my table\" set answer=?", "UPDATE", "my table")),
422-
Arguments.of("update /*table", expect("UPDATE", null)),
416+
expect("update \"my table\" set answer=?", "UPDATE", "my table", "UPDATE my table")),
417+
Arguments.of("update /*table", expect("UPDATE", null, "UPDATE")),
423418

424419
// 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")),
420+
Arguments.of("call test_proc()", expect("CALL", "test_proc", "CALL test_proc")),
421+
Arguments.of("call test_proc", expect("CALL", "test_proc", "CALL test_proc")),
422+
Arguments.of("call next value in hibernate_sequence", expect("CALL", null, "CALL")),
423+
Arguments.of("call db.test_proc", expect("CALL", "db.test_proc", "CALL db.test_proc")),
429424

430425
// 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)),
426+
Arguments.of("merge into table", expect("MERGE", "table", "MERGE table")),
427+
Arguments.of("merge into `my table`", expect("MERGE", "my table", "MERGE my table")),
428+
Arguments.of("merge into \"my table\"", expect("MERGE", "my table", "MERGE my table")),
429+
Arguments.of(
430+
"merge table (into is optional in some dbs)",
431+
expect("MERGE", "table", "MERGE table")),
432+
Arguments.of("merge (into )))", expect("MERGE", null, "MERGE")),
436433

437434
// 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)));
435+
Arguments.of("and now for something completely different", expect(null, null, null)),
436+
Arguments.of("", expect(null, null, null)),
437+
Arguments.of(null, expect(null, null, null)));
441438
}
442439

443440
private static Stream<Arguments> ddlArgs() {
444441
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")),
442+
Arguments.of("CREATE TABLE `table`", expect("CREATE TABLE", "table", "CREATE TABLE table")),
443+
Arguments.of(
444+
"CREATE TABLE IF NOT EXISTS table", expect("CREATE TABLE", "table", "CREATE TABLE table")),
445+
Arguments.of("DROP TABLE `if`", expect("DROP TABLE", "if", "DROP TABLE if")),
448446
Arguments.of(
449447
"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)),
448+
expect("ALTER TABLE", "table", "ALTER TABLE table")),
449+
Arguments.of(
450+
"CREATE INDEX types_name ON types (name)", expect("CREATE INDEX", null, "CREATE INDEX")),
451+
Arguments.of(
452+
"DROP INDEX types_name ON types (name)", expect("DROP INDEX", null, "DROP INDEX")),
453453
Arguments.of(
454-
"CREATE VIEW tmp AS SELECT type FROM table WHERE id = ?", expect("CREATE VIEW", null)),
454+
"CREATE VIEW tmp AS SELECT type FROM table WHERE id = ?",
455+
expect("CREATE VIEW", null, "CREATE VIEW SELECT WHERE")),
455456
Arguments.of(
456-
"CREATE PROCEDURE p AS SELECT * FROM table GO", expect("CREATE PROCEDURE", null)));
457+
"CREATE PROCEDURE p AS SELECT * FROM table GO",
458+
expect("CREATE PROCEDURE", null, "CREATE PROCEDURE SELECT GO")));
457459
}
458460
}

0 commit comments

Comments
 (0)