Skip to content

Commit 5a03eca

Browse files
authored
Deprecate DbClientCommonAttributesGetter (#15139)
1 parent 15de4e8 commit 5a03eca

File tree

6 files changed

+139
-101
lines changed

6 files changed

+139
-101
lines changed

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

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@
66
package io.opentelemetry.instrumentation.api.incubator.semconv.db;
77

88
import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
9+
import static io.opentelemetry.semconv.DbAttributes.DB_NAMESPACE;
910
import static io.opentelemetry.semconv.DbAttributes.DB_OPERATION_NAME;
1011
import static io.opentelemetry.semconv.DbAttributes.DB_QUERY_SUMMARY;
1112
import static io.opentelemetry.semconv.DbAttributes.DB_QUERY_TEXT;
13+
import static io.opentelemetry.semconv.DbAttributes.DB_RESPONSE_STATUS_CODE;
14+
import static io.opentelemetry.semconv.DbAttributes.DB_SYSTEM_NAME;
15+
import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE;
1216

1317
import io.opentelemetry.api.common.AttributeKey;
1418
import io.opentelemetry.api.common.AttributesBuilder;
1519
import io.opentelemetry.context.Context;
1620
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
1721
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
22+
import io.opentelemetry.instrumentation.api.internal.SpanKey;
23+
import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider;
24+
import javax.annotation.Nullable;
1825

1926
/**
2027
* Extractor of <a
@@ -25,12 +32,18 @@
2532
* attribute extraction from request/response objects.
2633
*/
2734
public final class DbClientAttributesExtractor<REQUEST, RESPONSE>
28-
extends DbClientCommonAttributesExtractor<
29-
REQUEST, RESPONSE, DbClientAttributesGetter<REQUEST, RESPONSE>> {
35+
implements AttributesExtractor<REQUEST, RESPONSE>, SpanKeyProvider {
3036

3137
// copied from DbIncubatingAttributes
38+
private static final AttributeKey<String> DB_NAME = AttributeKey.stringKey("db.name");
39+
private static final AttributeKey<String> DB_SYSTEM = AttributeKey.stringKey("db.system");
40+
private static final AttributeKey<String> DB_USER = AttributeKey.stringKey("db.user");
41+
private static final AttributeKey<String> DB_CONNECTION_STRING =
42+
AttributeKey.stringKey("db.connection_string");
3243
private static final AttributeKey<String> DB_STATEMENT = AttributeKey.stringKey("db.statement");
33-
static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");
44+
private static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");
45+
46+
private final DbClientAttributesGetter<REQUEST, RESPONSE> getter;
3447

3548
/** Creates the database client attributes extractor with default configuration. */
3649
public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
@@ -39,21 +52,71 @@ public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
3952
}
4053

4154
DbClientAttributesExtractor(DbClientAttributesGetter<REQUEST, RESPONSE> getter) {
42-
super(getter);
55+
this.getter = getter;
4356
}
4457

58+
@SuppressWarnings("deprecation") // until old db semconv are dropped
4559
@Override
4660
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
47-
super.onStart(attributes, parentContext, request);
61+
onStartCommon(attributes, getter, request);
62+
}
4863

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) {
4969
if (SemconvStability.emitStableDatabaseSemconv()) {
70+
internalSet(
71+
attributes,
72+
DB_SYSTEM_NAME,
73+
SemconvStability.stableDbSystemName(getter.getDbSystem(request)));
74+
internalSet(attributes, DB_NAMESPACE, getter.getDbNamespace(request));
5075
internalSet(attributes, DB_QUERY_TEXT, getter.getDbQueryText(request));
5176
internalSet(attributes, DB_OPERATION_NAME, getter.getDbOperationName(request));
5277
internalSet(attributes, DB_QUERY_SUMMARY, getter.getDbQuerySummary(request));
5378
}
5479
if (SemconvStability.emitOldDatabaseSemconv()) {
80+
internalSet(attributes, DB_SYSTEM, getter.getDbSystem(request));
81+
internalSet(attributes, DB_USER, getter.getUser(request));
82+
internalSet(attributes, DB_NAME, getter.getDbNamespace(request));
83+
internalSet(attributes, DB_CONNECTION_STRING, getter.getConnectionString(request));
5584
internalSet(attributes, DB_STATEMENT, getter.getDbQueryText(request));
5685
internalSet(attributes, DB_OPERATION, getter.getDbOperationName(request));
5786
}
5887
}
88+
89+
@Override
90+
public void onEnd(
91+
AttributesBuilder attributes,
92+
Context context,
93+
REQUEST request,
94+
@Nullable RESPONSE response,
95+
@Nullable Throwable error) {
96+
onEndCommon(attributes, getter, response, error);
97+
}
98+
99+
static <REQUEST, RESPONSE> void onEndCommon(
100+
AttributesBuilder attributes,
101+
DbClientAttributesGetter<REQUEST, RESPONSE> getter,
102+
@Nullable RESPONSE response,
103+
@Nullable Throwable error) {
104+
if (SemconvStability.emitStableDatabaseSemconv()) {
105+
if (error != null) {
106+
internalSet(attributes, ERROR_TYPE, error.getClass().getName());
107+
}
108+
if (error != null || response != null) {
109+
internalSet(attributes, DB_RESPONSE_STATUS_CODE, getter.getResponseStatus(response, error));
110+
}
111+
}
112+
}
113+
114+
/**
115+
* This method is internal and is hence not for public use. Its API is unstable and can change at
116+
* any time.
117+
*/
118+
@Override
119+
public SpanKey internalGetSpanKey() {
120+
return SpanKey.DB_CLIENT;
121+
}
59122
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* from the attribute methods, but implement as many as possible for best compliance with the
1919
* OpenTelemetry specification.
2020
*/
21+
@SuppressWarnings("deprecation") // extending deprecated interface for backward compatibility
2122
public interface DbClientAttributesGetter<REQUEST, RESPONSE>
2223
extends DbClientCommonAttributesGetter<REQUEST, RESPONSE> {
2324

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

Lines changed: 0 additions & 83 deletions
This file was deleted.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
import javax.annotation.Nullable;
99

10-
/** An interface for getting attributes common to database clients. */
10+
/**
11+
* An interface for getting attributes common to database clients.
12+
*
13+
* @deprecated Use {@link DbClientAttributesGetter} instead.
14+
*/
15+
@Deprecated
1116
public interface DbClientCommonAttributesGetter<REQUEST, RESPONSE> {
1217

1318
String getDbSystem(REQUEST request);

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

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
import io.opentelemetry.context.Context;
1717
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
1818
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
19+
import io.opentelemetry.instrumentation.api.internal.SpanKey;
20+
import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider;
1921
import io.opentelemetry.semconv.AttributeKeyTemplate;
2022
import java.util.Collection;
2123
import java.util.Map;
24+
import javax.annotation.Nullable;
2225

2326
/**
2427
* Extractor of <a
@@ -31,8 +34,7 @@
3134
* statement parameters are removed.
3235
*/
3336
public final class SqlClientAttributesExtractor<REQUEST, RESPONSE>
34-
extends DbClientCommonAttributesExtractor<
35-
REQUEST, RESPONSE, SqlClientAttributesGetter<REQUEST, RESPONSE>> {
37+
implements AttributesExtractor<REQUEST, RESPONSE>, SpanKeyProvider {
3638

3739
// copied from DbIncubatingAttributes
3840
private static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");
@@ -57,6 +59,7 @@ public static <REQUEST, RESPONSE> SqlClientAttributesExtractorBuilder<REQUEST, R
5759

5860
private static final String SQL_CALL = "CALL";
5961

62+
private final SqlClientAttributesGetter<REQUEST, RESPONSE> getter;
6063
private final AttributeKey<String> oldSemconvTableAttribute;
6164
private final boolean statementSanitizationEnabled;
6265
private final boolean captureQueryParameters;
@@ -66,23 +69,18 @@ public static <REQUEST, RESPONSE> SqlClientAttributesExtractorBuilder<REQUEST, R
6669
AttributeKey<String> oldSemconvTableAttribute,
6770
boolean statementSanitizationEnabled,
6871
boolean captureQueryParameters) {
69-
super(getter);
72+
this.getter = getter;
7073
this.oldSemconvTableAttribute = oldSemconvTableAttribute;
7174
// capturing query parameters disables statement sanitization
7275
this.statementSanitizationEnabled = !captureQueryParameters && statementSanitizationEnabled;
7376
this.captureQueryParameters = captureQueryParameters;
7477
}
7578

79+
@SuppressWarnings("deprecation") // until old db semconv are dropped
7680
@Override
7781
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
78-
super.onStart(attributes, parentContext, request);
79-
8082
Collection<String> rawQueryTexts = getter.getRawQueryTexts(request);
8183

82-
if (rawQueryTexts.isEmpty()) {
83-
return;
84-
}
85-
8684
Long batchSize = getter.getBatchSize(request);
8785
boolean isBatch = batchSize != null && batchSize > 1;
8886

@@ -118,7 +116,7 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
118116
if (!SQL_CALL.equals(operation)) {
119117
internalSet(attributes, DB_COLLECTION_NAME, sanitizedStatement.getMainIdentifier());
120118
}
121-
} else {
119+
} else if (rawQueryTexts.size() > 1) {
122120
MultiQuery multiQuery =
123121
MultiQuery.analyze(getter.getRawQueryTexts(request), statementSanitizationEnabled);
124122
internalSet(attributes, DB_QUERY_TEXT, join("; ", multiQuery.getStatements()));
@@ -136,6 +134,11 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
136134

137135
Map<String, String> queryParameters = getter.getQueryParameters(request);
138136
setQueryParameters(attributes, isBatch, queryParameters);
137+
138+
// calling this last so explicit getDbOperationName(), getDbCollectionName(),
139+
// getDbQueryText(), and getDbQuerySummary() implementations can override
140+
// the parsed values from above
141+
DbClientAttributesExtractor.onStartCommon(attributes, getter, request);
139142
}
140143

141144
private void setQueryParameters(
@@ -160,4 +163,23 @@ private static String join(String delimiter, Collection<String> collection) {
160163
}
161164
return builder.toString();
162165
}
166+
167+
@Override
168+
public void onEnd(
169+
AttributesBuilder attributes,
170+
Context context,
171+
REQUEST request,
172+
@Nullable RESPONSE response,
173+
@Nullable Throwable error) {
174+
DbClientAttributesExtractor.onEndCommon(attributes, getter, response, error);
175+
}
176+
177+
/**
178+
* This method is internal and is hence not for public use. Its API is unstable and can change at
179+
* any time.
180+
*/
181+
@Override
182+
public SpanKey internalGetSpanKey() {
183+
return SpanKey.DB_CLIENT;
184+
}
163185
}

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,40 @@
2222
* OpenTelemetry specification.
2323
*/
2424
public interface SqlClientAttributesGetter<REQUEST, RESPONSE>
25-
extends DbClientCommonAttributesGetter<REQUEST, RESPONSE> {
25+
extends DbClientAttributesGetter<REQUEST, RESPONSE> {
2626

2727
/**
28-
* Get the raw SQL query texts. The values returned by this method is later sanitized by the
28+
* SqlClientAttributesExtractor will try to populate db.operation.name based on {@link
29+
* #getRawQueryTexts(REQUEST)}, but this can be used to explicitly provide the operation name.
30+
*/
31+
@Override
32+
@Nullable
33+
default String getDbOperationName(REQUEST request) {
34+
return null;
35+
}
36+
37+
/**
38+
* SqlClientAttributesExtractor will try to populate db.query.text based on {@link
39+
* #getRawQueryTexts(REQUEST)}, but this can be used to explicitly provide the query text.
40+
*/
41+
@Override
42+
@Nullable
43+
default String getDbQueryText(REQUEST request) {
44+
return null;
45+
}
46+
47+
/**
48+
* SqlClientAttributesExtractor will try to populate db.query.summary based on {@link
49+
* #getRawQueryTexts(REQUEST)}, but this can be used to explicitly provide the query summary.
50+
*/
51+
@Override
52+
@Nullable
53+
default String getDbQuerySummary(REQUEST request) {
54+
return null;
55+
}
56+
57+
/**
58+
* Get the raw SQL query texts. The values returned by this method are later sanitized by the
2959
* {@link SqlClientAttributesExtractor} before being set as span attribute.
3060
*
3161
* <p>If {@code request} is not a batch query, then this method should return a collection with a

0 commit comments

Comments
 (0)