Skip to content

Commit 3b3feaa

Browse files
committed
Extract onStartCommon
1 parent b3f6bc0 commit 3b3feaa

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
5858
@SuppressWarnings("deprecation") // until old db semconv are dropped
5959
@Override
6060
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
61+
onStartCommon(attributes, getter, request);
62+
}
63+
64+
@SuppressWarnings("deprecation") // until old db semconv are dropped
65+
static <REQUEST, RESPONSE> void onStartCommon(
66+
AttributesBuilder attributes,
67+
DbClientAttributesGetter<REQUEST, RESPONSE> getter,
68+
REQUEST request) {
6169
if (SemconvStability.emitStableDatabaseSemconv()) {
6270
internalSet(
6371
attributes,

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,8 @@ public static <REQUEST, RESPONSE> SqlClientAttributesExtractorBuilder<REQUEST, R
8888
@SuppressWarnings("deprecation") // until old db semconv are dropped
8989
@Override
9090
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
91-
// Common attributes
92-
if (SemconvStability.emitStableDatabaseSemconv()) {
93-
internalSet(
94-
attributes,
95-
DB_SYSTEM_NAME,
96-
SemconvStability.stableDbSystemName(getter.getDbSystem(request)));
97-
internalSet(attributes, DB_NAMESPACE, getter.getDbNamespace(request));
98-
}
99-
if (SemconvStability.emitOldDatabaseSemconv()) {
100-
internalSet(attributes, DB_SYSTEM, getter.getDbSystem(request));
101-
internalSet(attributes, DB_USER, getter.getUser(request));
102-
internalSet(attributes, DB_NAME, getter.getDbNamespace(request));
103-
internalSet(attributes, DB_CONNECTION_STRING, getter.getConnectionString(request));
104-
}
105-
106-
// SQL-specific attributes
10791
Collection<String> rawQueryTexts = getter.getRawQueryTexts(request);
10892

109-
if (rawQueryTexts.isEmpty()) {
110-
return;
111-
}
112-
11393
Long batchSize = getter.getBatchSize(request);
11494
boolean isBatch = batchSize != null && batchSize > 1;
11595

@@ -145,7 +125,7 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
145125
if (!SQL_CALL.equals(operation)) {
146126
internalSet(attributes, DB_COLLECTION_NAME, sanitizedStatement.getMainIdentifier());
147127
}
148-
} else {
128+
} else if (rawQueryTexts.size() > 1) {
149129
MultiQuery multiQuery =
150130
MultiQuery.analyze(getter.getRawQueryTexts(request), statementSanitizationEnabled);
151131
internalSet(attributes, DB_QUERY_TEXT, join("; ", multiQuery.getStatements()));
@@ -163,6 +143,11 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
163143

164144
Map<String, String> queryParameters = getter.getQueryParameters(request);
165145
setQueryParameters(attributes, isBatch, queryParameters);
146+
147+
// calling this last so explicit getDbOperationName(), getDbCollectionName(),
148+
// getDbQueryText(), and getDbQuerySummary() implementations can override
149+
// the parsed values from above
150+
DbClientAttributesExtractor.onStartCommon(attributes, getter, request);
166151
}
167152

168153
private void setQueryParameters(

0 commit comments

Comments
 (0)