Skip to content

Commit 96d414f

Browse files
committed
fix missing db.operation for create/drop/alter statement
1 parent 54c30e1 commit 96d414f

File tree

7 files changed

+219
-69
lines changed

7 files changed

+219
-69
lines changed

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

+117-1
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,48 @@ WHITESPACE = [ \t\r\n]+
122122
/** @return true if all statement info is gathered */
123123
boolean handleNext() {
124124
return false;
125-
}
125+
}
126+
127+
/** @return true if all statement info is gathered */
128+
boolean handleOperationTarget(String target) {
129+
return false;
130+
}
131+
132+
boolean waitingOperationTarget() {
133+
return false;
134+
}
126135

127136
SqlStatementInfo getResult(String fullStatement) {
128137
return SqlStatementInfo.create(fullStatement, getClass().getSimpleName().toUpperCase(java.util.Locale.ROOT), mainIdentifier);
129138
}
130139
}
131140

141+
private static abstract class CompositeOp extends Operation {
142+
private String operationTarget = "";
143+
private boolean waitingOperationTarget = true;
144+
145+
boolean waitingOperationTarget() {
146+
return waitingOperationTarget;
147+
}
148+
149+
boolean handleOperationTarget(String target) {
150+
operationTarget = target;
151+
waitingOperationTarget = false;
152+
return false;
153+
}
154+
155+
boolean shouldHandleIdentifier() {
156+
return "TABLE".equals(operationTarget);
157+
}
158+
159+
SqlStatementInfo getResult(String fullStatement) {
160+
if (!"".equals(operationTarget)) {
161+
return SqlStatementInfo.create(fullStatement, getClass().getSimpleName().toUpperCase(java.util.Locale.ROOT) + " " + operationTarget, mainIdentifier);
162+
}
163+
return super.getResult(fullStatement);
164+
}
165+
}
166+
132167
private static class NoOp extends Operation {
133168
static final Operation INSTANCE = new NoOp();
134169

@@ -273,6 +308,33 @@ WHITESPACE = [ \t\r\n]+
273308
}
274309
}
275310

