Skip to content

Commit 62e11c9

Browse files
committed
Simplify db getter and extractor hierarchy
1 parent 74d07f1 commit 62e11c9

File tree

6 files changed

+102
-126
lines changed

6 files changed

+102
-126
lines changed

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

+36-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import io.opentelemetry.context.Context;
1313
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
1414
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
15+
import io.opentelemetry.instrumentation.api.internal.SpanKey;
16+
import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider;
17+
import javax.annotation.Nullable;
1518

1619
/**
1720
* Extractor of <a
@@ -22,38 +25,66 @@
2225
* attribute extraction from request/response objects.
2326
*/
2427
public final class DbClientAttributesExtractor<REQUEST, RESPONSE>
25-
extends DbClientCommonAttributesExtractor<
26-
REQUEST, RESPONSE, DbClientAttributesGetter<REQUEST>> {
28+
implements AttributesExtractor<REQUEST, RESPONSE>, SpanKeyProvider {
2729

2830
// copied from DbIncubatingAttributes
31+
private static final AttributeKey<String> DB_NAME = AttributeKey.stringKey("db.name");
32+
private static final AttributeKey<String> DB_NAMESPACE = AttributeKey.stringKey("db.namespace");
33+
private static final AttributeKey<String> DB_SYSTEM = AttributeKey.stringKey("db.system");
34+
private static final AttributeKey<String> DB_USER = AttributeKey.stringKey("db.user");
35+
private static final AttributeKey<String> DB_CONNECTION_STRING =
36+
AttributeKey.stringKey("db.connection_string");
2937
private static final AttributeKey<String> DB_STATEMENT = AttributeKey.stringKey("db.statement");
3038
private static final AttributeKey<String> DB_QUERY_TEXT = AttributeKey.stringKey("db.query.text");
3139

3240
private static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");
3341
private static final AttributeKey<String> DB_OPERATION_NAME =
3442
AttributeKey.stringKey("db.operation.name");
3543

44+
private final DbClientAttributesGetter<REQUEST> getter;
45+
3646
/** Creates the database client attributes extractor with default configuration. */
3747
public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
3848
DbClientAttributesGetter<REQUEST> getter) {
3949
return new DbClientAttributesExtractor<>(getter);
4050
}
4151

4252
DbClientAttributesExtractor(DbClientAttributesGetter<REQUEST> getter) {
43-
super(getter);
53+
this.getter = getter;
4454
}
4555

4656
@Override
57+
@SuppressWarnings("deprecation") // using deprecated semconv
4758
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
48-
super.onStart(attributes, parentContext, request);
49-
59+
internalSet(attributes, DB_SYSTEM, getter.getDbSystem(request));
5060
if (SemconvStability.emitStableDatabaseSemconv()) {
61+
internalSet(attributes, DB_NAMESPACE, getter.getDbNamespace(request));
5162
internalSet(attributes, DB_QUERY_TEXT, getter.getDbQueryText(request));
5263
internalSet(attributes, DB_OPERATION_NAME, getter.getDbOperationName(request));
5364
}
5465
if (SemconvStability.emitOldDatabaseSemconv()) {
66+
internalSet(attributes, DB_USER, getter.getUser(request));
67+
internalSet(attributes, DB_NAME, getter.getDbNamespace(request));
68+
internalSet(attributes, DB_CONNECTION_STRING, getter.getConnectionString(request));
5569
internalSet(attributes, DB_STATEMENT, getter.getDbQueryText(request));
5670
internalSet(attributes, DB_OPERATION, getter.getDbOperationName(request));
5771
}
5872
}
73+
74+
@Override
75+
public void onEnd(
76+
AttributesBuilder attributes,
77+
Context context,
78+
REQUEST request,
79+
@Nullable RESPONSE response,
80+
@Nullable Throwable error) {}
81+
82+
/**
83+
* This method is internal and is hence not for public use. Its API is unstable and can change at
84+
* any time.
85+
*/
86+
@Override
87+
public SpanKey internalGetSpanKey() {
88+
return SpanKey.DB_CLIENT;
89+
}
5990
}

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

