Skip to content

Commit 0da891b

Browse files
committed
renames
1 parent e5e7d32 commit 0da891b

File tree

19 files changed

+314
-136
lines changed

19 files changed

+314
-136
lines changed

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

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public static <REQUEST> SpanNameExtractor<REQUEST> create(
2929
* Returns a {@link SpanNameExtractor} that constructs the span name according to DB semantic
3030
* conventions: {@code <db.operation> <db.name>.<identifier>}.
3131
*
32-
* @see SqlStatementInfo#getOperation() used to extract {@code <db.operation>}.
32+
* @see SqlStatementInfo#getOperationName() used to extract {@code <db.operation.name>}.
3333
* @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.
34+
* @see SqlStatementInfo#getCollectionName() used to extract table name.
35+
* @see SqlStatementInfo#getStoredProcedureName() used to extract stored procedure name.
3636
*/
3737
public static <REQUEST> SpanNameExtractor<REQUEST> create(
3838
SqlClientAttributesGetter<REQUEST, ?> getter) {
@@ -44,24 +44,30 @@ public static <REQUEST> SpanNameExtractor<REQUEST> create(
4444
private DbClientSpanNameExtractor() {}
4545

4646
protected String computeSpanName(
47-
@Nullable String dbName, @Nullable String operation, @Nullable String mainIdentifier) {
47+
@Nullable String dbName,
48+
@Nullable String operation,
49+
@Nullable String collectionName,
50+
@Nullable String storedProcedureName) {
51+
// Use whichever identifier is available (they're mutually exclusive)
52+
String identifier = collectionName != null ? collectionName : storedProcedureName;
53+
4854
if (operation == null) {
4955
return dbName == null ? DEFAULT_SPAN_NAME : dbName;
5056
}
5157

5258
StringBuilder name = new StringBuilder(operation);
53-
if (dbName != null || mainIdentifier != null) {
59+
if (dbName != null || identifier != null) {
5460
name.append(' ');
5561
}
5662
// skip db name if identifier already has a db name prefixed to it
57-
if (dbName != null && (mainIdentifier == null || mainIdentifier.indexOf('.') == -1)) {
63+
if (dbName != null && (identifier == null || identifier.indexOf('.') == -1)) {
5864
name.append(dbName);
59-
if (mainIdentifier != null) {
65+
if (identifier != null) {
6066
name.append('.');
6167
}
6268
}
63-
if (mainIdentifier != null) {
64-
name.append(mainIdentifier);
69+
if (identifier != null) {
70+
name.append(identifier);
6571
}
6672
return name.toString();
6773
}
@@ -79,7 +85,7 @@ private GenericDbClientSpanNameExtractor(DbClientAttributesGetter<REQUEST, ?> ge
7985
public String extract(REQUEST request) {
8086
String namespace = getter.getDbNamespace(request);
8187
String operationName = getter.getDbOperationName(request);
82-
return computeSpanName(namespace, operationName, null);
88+
return computeSpanName(namespace, operationName, null, null);
8389
}
8490
}
8591

@@ -98,17 +104,20 @@ public String extract(REQUEST request) {
98104
Collection<String> rawQueryTexts = getter.getRawQueryTexts(request);
99105

100106
if (rawQueryTexts.isEmpty()) {
101-
return computeSpanName(namespace, null, null);
107+
return computeSpanName(namespace, null, null, null);
102108
}
103109

104110
if (!SemconvStability.emitStableDatabaseSemconv()) {
105111
if (rawQueryTexts.size() > 1) { // for backcompat(?)
106-
return computeSpanName(namespace, null, null);
112+
return computeSpanName(namespace, null, null, null);
107113
}
108114
SqlStatementInfo sanitizedStatement =
109115
SqlStatementSanitizerUtil.sanitize(rawQueryTexts.iterator().next());
110116
return computeSpanName(
111-
namespace, sanitizedStatement.getOperation(), sanitizedStatement.getMainIdentifier());
117+
namespace,
118+
sanitizedStatement.getOperationName(),
119+
sanitizedStatement.getCollectionName(),
120+
sanitizedStatement.getStoredProcedureName());
112121
}
113122

114123
// For stable semconv, use query summary as span name if available
@@ -123,23 +132,25 @@ public String extract(REQUEST request) {
123132
return querySummary;
124133
}
125134
// Fall back to old behavior if no query summary
126-
String operation = sanitizedStatement.getOperation();
135+
String operation = sanitizedStatement.getOperationName();
127136
if (isBatch(request)) {
128137
operation = "BATCH " + operation;
129138
}
130-
return computeSpanName(namespace, operation, sanitizedStatement.getMainIdentifier());
139+
return computeSpanName(
140+
namespace,
141+
operation,
142+
sanitizedStatement.getCollectionName(),
143+
sanitizedStatement.getStoredProcedureName());
131144
}
132145

133146
MultiQuery multiQuery = MultiQuery.analyze(rawQueryTexts, false);
134147
String querySummary = multiQuery.getQuerySummary();
135-
if (querySummary != null) {
136-
return "BATCH " + querySummary;
148+
// Fall back to old behavior if query summary equals operation (no common table)
149+
if (!querySummary.equals(multiQuery.getOperationName())) {
150+
return querySummary;
137151
}
138-
// Fall back to old behavior if no query summary
139152
return computeSpanName(
140-
namespace,
141-
multiQuery.getOperation() != null ? "BATCH " + multiQuery.getOperation() : "BATCH",
142-
multiQuery.getMainIdentifier());
153+
namespace, multiQuery.getOperationName(), multiQuery.getCollectionName(), null);
143154
}
144155

145156
private boolean isBatch(REQUEST request) {

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

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,62 @@
1212

1313
class MultiQuery {
1414

15-
@Nullable private final String mainIdentifier;
16-
@Nullable private final String operation;
17-
@Nullable private final String querySummary;
18-
private final Set<String> statements;
15+
@Nullable private final String collectionName;
16+
private final String operationName;
17+
private final String querySummary;
18+
private final Set<String> queryTexts;
1919

2020
private MultiQuery(
21-
@Nullable String mainIdentifier,
22-
@Nullable String operation,
23-
@Nullable String querySummary,
24-
Set<String> statements) {
25-
this.mainIdentifier = mainIdentifier;
26-
this.operation = operation;
21+
@Nullable String collectionName,
22+
String operationName,
23+
String querySummary,
24+
Set<String> queryTexts) {
25+
this.collectionName = collectionName;
26+
this.operationName = operationName;
2727
this.querySummary = querySummary;
28-
this.statements = statements;
28+
this.queryTexts = queryTexts;
2929
}
3030

3131
static MultiQuery analyze(
3232
Collection<String> rawQueryTexts, boolean statementSanitizationEnabled) {
33-
UniqueValue uniqueMainIdentifier = new UniqueValue();
34-
UniqueValue uniqueOperation = new UniqueValue();
33+
UniqueValue uniqueCollectionName = new UniqueValue();
34+
UniqueValue uniqueOperationName = new UniqueValue();
3535
UniqueValue uniqueQuerySummary = new UniqueValue();
36-
Set<String> uniqueStatements = new LinkedHashSet<>();
36+
Set<String> uniqueQueryTexts = new LinkedHashSet<>();
3737
for (String rawQueryText : rawQueryTexts) {
3838
SqlStatementInfo sanitizedStatement = SqlStatementSanitizerUtil.sanitize(rawQueryText);
39-
String mainIdentifier = sanitizedStatement.getMainIdentifier();
40-
uniqueMainIdentifier.set(mainIdentifier);
41-
String operation = sanitizedStatement.getOperation();
42-
uniqueOperation.set(operation);
43-
String querySummary = sanitizedStatement.getQuerySummary();
44-
uniqueQuerySummary.set(querySummary);
45-
uniqueStatements.add(
46-
statementSanitizationEnabled ? sanitizedStatement.getFullStatement() : rawQueryText);
39+
uniqueCollectionName.set(sanitizedStatement.getCollectionName());
40+
uniqueOperationName.set(sanitizedStatement.getOperationName());
41+
uniqueQuerySummary.set(sanitizedStatement.getQuerySummary());
42+
uniqueQueryTexts.add(
43+
statementSanitizationEnabled ? sanitizedStatement.getQueryText() : rawQueryText);
4744
}
4845

49-
return new MultiQuery(
50-
uniqueMainIdentifier.getValue(),
51-
uniqueOperation.getValue(),
52-
uniqueQuerySummary.getValue(),
53-
uniqueStatements);
46+
String operationName = uniqueOperationName.getValue();
47+
String querySummary = uniqueQuerySummary.getValue();
48+
49+
String collectionName = uniqueCollectionName.getValue();
50+
String batchOperationName = operationName != null ? "BATCH " + operationName : "BATCH";
51+
String batchQuerySummary = querySummary != null ? "BATCH " + querySummary : batchOperationName;
52+
53+
return new MultiQuery(collectionName, batchOperationName, batchQuerySummary, uniqueQueryTexts);
5454
}
5555

5656
@Nullable
57-
public String getMainIdentifier() {
58-
return mainIdentifier;
57+
public String getCollectionName() {
58+
return collectionName;
5959
}
6060

61-
@Nullable
62-
public String getOperation() {
63-
return operation;
61+
public String getOperationName() {
62+
return operationName;
6463
}
6564

66-
@Nullable
6765
public String getQuerySummary() {
6866
return querySummary;
6967
}
7068

71-
public Set<String> getStatements() {
72-
return statements;
69+
public Set<String> getQueryTexts() {
70+
return queryTexts;
7371
}
7472

7573
private static class UniqueValue {

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

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ public static <REQUEST, RESPONSE> SqlClientAttributesExtractorBuilder<REQUEST, R
6060
return new SqlClientAttributesExtractorBuilder<>(getter);
6161
}
6262

63-
private static final String SQL_CALL = "CALL";
64-
6563
private final SqlClientAttributesGetter<REQUEST, RESPONSE> getter;
6664
private final InternalNetworkAttributesExtractor<REQUEST, RESPONSE> internalNetworkExtractor;
6765
private final ServerAttributesExtractor<REQUEST, RESPONSE> serverAttributesExtractor;
@@ -95,15 +93,13 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
9593
if (rawQueryTexts.size() == 1) { // for backcompat(?)
9694
String rawQueryText = rawQueryTexts.iterator().next();
9795
SqlStatementInfo sanitizedStatement = SqlStatementSanitizerUtil.sanitize(rawQueryText);
98-
String operation = sanitizedStatement.getOperation();
96+
String operation = sanitizedStatement.getOperationName();
9997
internalSet(
10098
attributes,
10199
DB_STATEMENT,
102-
statementSanitizationEnabled ? sanitizedStatement.getFullStatement() : rawQueryText);
100+
statementSanitizationEnabled ? sanitizedStatement.getQueryText() : rawQueryText);
103101
internalSet(attributes, DB_OPERATION, operation);
104-
if (!SQL_CALL.equals(operation)) {
105-
internalSet(attributes, oldSemconvTableAttribute, sanitizedStatement.getMainIdentifier());
106-
}
102+
internalSet(attributes, oldSemconvTableAttribute, sanitizedStatement.getCollectionName());
107103
}
108104
}
109105

@@ -114,38 +110,25 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
114110
if (rawQueryTexts.size() == 1) {
115111
String rawQueryText = rawQueryTexts.iterator().next();
116112
SqlStatementInfo sanitizedStatement = SqlStatementSanitizerUtil.sanitize(rawQueryText);
117-
String operation = sanitizedStatement.getOperation();
113+
String operation = sanitizedStatement.getOperationName();
118114
internalSet(
119115
attributes,
120116
DB_QUERY_TEXT,
121-
statementSanitizationEnabled ? sanitizedStatement.getFullStatement() : rawQueryText);
117+
statementSanitizationEnabled ? sanitizedStatement.getQueryText() : rawQueryText);
122118
internalSet(attributes, DB_OPERATION_NAME, isBatch ? "BATCH " + operation : operation);
123119
String querySummary = sanitizedStatement.getQuerySummary();
124120
internalSet(
125121
attributes,
126122
DB_QUERY_SUMMARY,
127123
isBatch && querySummary != null ? "BATCH " + querySummary : querySummary);
128-
if (!SQL_CALL.equals(operation)) {
129-
internalSet(attributes, DB_COLLECTION_NAME, sanitizedStatement.getMainIdentifier());
130-
}
124+
internalSet(attributes, DB_COLLECTION_NAME, sanitizedStatement.getCollectionName());
131125
} else if (rawQueryTexts.size() > 1) {
132126
MultiQuery multiQuery =
133127
MultiQuery.analyze(getter.getRawQueryTexts(request), statementSanitizationEnabled);
134-
internalSet(attributes, DB_QUERY_TEXT, join("; ", multiQuery.getStatements()));
135-
136-
String operation =
137-
multiQuery.getOperation() != null ? "BATCH " + multiQuery.getOperation() : "BATCH";
138-
internalSet(attributes, DB_OPERATION_NAME, operation);
139-
String querySummary = multiQuery.getQuerySummary();
140-
internalSet(
141-
attributes,
142-
DB_QUERY_SUMMARY,
143-
querySummary != null ? "BATCH " + querySummary : null);
144-
145-
if (multiQuery.getMainIdentifier() != null
146-
&& (multiQuery.getOperation() == null || !SQL_CALL.equals(multiQuery.getOperation()))) {
147-
internalSet(attributes, DB_COLLECTION_NAME, multiQuery.getMainIdentifier());
148-
}
128+
internalSet(attributes, DB_QUERY_TEXT, join("; ", multiQuery.getQueryTexts()));
129+
internalSet(attributes, DB_OPERATION_NAME, multiQuery.getOperationName());
130+
internalSet(attributes, DB_QUERY_SUMMARY, multiQuery.getQuerySummary());
131+
internalSet(attributes, DB_COLLECTION_NAME, multiQuery.getCollectionName());
149132
}
150133
}
151134

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

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111
@AutoValue
1212
public abstract class SqlStatementInfo {
1313

14+
private static final String SQL_CALL = "CALL";
1415
private static final int QUERY_SUMMARY_MAX_LENGTH = 255;
1516

1617
public static SqlStatementInfo create(
17-
@Nullable String fullStatement,
18-
@Nullable String operation,
19-
@Nullable String identifier,
18+
@Nullable String queryText,
19+
@Nullable String operationName,
20+
@Nullable String target,
2021
@Nullable String querySummary) {
2122
String truncatedQuerySummary = truncateQuerySummary(querySummary);
2223
return new AutoValue_SqlStatementInfo(
23-
fullStatement, operation, identifier, truncatedQuerySummary);
24+
queryText, operationName, target, truncatedQuerySummary);
2425
}
2526

2627
/**
@@ -42,13 +43,64 @@ private static String truncateQuerySummary(@Nullable String querySummary) {
4243
}
4344

4445
@Nullable
45-
public abstract String getFullStatement();
46+
public abstract String getQueryText();
4647

48+
/**
49+
* @deprecated Use {@link #getQueryText()} instead.
50+
*/
51+
@Deprecated
52+
@Nullable
53+
public String getFullStatement() {
54+
return getQueryText();
55+
}
56+
57+
@Nullable
58+
public abstract String getOperationName();
59+
60+
/**
61+
* @deprecated Use {@link #getOperationName()} instead.
62+
*/
63+
@Deprecated
64+
@Nullable
65+
public String getOperation() {
66+
return getOperationName();
67+
}
68+
69+
/**
70+
* Returns the table/collection name, or null for CALL operations.
71+
*
72+
* @see #getStoredProcedureName()
73+
*/
4774
@Nullable
48-
public abstract String getOperation();
75+
public String getCollectionName() {
76+
return SQL_CALL.equals(getOperationName()) ? null : getTarget();
77+
}
78+
79+
/** Returns the stored procedure name for CALL operations, or null for other operations. */
80+
@Nullable
81+
public String getStoredProcedureName() {
82+
return SQL_CALL.equals(getOperationName()) ? getTarget() : null;
83+
}
84+
85+
/**
86+
* Returns the main identifier from the SQL statement - either the table/collection name or stored
87+
* procedure name depending on the operation.
88+
*
89+
* <p>For setting the {@code db.collection.name} attribute, use {@link #getCollectionName()}
90+
* instead which returns null for CALL operations.
91+
*
92+
* @deprecated Use {@link #getCollectionName()} for db.collection.name attribute, or {@link
93+
* #getStoredProcedureName()} for stored procedure name. This method may be used for span
94+
* names where both table and procedure names are needed.
95+
*/
96+
@Deprecated
97+
@Nullable
98+
public String getMainIdentifier() {
99+
return getTarget();
100+
}
49101

50102
@Nullable
51-
public abstract String getMainIdentifier();
103+
abstract String getTarget();
52104

53105
/**
54106
* Returns a low cardinality summary of the database query suitable for use as a span name or

0 commit comments

Comments
 (0)