311+
private class Create extends CompositeOp {
312+
boolean handleIdentifier() {
313+
if (shouldHandleIdentifier()) {
314+
mainIdentifier = readIdentifierName();
315+
}
316+
return true;
317+
}
318+
}
319+
320+
private class Drop extends CompositeOp {
321+
boolean handleIdentifier() {
322+
if (shouldHandleIdentifier()) {
323+
mainIdentifier = readIdentifierName();
324+
}
325+
return true;
326+
}
327+
}
328+
329+
private class Alter extends CompositeOp {
330+
boolean handleIdentifier() {
331+
if (shouldHandleIdentifier()) {
332+
mainIdentifier = readIdentifierName();
333+
}
334+
return true;
335+
}
336+
}
337+
276338
private SqlStatementInfo getResult() {
277339
if (builder.length() > LIMIT) {
278340
builder.delete(LIMIT, builder.length());
@@ -329,6 +391,27 @@ WHITESPACE = [ \t\r\n]+
329391
appendCurrentFragment();
330392
if (isOverLimit()) return YYEOF;
331393
}
394+
"CREATE" {
395+
if (!insideComment) {
396+
setOperation(new Create());
397+
}
398+
appendCurrentFragment();
399+
if (isOverLimit()) return YYEOF;
400+
}
401+
"DROP" {
402+
if (!insideComment) {
403+
setOperation(new Drop());
404+
}
405+
appendCurrentFragment();
406+
if (isOverLimit()) return YYEOF;
407+
}
408+
"ALTER" {
409+
if (!insideComment) {
410+
setOperation(new Alter());
411+
}
412+
appendCurrentFragment();
413+
if (isOverLimit()) return YYEOF;
414+
}
332415
"FROM" {
333416
if (!insideComment && !extractionDone) {
334417
if (operation == NoOp.INSTANCE) {
@@ -358,7 +441,40 @@ WHITESPACE = [ \t\r\n]+
358441
"NEXT" {
359442
if (!insideComment && !extractionDone) {
360443
extractionDone = operation.handleNext();
444+
}
445+
appendCurrentFragment();
446+
if (isOverLimit()) return YYEOF;
447+
}
448+
"TABLE" {
449+
if (!insideComment && !extractionDone) {
450+
if (operation.waitingOperationTarget()) {
451+
extractionDone = operation.handleOperationTarget("TABLE");
452+
} else {
453+
extractionDone = operation.handleIdentifier();
454+
}
455+
}
456+
appendCurrentFragment();
457+
if (isOverLimit()) return YYEOF;
458+
}
459+
"INDEX" {
460+
if (!insideComment && !extractionDone) {
461+
if (operation.waitingOperationTarget()) {
462+
extractionDone = operation.handleOperationTarget("INDEX");
463+
} else {
464+
extractionDone = operation.handleIdentifier();
361465
}
466+
}
467+
appendCurrentFragment();
468+
if (isOverLimit()) return YYEOF;
469+
}
470+
"DATABASE" {
471+
if (!insideComment && !extractionDone) {
472+
if (operation.waitingOperationTarget()) {
473+
extractionDone = operation.handleOperationTarget("DATABASE");
474+
} else {
475+
extractionDone = operation.handleIdentifier();
476+
}
477+
}
362478
appendCurrentFragment();
363479
if (isOverLimit()) return YYEOF;
364480
}

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

+30
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,36 @@ void veryLongSelectStatementsAreOk() {
5858
assertThat(result).isEqualTo(expected);
5959
}
6060

61+
@Test
62+
void changeSchemaStatementsAreOk() {
63+
String createTableStatement = "CREATE TABLE `table`";
64+
SqlStatementInfo expected =
65+
SqlStatementInfo.create(createTableStatement, "CREATE TABLE", "table");
66+
SqlStatementInfo result = SqlStatementSanitizer.create(true).sanitize(createTableStatement);
67+
assertThat(result).isEqualTo(expected);
68+
69+
String dropTableStatement = "DROP TABLE anotherTable";
70+
expected = SqlStatementInfo.create(dropTableStatement, "DROP TABLE", "anotherTable");
71+
result = SqlStatementSanitizer.create(true).sanitize(dropTableStatement);
72+
assertThat(result).isEqualTo(expected);
73+
74+
String alterStatement =
75+
"ALTER TABLE table ADD CONSTRAINT c FOREIGN KEY (foreign_id) REFERENCES ref (id)";
76+
expected = SqlStatementInfo.create(alterStatement, "ALTER TABLE", "table");
77+
result = SqlStatementSanitizer.create(true).sanitize(alterStatement);
78+
assertThat(result).isEqualTo(expected);
79+
80+
String createStatement = "CREATE INDEX types_name ON types (name)";
81+
expected = SqlStatementInfo.create(createStatement, "CREATE INDEX", null);
82+
result = SqlStatementSanitizer.create(true).sanitize(createStatement);
83+
assertThat(result).isEqualTo(expected);
84+
85+
String dropStatement = "DROP INDEX types_name ON types (name)";
86+
expected = SqlStatementInfo.create(dropStatement, "DROP INDEX", null);
87+
result = SqlStatementSanitizer.create(true).sanitize(dropStatement);
88+
assertThat(result).isEqualTo(expected);
89+
}
90+
6191
@Test
6292
void lotsOfTicksDontCauseStackOverflowOrLongRuntimes() {
6393
String s = "'";

instrumentation/cassandra/cassandra-3.0/javaagent/src/test/java/CassandraClientTest.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ private static Stream<Arguments> provideSyncParameters() {
213213
null,
214214
"DROP KEYSPACE IF EXISTS sync_test",
215215
"DROP KEYSPACE IF EXISTS sync_test",
216-
"DB Query",
217-
null,
216+
"DROP",
217+
"DROP",
218218
null))),
219219
Arguments.of(
220220
named(
@@ -223,8 +223,8 @@ private static Stream<Arguments> provideSyncParameters() {
223223
null,
224224
"CREATE KEYSPACE sync_test WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':3}",
225225
"CREATE KEYSPACE sync_test WITH REPLICATION = {?:?, ?:?}",
226-
"DB Query",
227-
null,
226+
"CREATE",
227+
"CREATE",
228228
null))),
229229
Arguments.of(
230230
named(
@@ -233,9 +233,9 @@ private static Stream<Arguments> provideSyncParameters() {
233233
"sync_test",
234234
"CREATE TABLE sync_test.users ( id UUID PRIMARY KEY, name text )",
235235
"CREATE TABLE sync_test.users ( id UUID PRIMARY KEY, name text )",
236-
"sync_test",
237-
null,
238-
null))),
236+
"CREATE TABLE sync_test.users",
237+
"CREATE TABLE",
238+
"sync_test.users"))),
239239
Arguments.of(
240240
named(
241241
"Insert data",
@@ -267,8 +267,8 @@ private static Stream<Arguments> provideAsyncParameters() {
267267
null,
268268
"DROP KEYSPACE IF EXISTS async_test",
269269
"DROP KEYSPACE IF EXISTS async_test",
270-
"DB Query",
271-
null,
270+
"DROP",
271+
"DROP",
272272
null))),
273273
Arguments.of(
274274
named(
@@ -277,8 +277,8 @@ private static Stream<Arguments> provideAsyncParameters() {
277277
null,
278278
"CREATE KEYSPACE async_test WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':3}",
279279
"CREATE KEYSPACE async_test WITH REPLICATION = {?:?, ?:?}",
280-
"DB Query",
281-
null,
280+
"CREATE",
281+
"CREATE",
282282
null))),
283283
Arguments.of(
284284
named(
@@ -287,9 +287,9 @@ private static Stream<Arguments> provideAsyncParameters() {
287287
"async_test",
288288
"CREATE TABLE async_test.users ( id UUID PRIMARY KEY, name text )",
289289
"CREATE TABLE async_test.users ( id UUID PRIMARY KEY, name text )",
290-
"async_test",
291-
null,
292-
null))),
290+
"CREATE TABLE async_test.users",
291+
"CREATE TABLE",
292+
"async_test.users"))),
293293
Arguments.of(
294294
named(
295295
"Insert data",

instrumentation/cassandra/cassandra-4-common/testing/src/main/java/io/opentelemetry/cassandra/v4/common/AbstractCassandraTest.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ private static Stream<Arguments> provideSyncParameters() {
172172
null,
173173
"DROP KEYSPACE IF EXISTS sync_test",
174174
"DROP KEYSPACE IF EXISTS sync_test",
175-
"DB Query",
176-
null,
175+
"DROP",
176+
"DROP",
177177
null))),
178178
Arguments.of(
179179
named(
@@ -182,8 +182,8 @@ private static Stream<Arguments> provideSyncParameters() {
182182
null,
183183
"CREATE KEYSPACE sync_test WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':3}",
184184
"CREATE KEYSPACE sync_test WITH REPLICATION = {?:?, ?:?}",
185-
"DB Query",
186-
null,
185+
"CREATE",
186+
"CREATE",
187187
null))),
188188
Arguments.of(
189189
named(
@@ -192,9 +192,9 @@ private static Stream<Arguments> provideSyncParameters() {
192192
"sync_test",
193193
"CREATE TABLE sync_test.users ( id UUID PRIMARY KEY, name text )",
194194
"CREATE TABLE sync_test.users ( id UUID PRIMARY KEY, name text )",
195-
"sync_test",
196-
null,
197-
null))),
195+
"CREATE TABLE sync_test.users",
196+
"CREATE TABLE",
197+
"sync_test.users"))),
198198
Arguments.of(
199199
named(
200200
"Insert data",
@@ -226,8 +226,8 @@ private static Stream<Arguments> provideAsyncParameters() {
226226
null,
227227
"DROP KEYSPACE IF EXISTS async_test",
228228
"DROP KEYSPACE IF EXISTS async_test",
229-
"DB Query",
230-
null,
229+
"DROP",
230+
"DROP",
231231
null))),
232232
Arguments.of(
233233
named(
@@ -236,8 +236,8 @@ private static Stream<Arguments> provideAsyncParameters() {
236236
null,
237237
"CREATE KEYSPACE async_test WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':3}",
238238
"CREATE KEYSPACE async_test WITH REPLICATION = {?:?, ?:?}",
239-
"DB Query",
240-
null,
239+
"CREATE",
240+
"CREATE",
241241
null))),
242242
Arguments.of(
243243
named(
@@ -246,9 +246,9 @@ private static Stream<Arguments> provideAsyncParameters() {
246246
"async_test",
247247
"CREATE TABLE async_test.users ( id UUID PRIMARY KEY, name text )",
248248
"CREATE TABLE async_test.users ( id UUID PRIMARY KEY, name text )",
249-
"async_test",
250-
null,
251-
null))),
249+
"CREATE TABLE async_test.users",
250+
"CREATE TABLE",
251+
"async_test.users"))),
252252
Arguments.of(
253253
named(
254254
"Insert data",

instrumentation/cassandra/cassandra-4.4/testing/src/main/java/io/opentelemetry/testing/cassandra/v4_4/AbstractCassandra44Test.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ private static Stream<Arguments> provideReactiveParameters() {
9191
null,
9292
"DROP KEYSPACE IF EXISTS reactive_test",
9393
"DROP KEYSPACE IF EXISTS reactive_test",
94-
"DB Query",
95-
null,
94+
"DROP",
95+
"DROP",
9696
null))),
9797
Arguments.of(
9898
named(
@@ -101,8 +101,8 @@ private static Stream<Arguments> provideReactiveParameters() {
101101
null,
102102
"CREATE KEYSPACE reactive_test WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':3}",
103103
"CREATE KEYSPACE reactive_test WITH REPLICATION = {?:?, ?:?}",
104-
"DB Query",
105-
null,
104+
"CREATE",
105+
"CREATE",
106106
null))),
107107
Arguments.of(
108108
named(
@@ -111,9 +111,9 @@ private static Stream<Arguments> provideReactiveParameters() {
111111
"reactive_test",
112112
"CREATE TABLE reactive_test.users ( id UUID PRIMARY KEY, name text )",
113113
"CREATE TABLE reactive_test.users ( id UUID PRIMARY KEY, name text )",
114-
"reactive_test",
115-
null,
116-
null))),
114+
"CREATE TABLE reactive_test.users",
115+
"CREATE TABLE",
116+
"reactive_test.users"))),
117117
Arguments.of(
118118
named(
119119
"Insert data",

0 commit comments

Comments
 (0)