Skip to content

Commit e2cce4c

Browse files
Db client error type for JDBC (#13331)
Co-authored-by: Jay DeLuca <[email protected]>
1 parent 67f99cb commit e2cce4c

File tree

45 files changed

+378
-108
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+378
-108
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@
2323
*/
2424
public final class DbClientAttributesExtractor<REQUEST, RESPONSE>
2525
extends DbClientCommonAttributesExtractor<
26-
REQUEST, RESPONSE, DbClientAttributesGetter<REQUEST>> {
26+
REQUEST, RESPONSE, DbClientAttributesGetter<REQUEST, RESPONSE>> {
2727

2828
// copied from DbIncubatingAttributes
2929
private static final AttributeKey<String> DB_STATEMENT = AttributeKey.stringKey("db.statement");
3030
private static final AttributeKey<String> DB_QUERY_TEXT = AttributeKey.stringKey("db.query.text");
3131
static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");
3232
static final AttributeKey<String> DB_OPERATION_NAME = AttributeKey.stringKey("db.operation.name");
33-
static final AttributeKey<Long> DB_RESPONSE_STATUS_CODE =
34-
AttributeKey.longKey("db.response.status_code");
33+
static final AttributeKey<String> DB_RESPONSE_STATUS_CODE =
34+
AttributeKey.stringKey("db.response.status_code");
3535

3636
/** Creates the database client attributes extractor with default configuration. */
3737
public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
38-
DbClientAttributesGetter<REQUEST> getter) {
38+
DbClientAttributesGetter<REQUEST, RESPONSE> getter) {
3939
return new DbClientAttributesExtractor<>(getter);
4040
}
4141

42-
DbClientAttributesExtractor(DbClientAttributesGetter<REQUEST> getter) {
42+
DbClientAttributesExtractor(DbClientAttributesGetter<REQUEST, RESPONSE> getter) {
4343
super(getter);
4444
}
4545

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
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, RESPONSE>
22+
extends DbClientCommonAttributesGetter<REQUEST, RESPONSE> {
2223

2324
/**
2425
* @deprecated Use {@link #getDbQueryText(REQUEST)} instead.

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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.ErrorAttributes.ERROR_TYPE;
910

1011
import io.opentelemetry.api.common.AttributeKey;
1112
import io.opentelemetry.api.common.AttributesBuilder;
@@ -17,7 +18,7 @@
1718
import javax.annotation.Nullable;
1819

1920
abstract class DbClientCommonAttributesExtractor<
20-
REQUEST, RESPONSE, GETTER extends DbClientCommonAttributesGetter<REQUEST>>
21+
REQUEST, RESPONSE, GETTER extends DbClientCommonAttributesGetter<REQUEST, RESPONSE>>
2122
implements AttributesExtractor<REQUEST, RESPONSE>, SpanKeyProvider {
2223

2324
// copied from DbIncubatingAttributes
@@ -29,6 +30,8 @@ abstract class DbClientCommonAttributesExtractor<
2930
private static final AttributeKey<String> DB_USER = AttributeKey.stringKey("db.user");
3031
private static final AttributeKey<String> DB_CONNECTION_STRING =
3132
AttributeKey.stringKey("db.connection_string");
33+
private static final AttributeKey<String> DB_RESPONSE_STATUS_CODE =
34+
AttributeKey.stringKey("db.response.status_code");
3235

3336
final GETTER getter;
3437

@@ -60,7 +63,16 @@ public final void onEnd(
6063
Context context,
6164
REQUEST request,
6265
@Nullable RESPONSE response,
63-
@Nullable Throwable error) {}
66+
@Nullable Throwable error) {
67+
if (SemconvStability.emitStableDatabaseSemconv()) {
68+
if (error != null) {
69+
internalSet(attributes, ERROR_TYPE, error.getClass().getName());
70+
}
71+
if (error != null || response != null) {
72+
internalSet(attributes, DB_RESPONSE_STATUS_CODE, getter.getResponseStatus(response, error));
73+
}
74+
}
75+
}
6476

6577
/**
6678
* This method is internal and is hence not for public use. Its API is unstable and can change at

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
@@ -8,7 +8,7 @@
88
import javax.annotation.Nullable;
99

1010
/** An interface for getting attributes common to database clients. */
11-
public interface DbClientCommonAttributesGetter<REQUEST> {
11+
public interface DbClientCommonAttributesGetter<REQUEST, RESPONSE> {
1212

1313
@Deprecated
1414
@Nullable
@@ -44,4 +44,9 @@ default String getDbNamespace(REQUEST request) {
4444
@Deprecated
4545
@Nullable
4646
String getConnectionString(REQUEST request);
47+
48+
@Nullable
49+
default String getResponseStatus(@Nullable RESPONSE response, @Nullable Throwable error) {
50+
return null;
51+
}
4752
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ static void applyClientDurationAdvice(DoubleHistogramBuilder builder) {
3232
SqlClientAttributesExtractor.DB_COLLECTION_NAME,
3333
DbClientCommonAttributesExtractor.DB_NAMESPACE,
3434
DbClientAttributesExtractor.DB_OPERATION_NAME,
35-
// will be implemented in
36-
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/12804
3735
DbClientAttributesExtractor.DB_RESPONSE_STATUS_CODE,
38-
// will be implemented in
39-
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/12804
4036
ErrorAttributes.ERROR_TYPE,
4137
NetworkAttributes.NETWORK_PEER_ADDRESS,
4238
NetworkAttributes.NETWORK_PEER_PORT,

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public abstract class DbClientSpanNameExtractor<REQUEST> implements SpanNameExtr
2020
* @see DbClientAttributesGetter#getDbNamespace(Object) used to extract {@code <db.namespace>}.
2121
*/
2222
public static <REQUEST> SpanNameExtractor<REQUEST> create(
23-
DbClientAttributesGetter<REQUEST> getter) {
23+
DbClientAttributesGetter<REQUEST, ?> getter) {
2424
return new GenericDbClientSpanNameExtractor<>(getter);
2525
}
2626

@@ -34,7 +34,7 @@ public static <REQUEST> SpanNameExtractor<REQUEST> create(
3434
* procedure name.
3535
*/
3636
public static <REQUEST> SpanNameExtractor<REQUEST> create(
37-
SqlClientAttributesGetter<REQUEST> getter) {
37+
SqlClientAttributesGetter<REQUEST, ?> getter) {
3838
return new SqlClientSpanNameExtractor<>(getter);
3939
}
4040

@@ -67,9 +67,9 @@ protected String computeSpanName(String dbName, String operation, String mainIde
6767
private static final class GenericDbClientSpanNameExtractor<REQUEST>
6868
extends DbClientSpanNameExtractor<REQUEST> {
6969

70-
private final DbClientAttributesGetter<REQUEST> getter;
70+
private final DbClientAttributesGetter<REQUEST, ?> getter;
7171

72-
private GenericDbClientSpanNameExtractor(DbClientAttributesGetter<REQUEST> getter) {
72+
private GenericDbClientSpanNameExtractor(DbClientAttributesGetter<REQUEST, ?> getter) {
7373
this.getter = getter;
7474
}
7575

@@ -87,9 +87,9 @@ private static final class SqlClientSpanNameExtractor<REQUEST>
8787
// a dedicated sanitizer just for extracting the operation and identifier name
8888
private static final SqlStatementSanitizer sanitizer = SqlStatementSanitizer.create(true);
8989

90-
private final SqlClientAttributesGetter<REQUEST> getter;
90+
private final SqlClientAttributesGetter<REQUEST, ?> getter;
9191

92-
private SqlClientSpanNameExtractor(SqlClientAttributesGetter<REQUEST> getter) {
92+
private SqlClientSpanNameExtractor(SqlClientAttributesGetter<REQUEST, ?> getter) {
9393
this.getter = getter;
9494
}
9595

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
public final class SqlClientAttributesExtractor<REQUEST, RESPONSE>
2828
extends DbClientCommonAttributesExtractor<
29-
REQUEST, RESPONSE, SqlClientAttributesGetter<REQUEST>> {
29+
REQUEST, RESPONSE, SqlClientAttributesGetter<REQUEST, RESPONSE>> {
3030

3131
// copied from DbIncubatingAttributes
3232
private static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");
@@ -41,7 +41,7 @@ public final class SqlClientAttributesExtractor<REQUEST, RESPONSE>
4141

4242
/** Creates the SQL client attributes extractor with default configuration. */
4343
public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
44-
SqlClientAttributesGetter<REQUEST> getter) {
44+
SqlClientAttributesGetter<REQUEST, RESPONSE> getter) {
4545
return SqlClientAttributesExtractor.<REQUEST, RESPONSE>builder(getter).build();
4646
}
4747

@@ -50,7 +50,7 @@ public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
5050
* client attributes extractor.
5151
*/
5252
public static <REQUEST, RESPONSE> SqlClientAttributesExtractorBuilder<REQUEST, RESPONSE> builder(
53-
SqlClientAttributesGetter<REQUEST> getter) {
53+
SqlClientAttributesGetter<REQUEST, RESPONSE> getter) {
5454
return new SqlClientAttributesExtractorBuilder<>(getter);
5555
}
5656

@@ -62,7 +62,7 @@ public static <REQUEST, RESPONSE> SqlClientAttributesExtractorBuilder<REQUEST, R
6262
private final boolean statementSanitizationEnabled;
6363

6464
SqlClientAttributesExtractor(
65-
SqlClientAttributesGetter<REQUEST> getter,
65+
SqlClientAttributesGetter<REQUEST, RESPONSE> getter,
6666
AttributeKey<String> oldSemconvTableAttribute,
6767
boolean statementSanitizationEnabled) {
6868
super(getter);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ public final class SqlClientAttributesExtractorBuilder<REQUEST, RESPONSE> {
1717
// copied from DbIncubatingAttributes
1818
private static final AttributeKey<String> DB_SQL_TABLE = AttributeKey.stringKey("db.sql.table");
1919

20-
final SqlClientAttributesGetter<REQUEST> getter;
20+
final SqlClientAttributesGetter<REQUEST, RESPONSE> getter;
2121
AttributeKey<String> oldSemconvTableAttribute = DB_SQL_TABLE;
2222
boolean statementSanitizationEnabled = true;
2323

24-
SqlClientAttributesExtractorBuilder(SqlClientAttributesGetter<REQUEST> getter) {
24+
SqlClientAttributesExtractorBuilder(SqlClientAttributesGetter<REQUEST, RESPONSE> getter) {
2525
this.getter = getter;
2626
}
2727

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
* from the attribute methods, but implement as many as possible for best compliance with the
2323
* OpenTelemetry specification.
2424
*/
25-
public interface SqlClientAttributesGetter<REQUEST>
26-
extends DbClientCommonAttributesGetter<REQUEST> {
25+
public interface SqlClientAttributesGetter<REQUEST, RESPONSE>
26+
extends DbClientCommonAttributesGetter<REQUEST, RESPONSE> {
2727

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
class DbClientAttributesExtractorTest {
2323

24-
static final class TestAttributesGetter implements DbClientAttributesGetter<Map<String, String>> {
24+
static final class TestAttributesGetter
25+
implements DbClientAttributesGetter<Map<String, String>, Void> {
2526
@Override
2627
public String getDbSystem(Map<String, String> map) {
2728
return map.get("db.system");

0 commit comments

Comments
 (0)