Skip to content

Commit 996098c

Browse files
committed
Addressed PR comments
1 parent d598edb commit 996098c

File tree

6 files changed

+24
-34
lines changed

6 files changed

+24
-34
lines changed

Diff for: instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/db/SqlStatementInfo.java

+1-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package io.opentelemetry.instrumentation.api.db;
77

88
import com.google.auto.value.AutoValue;
9-
import java.util.function.Function;
109
import javax.annotation.Nullable;
1110

1211
@AutoValue
@@ -17,26 +16,12 @@ public static SqlStatementInfo create(
1716
return new AutoValue_SqlStatementInfo(fullStatement, operation, identifier);
1817
}
1918

20-
public SqlStatementInfo mapTable(Function<String, String> mapper) {
21-
return SqlStatementInfo.create(
22-
getFullStatement(), getOperation(), mapper.apply(getIdentifier()));
23-
}
24-
2519
@Nullable
2620
public abstract String getFullStatement();
2721

2822
@Nullable
2923
public abstract String getOperation();
3024

3125
@Nullable
32-
public abstract String getIdentifier();
33-
34-
@Nullable
35-
public String getTable() {
36-
String operation = getOperation();
37-
if (operation != null && !operation.equals("CALL")) {
38-
return getIdentifier();
39-
}
40-
return null;
41-
}
26+
public abstract String getMainIdentifier();
4227
}

Diff for: instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/db/DbClientSpanNameExtractor.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public static <REQUEST> SpanNameExtractor<REQUEST> create(
2929
*
3030
* @see SqlStatementInfo#getOperation() used to extract {@code <db.operation>}.
3131
* @see DbClientAttributesGetter#name(Object) used to extract {@code <db.name>}.
32-
* @see SqlStatementInfo#getIdentifier() used to extract {@code <db.identifier>}.
32+
* @see SqlStatementInfo#getMainIdentifier() used to extract {@code <db.table>} or stored
33+
* procedure name.
3334
*/
3435
public static <REQUEST> SpanNameExtractor<REQUEST> create(
3536
SqlClientAttributesGetter<REQUEST> getter) {
@@ -96,7 +97,7 @@ public String extract(REQUEST request) {
9697
String dbName = getter.name(request);
9798
SqlStatementInfo sanitizedStatement = sanitizer.sanitize(getter.rawStatement(request));
9899
return computeSpanName(
99-
dbName, sanitizedStatement.getOperation(), sanitizedStatement.getIdentifier());
100+
dbName, sanitizedStatement.getOperation(), sanitizedStatement.getMainIdentifier());
100101
}
101102
}
102103
}

Diff for: instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/db/SqlClientAttributesExtractor.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static <REQUEST, RESPONSE> SqlClientAttributesExtractorBuilder<REQUEST, R
4343
return new SqlClientAttributesExtractorBuilder<>(getter);
4444
}
4545

46+
private static final String SQL_CALL = "CALL";
4647
private final AttributeKey<String> dbTableAttribute;
4748
private final SqlStatementSanitizer sanitizer;
4849

@@ -60,8 +61,11 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
6061
super.onStart(attributes, parentContext, request);
6162

6263
SqlStatementInfo sanitizedStatement = sanitizer.sanitize(getter.rawStatement(request));
64+
String operation = sanitizedStatement.getOperation();
6365
internalSet(attributes, SemanticAttributes.DB_STATEMENT, sanitizedStatement.getFullStatement());
64-
internalSet(attributes, SemanticAttributes.DB_OPERATION, sanitizedStatement.getOperation());
65-
internalSet(attributes, dbTableAttribute, sanitizedStatement.getTable());
66+
internalSet(attributes, SemanticAttributes.DB_OPERATION, operation);
67+
if (operation != null && !operation.equals(SQL_CALL)) {
68+
internalSet(attributes, dbTableAttribute, sanitizedStatement.getMainIdentifier());
69+
}
6670
}
6771
}

Diff for: instrumentation-api-semconv/src/main/jflex/SqlSanitizer.jflex

+11-12
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ WHITESPACE = [ \t\r\n]+
6464

6565
/** @return text matched by current token without enclosing double quotes or backticks */
6666
private String readIdentifierName() {
67-
String IdentifierName = yytext();
68-
if (IdentifierName != null && ((IdentifierName.startsWith("\"") && IdentifierName.endsWith("\""))
69-
|| (IdentifierName.startsWith("`") && IdentifierName.endsWith("`")))) {
70-
IdentifierName = IdentifierName.substring(1, IdentifierName.length() - 1);
67+
String identifierName = yytext();
68+
if (identifierName != null && ((identifierName.startsWith("\"") && identifierName.endsWith("\""))
69+
|| (identifierName.startsWith("`") && identifierName.endsWith("`")))) {
70+
identifierName = identifierName.substring(1, identifierName.length() - 1);
7171
}
72-
return IdentifierName;
72+
return identifierName;
7373
}
7474

7575
// you can reference a table in the FROM clause in one of the following ways:
@@ -316,13 +316,12 @@ WHITESPACE = [ \t\r\n]+
316316
if (isOverLimit()) return YYEOF;
317317
}
318318
"CALL" {
319-
if (!insideComment) {
320-
setOperation(new Call());
321-
}
322-
operation.handleIdentifier();
323-
appendCurrentFragment();
324-
if (isOverLimit()) return YYEOF;
325-
}
319+
if (!insideComment) {
320+
setOperation(new Call());
321+
}
322+
appendCurrentFragment();
323+
if (isOverLimit()) return YYEOF;
324+
}
326325
"MERGE" {
327326
if (!insideComment) {
328327
setOperation(new Merge());

Diff for: instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/db/SqlStatementSanitizerTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) th
276276
Arguments.of("/* update comment */ select * from table1", expect("SELECT", "table1")),
277277
Arguments.of("select /*((*/abc from table", expect("SELECT", "table")),
278278
Arguments.of("SeLeCT * FrOm TAblE", expect("SELECT", "table")),
279+
Arguments.of("select next value in hibernate_sequence", expect("SELECT", null)),
279280

280281
// hibernate/jpa
281282
Arguments.of("FROM schema.table", expect("SELECT", "schema.table")),

Diff for: instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/OperationNameUtil.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public static String getOperationNameForQuery(String query) {
2222
SqlStatementInfo info = sanitizer.sanitize(query);
2323
if (info.getOperation() != null) {
2424
operation = info.getOperation();
25-
if (info.getTable() != null) {
26-
operation += " " + info.getTable();
25+
if (info.getMainIdentifier() != null) {
26+
operation += " " + info.getMainIdentifier();
2727
}
2828
}
2929
return operation;

0 commit comments

Comments
 (0)