Skip to content

Commit 32fab83

Browse files
authored
Rename database method/fields/etc to match stable semconv terminology (1 of several) (#15833)
1 parent 9ead874 commit 32fab83

File tree

12 files changed

+119
-84
lines changed

12 files changed

+119
-84
lines changed

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientSpanNameExtractor.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ public abstract class DbClientSpanNameExtractor<REQUEST> implements SpanNameExtr
1414

1515
/**
1616
* Returns a {@link SpanNameExtractor} that constructs the span name according to DB semantic
17-
* conventions: {@code <db.operation> <db.name>}.
18-
*
19-
* @see DbClientAttributesGetter#getDbOperationName(Object) used to extract {@code
20-
* <db.operation.name>}.
21-
* @see DbClientAttributesGetter#getDbNamespace(Object) used to extract {@code <db.namespace>}.
17+
* conventions.
2218
*/
2319
public static <REQUEST> SpanNameExtractor<REQUEST> create(
2420
DbClientAttributesGetter<REQUEST, ?> getter) {
@@ -27,12 +23,7 @@ public static <REQUEST> SpanNameExtractor<REQUEST> create(
2723

2824
/**
2925
* Returns a {@link SpanNameExtractor} that constructs the span name according to DB semantic
30-
* conventions: {@code <db.operation> <db.name>.<identifier>}.
31-
*
32-
* @see SqlStatementInfo#getOperation() used to extract {@code <db.operation>}.
33-
* @see DbClientAttributesGetter#getDbNamespace(Object) used to extract {@code <db.namespace>}.
34-
* @see SqlStatementInfo#getMainIdentifier() used to extract {@code <db.table>} or stored
35-
* procedure name.
26+
* conventions.
3627
*/
3728
public static <REQUEST> SpanNameExtractor<REQUEST> create(
3829
SqlClientAttributesGetter<REQUEST, ?> getter) {
@@ -44,26 +35,26 @@ public static <REQUEST> SpanNameExtractor<REQUEST> create(
4435
private DbClientSpanNameExtractor() {}
4536

4637
protected String computeSpanName(
47-
@Nullable String dbName, @Nullable String operation, @Nullable String mainIdentifier) {
48-
if (operation == null) {
49-
return dbName == null ? DEFAULT_SPAN_NAME : dbName;
38+
@Nullable String namespace, @Nullable String operationName, @Nullable String mainIdentifier) {
39+
if (operationName == null) {
40+
return namespace == null ? DEFAULT_SPAN_NAME : namespace;
5041
}
5142

52-
StringBuilder name = new StringBuilder(operation);
53-
if (dbName != null || mainIdentifier != null) {
54-
name.append(' ');
43+
StringBuilder spanName = new StringBuilder(operationName);
44+
if (namespace != null || mainIdentifier != null) {
45+
spanName.append(' ');
5546
}
56-
// skip db name if identifier already has a db name prefixed to it
57-
if (dbName != null && (mainIdentifier == null || mainIdentifier.indexOf('.') == -1)) {
58-
name.append(dbName);
47+
// skip namespace if identifier already has a namespace prefixed to it
48+
if (namespace != null && (mainIdentifier == null || mainIdentifier.indexOf('.') == -1)) {
49+
spanName.append(namespace);
5950
if (mainIdentifier != null) {
60-
name.append('.');
51+
spanName.append('.');
6152
}
6253
}
6354
if (mainIdentifier != null) {
64-
name.append(mainIdentifier);
55+
spanName.append(mainIdentifier);
6556
}
66-
return name.toString();
57+
return spanName.toString();
6758
}
6859

6960
private static final class GenericDbClientSpanNameExtractor<REQUEST>
@@ -107,24 +98,29 @@ public String extract(REQUEST request) {
10798
}
10899
SqlStatementInfo sanitizedStatement =
109100
SqlStatementSanitizerUtil.sanitize(rawQueryTexts.iterator().next());
101+
110102
return computeSpanName(
111-
namespace, sanitizedStatement.getOperation(), sanitizedStatement.getMainIdentifier());
103+
namespace,
104+
sanitizedStatement.getOperationName(),
105+
sanitizedStatement.getMainIdentifier());
112106
}
113107

114108
if (rawQueryTexts.size() == 1) {
115109
SqlStatementInfo sanitizedStatement =
116110
SqlStatementSanitizerUtil.sanitize(rawQueryTexts.iterator().next());
117-
String operation = sanitizedStatement.getOperation();
111+
String operationName = sanitizedStatement.getOperationName();
118112
if (isBatch(request)) {
119-
operation = "BATCH " + operation;
113+
operationName = "BATCH " + operationName;
120114
}
121-
return computeSpanName(namespace, operation, sanitizedStatement.getMainIdentifier());
115+
return computeSpanName(namespace, operationName, sanitizedStatement.getMainIdentifier());
122116
}
123117

124118
MultiQuery multiQuery = MultiQuery.analyze(rawQueryTexts, false);
125119
return computeSpanName(
126120
namespace,
127-
multiQuery.getOperation() != null ? "BATCH " + multiQuery.getOperation() : "BATCH",
121+
multiQuery.getOperationName() != null
122+
? "BATCH " + multiQuery.getOperationName()
123+
: "BATCH",
128124
multiQuery.getMainIdentifier());
129125
}
130126

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/MultiQuery.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,64 @@
1313
class MultiQuery {
1414

1515
@Nullable private final String mainIdentifier;
16-
@Nullable private final String operation;
17-
private final Set<String> statements;
16+
@Nullable private final String operationName;
17+
private final Set<String> queryTexts;
1818

1919
private MultiQuery(
20-
@Nullable String mainIdentifier, @Nullable String operation, Set<String> statements) {
20+
@Nullable String mainIdentifier, @Nullable String operationName, Set<String> queryTexts) {
2121
this.mainIdentifier = mainIdentifier;
22-
this.operation = operation;
23-
this.statements = statements;
22+
this.operationName = operationName;
23+
this.queryTexts = queryTexts;
2424
}
2525

2626
static MultiQuery analyze(
2727
Collection<String> rawQueryTexts, boolean statementSanitizationEnabled) {
2828
UniqueValue uniqueMainIdentifier = new UniqueValue();
29-
UniqueValue uniqueOperation = new UniqueValue();
30-
Set<String> uniqueStatements = new LinkedHashSet<>();
29+
UniqueValue uniqueOperationName = new UniqueValue();
30+
Set<String> uniqueQueryTexts = new LinkedHashSet<>();
3131
for (String rawQueryText : rawQueryTexts) {
3232
SqlStatementInfo sanitizedStatement = SqlStatementSanitizerUtil.sanitize(rawQueryText);
3333
String mainIdentifier = sanitizedStatement.getMainIdentifier();
3434
uniqueMainIdentifier.set(mainIdentifier);
35-
String operation = sanitizedStatement.getOperation();
36-
uniqueOperation.set(operation);
37-
uniqueStatements.add(
38-
statementSanitizationEnabled ? sanitizedStatement.getFullStatement() : rawQueryText);
35+
String operationName = sanitizedStatement.getOperationName();
36+
uniqueOperationName.set(operationName);
37+
uniqueQueryTexts.add(
38+
statementSanitizationEnabled ? sanitizedStatement.getQueryText() : rawQueryText);
3939
}
4040

4141
return new MultiQuery(
42-
uniqueMainIdentifier.getValue(), uniqueOperation.getValue(), uniqueStatements);
42+
uniqueMainIdentifier.getValue(), uniqueOperationName.getValue(), uniqueQueryTexts);
4343
}
4444

4545
@Nullable
4646
public String getMainIdentifier() {
4747
return mainIdentifier;
4848
}
4949

50+
@Nullable
51+
public String getOperationName() {
52+
return operationName;
53+
}
54+
55+
/**
56+
* @deprecated Use {@link #getOperationName()} instead.
57+
*/
58+
@Deprecated
5059
@Nullable
5160
public String getOperation() {
52-
return operation;
61+
return getOperationName();
62+
}
63+
64+
public Set<String> getQueryTexts() {
65+
return queryTexts;
5366
}
5467

68+
/**
69+
* @deprecated Use {@link #getQueryTexts()} instead.
70+
*/
71+
@Deprecated
5572
public Set<String> getStatements() {
56-
return statements;
73+
return getQueryTexts();
5774
}
5875

5976
private static class UniqueValue {

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlClientAttributesExtractor.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
9494
if (rawQueryTexts.size() == 1) { // for backcompat(?)
9595
String rawQueryText = rawQueryTexts.iterator().next();
9696
SqlStatementInfo sanitizedStatement = SqlStatementSanitizerUtil.sanitize(rawQueryText);
97-
String operation = sanitizedStatement.getOperation();
97+
String operationName = sanitizedStatement.getOperationName();
9898
internalSet(
9999
attributes,
100100
DB_STATEMENT,
101-
statementSanitizationEnabled ? sanitizedStatement.getFullStatement() : rawQueryText);
102-
internalSet(attributes, DB_OPERATION, operation);
103-
if (!SQL_CALL.equals(operation)) {
101+
statementSanitizationEnabled ? sanitizedStatement.getQueryText() : rawQueryText);
102+
internalSet(attributes, DB_OPERATION, operationName);
103+
if (!SQL_CALL.equals(operationName)) {
104104
internalSet(attributes, oldSemconvTableAttribute, sanitizedStatement.getMainIdentifier());
105105
}
106106
}
@@ -113,26 +113,30 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
113113
if (rawQueryTexts.size() == 1) {
114114
String rawQueryText = rawQueryTexts.iterator().next();
115115
SqlStatementInfo sanitizedStatement = SqlStatementSanitizerUtil.sanitize(rawQueryText);
116-
String operation = sanitizedStatement.getOperation();
116+
String operationName = sanitizedStatement.getOperationName();
117117
internalSet(
118118
attributes,
119119
DB_QUERY_TEXT,
120-
statementSanitizationEnabled ? sanitizedStatement.getFullStatement() : rawQueryText);
121-
internalSet(attributes, DB_OPERATION_NAME, isBatch ? "BATCH " + operation : operation);
122-
if (!SQL_CALL.equals(operation)) {
120+
statementSanitizationEnabled ? sanitizedStatement.getQueryText() : rawQueryText);
121+
internalSet(
122+
attributes, DB_OPERATION_NAME, isBatch ? "BATCH " + operationName : operationName);
123+
if (!SQL_CALL.equals(operationName)) {
123124
internalSet(attributes, DB_COLLECTION_NAME, sanitizedStatement.getMainIdentifier());
124125
}
125126
} else if (rawQueryTexts.size() > 1) {
126127
MultiQuery multiQuery =
127128
MultiQuery.analyze(getter.getRawQueryTexts(request), statementSanitizationEnabled);
128-
internalSet(attributes, DB_QUERY_TEXT, join("; ", multiQuery.getStatements()));
129+
internalSet(attributes, DB_QUERY_TEXT, join("; ", multiQuery.getQueryTexts()));
129130

130-
String operation =
131-
multiQuery.getOperation() != null ? "BATCH " + multiQuery.getOperation() : "BATCH";
132-
internalSet(attributes, DB_OPERATION_NAME, operation);
131+
String operationName =
132+
multiQuery.getOperationName() != null
133+
? "BATCH " + multiQuery.getOperationName()
134+
: "BATCH";
135+
internalSet(attributes, DB_OPERATION_NAME, operationName);
133136

134137
if (multiQuery.getMainIdentifier() != null
135-
&& (multiQuery.getOperation() == null || !SQL_CALL.equals(multiQuery.getOperation()))) {
138+
&& (multiQuery.getOperationName() == null
139+
|| !SQL_CALL.equals(multiQuery.getOperationName()))) {
136140
internalSet(attributes, DB_COLLECTION_NAME, multiQuery.getMainIdentifier());
137141
}
138142
}

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlStatementInfo.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,33 @@
1212
public abstract class SqlStatementInfo {
1313

1414
public static SqlStatementInfo create(
15-
@Nullable String fullStatement, @Nullable String operation, @Nullable String identifier) {
16-
return new AutoValue_SqlStatementInfo(fullStatement, operation, identifier);
15+
@Nullable String queryText, @Nullable String operationName, @Nullable String mainIdentifier) {
16+
return new AutoValue_SqlStatementInfo(queryText, operationName, mainIdentifier);
1717
}
1818

1919
@Nullable
20-
public abstract String getFullStatement();
20+
public abstract String getQueryText();
2121

22+
/**
23+
* @deprecated Use {@link #getQueryText()} instead.
24+
*/
25+
@Deprecated
2226
@Nullable
23-
public abstract String getOperation();
27+
public String getFullStatement() {
28+
return getQueryText();
29+
}
30+
31+
@Nullable
32+
public abstract String getOperationName();
33+
34+
/**
35+
* @deprecated Use {@link #getOperationName()} instead.
36+
*/
37+
@Deprecated
38+
@Nullable
39+
public String getOperation() {
40+
return getOperationName();
41+
}
2442

2543
@Nullable
2644
public abstract String getMainIdentifier();

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,32 @@ class SqlStatementSanitizerTest {
2121
@MethodSource("sqlArgs")
2222
void sanitizeSql(String original, String expected) {
2323
SqlStatementInfo result = SqlStatementSanitizer.create(true).sanitize(original);
24-
assertThat(result.getFullStatement()).isEqualTo(expected);
24+
assertThat(result.getQueryText()).isEqualTo(expected);
2525
}
2626

2727
@ParameterizedTest
2828
@MethodSource("couchbaseArgs")
2929
void normalizeCouchbase(String original, String expected) {
3030
SqlStatementInfo result =
3131
SqlStatementSanitizer.create(true).sanitize(original, SqlDialect.COUCHBASE);
32-
assertThat(result.getFullStatement()).isEqualTo(expected);
32+
assertThat(result.getQueryText()).isEqualTo(expected);
3333
}
3434

3535
@ParameterizedTest
3636
@MethodSource("simplifyArgs")
3737
void simplifySql(String original, Function<String, SqlStatementInfo> expectedFunction) {
3838
SqlStatementInfo result = SqlStatementSanitizer.create(true).sanitize(original);
3939
SqlStatementInfo expected = expectedFunction.apply(original);
40-
assertThat(result.getFullStatement()).isEqualTo(expected.getFullStatement());
41-
assertThat(result.getOperation()).isEqualTo(expected.getOperation());
40+
assertThat(result.getQueryText()).isEqualTo(expected.getQueryText());
41+
assertThat(result.getOperationName()).isEqualTo(expected.getOperationName());
4242
assertThat(result.getMainIdentifier()).isEqualToIgnoringCase(expected.getMainIdentifier());
4343
}
4444

4545
@ParameterizedTest
4646
@MethodSource("sensitiveArgs")
4747
void sanitizeSensitive(String original, String expected) {
4848
SqlStatementInfo result = SqlStatementSanitizer.create(true).sanitize(original);
49-
assertThat(result.getFullStatement()).isEqualTo(expected);
49+
assertThat(result.getQueryText()).isEqualTo(expected);
5050
}
5151

5252
private static Stream<Arguments> sensitiveArgs() {
@@ -86,8 +86,8 @@ void checkDdlOperationStatementsAreOk(
8686
String actual, Function<String, SqlStatementInfo> expectFunc) {
8787
SqlStatementInfo result = SqlStatementSanitizer.create(true).sanitize(actual);
8888
SqlStatementInfo expected = expectFunc.apply(actual);
89-
assertThat(result.getFullStatement()).isEqualTo(expected.getFullStatement());
90-
assertThat(result.getOperation()).isEqualTo(expected.getOperation());
89+
assertThat(result.getQueryText()).isEqualTo(expected.getQueryText());
90+
assertThat(result.getOperationName()).isEqualTo(expected.getOperationName());
9191
assertThat(result.getMainIdentifier()).isEqualTo(expected.getMainIdentifier());
9292
}
9393

@@ -108,7 +108,7 @@ void veryLongNumbersAreOk() {
108108
s += String.valueOf(i);
109109
}
110110
SqlStatementInfo result = SqlStatementSanitizer.create(true).sanitize(s);
111-
assertThat(result.getFullStatement()).isEqualTo("?");
111+
assertThat(result.getQueryText()).isEqualTo("?");
112112
}
113113

114114
@Test
@@ -118,7 +118,7 @@ void veryLongNumbersAtEndOfTableAreOk() {
118118
s += String.valueOf(i);
119119
}
120120
SqlStatementInfo result = SqlStatementSanitizer.create(true).sanitize(s);
121-
assertThat(result.getFullStatement()).isEqualTo(s.substring(0, AutoSqlSanitizer.LIMIT));
121+
assertThat(result.getQueryText()).isEqualTo(s.substring(0, AutoSqlSanitizer.LIMIT));
122122
}
123123

124124
@Test
@@ -127,7 +127,7 @@ void test32kTruncation() {
127127
for (int i = 0; i < 10000; i++) {
128128
s.append("SELECT * FROM TABLE WHERE FIELD = 1234 AND ");
129129
}
130-
String sanitized = SqlStatementSanitizer.create(true).sanitize(s.toString()).getFullStatement();
130+
String sanitized = SqlStatementSanitizer.create(true).sanitize(s.toString()).getQueryText();
131131
assertThat(sanitized.length()).isLessThanOrEqualTo(AutoSqlSanitizer.LIMIT);
132132
assertThat(sanitized).doesNotContain("1234");
133133
}
@@ -152,7 +152,7 @@ public void longInStatementDoesntCauseStackOverflow() {
152152
}
153153
s.append("?)");
154154

155-
String sanitized = SqlStatementSanitizer.create(true).sanitize(s.toString()).getFullStatement();
155+
String sanitized = SqlStatementSanitizer.create(true).sanitize(s.toString()).getQueryText();
156156

157157
assertThat(sanitized).isEqualTo("select col from table where col in (?)");
158158
}
@@ -162,7 +162,7 @@ public void largeStatementCached() {
162162
// test that short statement is cached
163163
String shortStatement = "SELECT * FROM TABLE WHERE FIELD = 1234";
164164
String sanitizedShort =
165-
SqlStatementSanitizer.create(true).sanitize(shortStatement).getFullStatement();
165+
SqlStatementSanitizer.create(true).sanitize(shortStatement).getQueryText();
166166
assertThat(sanitizedShort).doesNotContain("1234");
167167
assertThat(SqlStatementSanitizer.isCached(shortStatement)).isTrue();
168168

@@ -173,7 +173,7 @@ public void largeStatementCached() {
173173
}
174174
String largeStatement = s.toString();
175175
String sanitizedLarge =
176-
SqlStatementSanitizer.create(true).sanitize(largeStatement).getFullStatement();
176+
SqlStatementSanitizer.create(true).sanitize(largeStatement).getQueryText();
177177
assertThat(sanitizedLarge).doesNotContain("1234");
178178
assertThat(SqlStatementSanitizer.isCached(largeStatement)).isFalse();
179179
}

0 commit comments

Comments
 (0)