From 62e11c938e7b13cf84e61ceaa862b5e228fa6247 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Sat, 9 Nov 2024 17:06:17 -0800 Subject: [PATCH] Simplify db getter and extractor hierarchy --- .../db/DbClientAttributesExtractor.java | 41 ++++++++++-- .../semconv/db/DbClientAttributesGetter.java | 37 +++++++++- .../db/DbClientCommonAttributesExtractor.java | 67 ------------------- .../db/DbClientCommonAttributesGetter.java | 47 ------------- .../db/SqlClientAttributesExtractor.java | 33 +++++++-- .../semconv/db/SqlClientAttributesGetter.java | 3 +- 6 files changed, 102 insertions(+), 126 deletions(-) delete mode 100644 instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientCommonAttributesExtractor.java delete mode 100644 instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientCommonAttributesGetter.java diff --git a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientAttributesExtractor.java b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientAttributesExtractor.java index 279f3171d192..d73167fa276d 100644 --- a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientAttributesExtractor.java +++ b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientAttributesExtractor.java @@ -12,6 +12,9 @@ import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.internal.SemconvStability; +import io.opentelemetry.instrumentation.api.internal.SpanKey; +import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider; +import javax.annotation.Nullable; /** * Extractor of - extends DbClientCommonAttributesExtractor< - REQUEST, RESPONSE, DbClientAttributesGetter> { + implements AttributesExtractor, SpanKeyProvider { // copied from DbIncubatingAttributes + private static final AttributeKey DB_NAME = AttributeKey.stringKey("db.name"); + private static final AttributeKey DB_NAMESPACE = AttributeKey.stringKey("db.namespace"); + private static final AttributeKey DB_SYSTEM = AttributeKey.stringKey("db.system"); + private static final AttributeKey DB_USER = AttributeKey.stringKey("db.user"); + private static final AttributeKey DB_CONNECTION_STRING = + AttributeKey.stringKey("db.connection_string"); private static final AttributeKey DB_STATEMENT = AttributeKey.stringKey("db.statement"); private static final AttributeKey DB_QUERY_TEXT = AttributeKey.stringKey("db.query.text"); @@ -33,6 +41,8 @@ public final class DbClientAttributesExtractor private static final AttributeKey DB_OPERATION_NAME = AttributeKey.stringKey("db.operation.name"); + private final DbClientAttributesGetter getter; + /** Creates the database client attributes extractor with default configuration. */ public static AttributesExtractor create( DbClientAttributesGetter getter) { @@ -40,20 +50,41 @@ public static AttributesExtractor create( } DbClientAttributesExtractor(DbClientAttributesGetter getter) { - super(getter); + this.getter = getter; } @Override + @SuppressWarnings("deprecation") // using deprecated semconv public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) { - super.onStart(attributes, parentContext, request); - + internalSet(attributes, DB_SYSTEM, getter.getDbSystem(request)); if (SemconvStability.emitStableDatabaseSemconv()) { + internalSet(attributes, DB_NAMESPACE, getter.getDbNamespace(request)); internalSet(attributes, DB_QUERY_TEXT, getter.getDbQueryText(request)); internalSet(attributes, DB_OPERATION_NAME, getter.getDbOperationName(request)); } if (SemconvStability.emitOldDatabaseSemconv()) { + internalSet(attributes, DB_USER, getter.getUser(request)); + internalSet(attributes, DB_NAME, getter.getDbNamespace(request)); + internalSet(attributes, DB_CONNECTION_STRING, getter.getConnectionString(request)); internalSet(attributes, DB_STATEMENT, getter.getDbQueryText(request)); internalSet(attributes, DB_OPERATION, getter.getDbOperationName(request)); } } + + @Override + public void onEnd( + AttributesBuilder attributes, + Context context, + REQUEST request, + @Nullable RESPONSE response, + @Nullable Throwable error) {} + + /** + * This method is internal and is hence not for public use. Its API is unstable and can change at + * any time. + */ + @Override + public SpanKey internalGetSpanKey() { + return SpanKey.DB_CLIENT; + } } diff --git a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientAttributesGetter.java b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientAttributesGetter.java index ddf9f8f07a1a..b691bb35ba71 100644 --- a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientAttributesGetter.java +++ b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientAttributesGetter.java @@ -18,7 +18,42 @@ * from the attribute methods, but implement as many as possible for best compliance with the * OpenTelemetry specification. */ -public interface DbClientAttributesGetter extends DbClientCommonAttributesGetter { +public interface DbClientAttributesGetter { + + @Deprecated + @Nullable + default String getSystem(REQUEST request) { + return null; + } + + // TODO: make this required to implement + @Nullable + default String getDbSystem(REQUEST request) { + return getSystem(request); + } + + @Deprecated + @Nullable + String getUser(REQUEST request); + + /** + * @deprecated Use {@link #getDbNamespace(Object)} instead. + */ + @Deprecated + @Nullable + default String getName(REQUEST request) { + return null; + } + + // TODO: make this required to implement + @Nullable + default String getDbNamespace(REQUEST request) { + return getName(request); + } + + @Deprecated + @Nullable + String getConnectionString(REQUEST request); /** * @deprecated Use {@link #getDbQueryText(REQUEST)} instead. diff --git a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientCommonAttributesExtractor.java b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientCommonAttributesExtractor.java deleted file mode 100644 index 9cbf4fb97895..000000000000 --- a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientCommonAttributesExtractor.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.api.incubator.semconv.db; - -import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet; - -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.common.AttributesBuilder; -import io.opentelemetry.context.Context; -import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; -import io.opentelemetry.instrumentation.api.internal.SemconvStability; -import io.opentelemetry.instrumentation.api.internal.SpanKey; -import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider; -import javax.annotation.Nullable; - -abstract class DbClientCommonAttributesExtractor< - REQUEST, RESPONSE, GETTER extends DbClientCommonAttributesGetter> - implements AttributesExtractor, SpanKeyProvider { - - // copied from DbIncubatingAttributes - private static final AttributeKey DB_NAME = AttributeKey.stringKey("db.name"); - private static final AttributeKey DB_NAMESPACE = AttributeKey.stringKey("db.namespace"); - private static final AttributeKey DB_SYSTEM = AttributeKey.stringKey("db.system"); - private static final AttributeKey DB_USER = AttributeKey.stringKey("db.user"); - private static final AttributeKey DB_CONNECTION_STRING = - AttributeKey.stringKey("db.connection_string"); - - final GETTER getter; - - DbClientCommonAttributesExtractor(GETTER getter) { - this.getter = getter; - } - - @SuppressWarnings("deprecation") // until old db semconv are dropped - @Override - public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) { - internalSet(attributes, DB_SYSTEM, getter.getDbSystem(request)); - if (SemconvStability.emitStableDatabaseSemconv()) { - internalSet(attributes, DB_NAMESPACE, getter.getDbNamespace(request)); - } - if (SemconvStability.emitOldDatabaseSemconv()) { - internalSet(attributes, DB_USER, getter.getUser(request)); - internalSet(attributes, DB_NAME, getter.getDbNamespace(request)); - internalSet(attributes, DB_CONNECTION_STRING, getter.getConnectionString(request)); - } - } - - @Override - public final void onEnd( - AttributesBuilder attributes, - Context context, - REQUEST request, - @Nullable RESPONSE response, - @Nullable Throwable error) {} - - /** - * This method is internal and is hence not for public use. Its API is unstable and can change at - * any time. - */ - @Override - public SpanKey internalGetSpanKey() { - return SpanKey.DB_CLIENT; - } -} diff --git a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientCommonAttributesGetter.java b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientCommonAttributesGetter.java deleted file mode 100644 index bc9a335f4e76..000000000000 --- a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/DbClientCommonAttributesGetter.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.api.incubator.semconv.db; - -import javax.annotation.Nullable; - -/** An interface for getting attributes common to database clients. */ -public interface DbClientCommonAttributesGetter { - - @Deprecated - @Nullable - default String getSystem(REQUEST request) { - return null; - } - - // TODO: make this required to implement - @Nullable - default String getDbSystem(REQUEST request) { - return getSystem(request); - } - - @Deprecated - @Nullable - String getUser(REQUEST request); - - /** - * @deprecated Use {@link #getDbNamespace(Object)} instead. - */ - @Deprecated - @Nullable - default String getName(REQUEST request) { - return null; - } - - // TODO: make this required to implement - @Nullable - default String getDbNamespace(REQUEST request) { - return getName(request); - } - - @Deprecated - @Nullable - String getConnectionString(REQUEST request); -} diff --git a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlClientAttributesExtractor.java b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlClientAttributesExtractor.java index fc5f29efe81b..ca3158b4a6de 100644 --- a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlClientAttributesExtractor.java +++ b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlClientAttributesExtractor.java @@ -12,6 +12,9 @@ import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.internal.SemconvStability; +import io.opentelemetry.instrumentation.api.internal.SpanKey; +import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider; +import javax.annotation.Nullable; /** * Extractor of - extends DbClientCommonAttributesExtractor< - REQUEST, RESPONSE, SqlClientAttributesGetter> { + implements AttributesExtractor, SpanKeyProvider { // copied from DbIncubatingAttributes private static final AttributeKey DB_OPERATION = AttributeKey.stringKey("db.operation"); @@ -58,18 +60,22 @@ public static SqlClientAttributesExtractorBuilder oldSemconvTableAttribute; private final boolean statementSanitizationEnabled; + private final AttributesExtractor delegate; + private final SqlClientAttributesGetter getter; + SqlClientAttributesExtractor( SqlClientAttributesGetter getter, AttributeKey oldSemconvTableAttribute, boolean statementSanitizationEnabled) { - super(getter); + this.delegate = DbClientAttributesExtractor.create(getter); this.oldSemconvTableAttribute = oldSemconvTableAttribute; this.statementSanitizationEnabled = statementSanitizationEnabled; + this.getter = getter; } @Override public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) { - super.onStart(attributes, parentContext, request); + delegate.onStart(attributes, parentContext, request); String rawQueryText = getter.getRawQueryText(request); SqlStatementInfo sanitizedStatement = sanitizer.sanitize(rawQueryText); @@ -97,4 +103,23 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST } } } + + @Override + public void onEnd( + AttributesBuilder attributes, + Context context, + REQUEST request, + @Nullable RESPONSE response, + @Nullable Throwable error) { + delegate.onEnd(attributes, context, request, response, error); + } + + /** + * This method is internal and is hence not for public use. Its API is unstable and can change at + * any time. + */ + @Override + public SpanKey internalGetSpanKey() { + return SpanKey.DB_CLIENT; + } } diff --git a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlClientAttributesGetter.java b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlClientAttributesGetter.java index d0817d63e460..fdf42b6da502 100644 --- a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlClientAttributesGetter.java +++ b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlClientAttributesGetter.java @@ -18,8 +18,7 @@ * from the attribute methods, but implement as many as possible for best compliance with the * OpenTelemetry specification. */ -public interface SqlClientAttributesGetter - extends DbClientCommonAttributesGetter { +public interface SqlClientAttributesGetter extends DbClientAttributesGetter { /** * Get the raw SQL statement. The value returned by this method is later sanitized by the {@link