+36-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,42 @@
1818
* from the attribute methods, but implement as many as possible for best compliance with the
1919
* OpenTelemetry specification.
2020
*/
21-
public interface DbClientAttributesGetter<REQUEST> extends DbClientCommonAttributesGetter<REQUEST> {
21+
public interface DbClientAttributesGetter<REQUEST> {
22+
23+
@Deprecated
24+
@Nullable
25+
default String getSystem(REQUEST request) {
26+
return null;
27+
}
28+
29+
// TODO: make this required to implement
30+
@Nullable
31+
default String getDbSystem(REQUEST request) {
32+
return getSystem(request);
33+
}
34+
35+
@Deprecated
36+
@Nullable
37+
String getUser(REQUEST request);
38+
39+
/**
40+
* @deprecated Use {@link #getDbNamespace(Object)} instead.
41+
*/
42+
@Deprecated
43+
@Nullable
44+
default String getName(REQUEST request) {
45+
return null;
46+
}
47+
48+
// TODO: make this required to implement
49+
@Nullable
50+
default String getDbNamespace(REQUEST request) {
51+
return getName(request);
52+
}
53+
54+
@Deprecated
55+
@Nullable
56+
String getConnectionString(REQUEST request);
2257

2358
/**
2459
* @deprecated Use {@link #getDbQueryText(REQUEST)} instead.

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

-67
This file was deleted.

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

-47
This file was deleted.

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

+29-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import io.opentelemetry.context.Context;
1313
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
1414
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
15+
import io.opentelemetry.instrumentation.api.internal.SpanKey;
16+
import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider;
17+
import javax.annotation.Nullable;
1518

1619
/**
1720
* Extractor of <a
@@ -24,8 +27,7 @@
2427
* parameters are removed.
2528
*/
2629
public final class SqlClientAttributesExtractor<REQUEST, RESPONSE>
27-
extends DbClientCommonAttributesExtractor<
28-
REQUEST, RESPONSE, SqlClientAttributesGetter<REQUEST>> {
30+
implements AttributesExtractor<REQUEST, RESPONSE>, SpanKeyProvider {
2931

3032
// copied from DbIncubatingAttributes
3133
private static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");
@@ -58,18 +60,22 @@ public static <REQUEST, RESPONSE> SqlClientAttributesExtractorBuilder<REQUEST, R
5860
private final AttributeKey<String> oldSemconvTableAttribute;
5961
private final boolean statementSanitizationEnabled;
6062

63+
private final AttributesExtractor<REQUEST, RESPONSE> delegate;
64+
private final SqlClientAttributesGetter<REQUEST> getter;
65+
6166
SqlClientAttributesExtractor(
6267
SqlClientAttributesGetter<REQUEST> getter,
6368
AttributeKey<String> oldSemconvTableAttribute,
6469
boolean statementSanitizationEnabled) {
65-
super(getter);
70+
this.delegate = DbClientAttributesExtractor.create(getter);
6671
this.oldSemconvTableAttribute = oldSemconvTableAttribute;
6772
this.statementSanitizationEnabled = statementSanitizationEnabled;
73+
this.getter = getter;
6874
}
6975

7076
@Override
7177
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
72-
super.onStart(attributes, parentContext, request);
78+
delegate.onStart(attributes, parentContext, request);
7379

7480
String rawQueryText = getter.getRawQueryText(request);
7581
SqlStatementInfo sanitizedStatement = sanitizer.sanitize(rawQueryText);
@@ -97,4 +103,23 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
97103
}
98104
}
99105
}
106+
107+
@Override
108+
public void onEnd(
109+
AttributesBuilder attributes,
110+
Context context,
111+
REQUEST request,
112+
@Nullable RESPONSE response,
113+
@Nullable Throwable error) {
114+
delegate.onEnd(attributes, context, request, response, error);
115+
}
116+
117+
/**
118+
* This method is internal and is hence not for public use. Its API is unstable and can change at
119+
* any time.
120+
*/
121+
@Override
122+
public SpanKey internalGetSpanKey() {
123+
return SpanKey.DB_CLIENT;
124+
}
100125
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
* from the attribute methods, but implement as many as possible for best compliance with the
1919
* OpenTelemetry specification.
2020
*/
21-
public interface SqlClientAttributesGetter<REQUEST>
22-
extends DbClientCommonAttributesGetter<REQUEST> {
21+
public interface SqlClientAttributesGetter<REQUEST> extends DbClientAttributesGetter<REQUEST> {
2322

2423
/**
2524
* Get the raw SQL statement. The value returned by this method is later sanitized by the {@link

0 commit comments

Comments
 (0)