Skip to content

Commit

Permalink
Simplify db getter and extractor hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Nov 10, 2024
1 parent 74d07f1 commit 62e11c9
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a
Expand All @@ -22,38 +25,66 @@
* attribute extraction from request/response objects.
*/
public final class DbClientAttributesExtractor<REQUEST, RESPONSE>
extends DbClientCommonAttributesExtractor<
REQUEST, RESPONSE, DbClientAttributesGetter<REQUEST>> {
implements AttributesExtractor<REQUEST, RESPONSE>, SpanKeyProvider {

// copied from DbIncubatingAttributes
private static final AttributeKey<String> DB_NAME = AttributeKey.stringKey("db.name");
private static final AttributeKey<String> DB_NAMESPACE = AttributeKey.stringKey("db.namespace");
private static final AttributeKey<String> DB_SYSTEM = AttributeKey.stringKey("db.system");
private static final AttributeKey<String> DB_USER = AttributeKey.stringKey("db.user");
private static final AttributeKey<String> DB_CONNECTION_STRING =
AttributeKey.stringKey("db.connection_string");
private static final AttributeKey<String> DB_STATEMENT = AttributeKey.stringKey("db.statement");
private static final AttributeKey<String> DB_QUERY_TEXT = AttributeKey.stringKey("db.query.text");

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

private final DbClientAttributesGetter<REQUEST> getter;

/** Creates the database client attributes extractor with default configuration. */
public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
DbClientAttributesGetter<REQUEST> getter) {
return new DbClientAttributesExtractor<>(getter);
}

DbClientAttributesExtractor(DbClientAttributesGetter<REQUEST> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,42 @@
* from the attribute methods, but implement as many as possible for best compliance with the
* OpenTelemetry specification.
*/
public interface DbClientAttributesGetter<REQUEST> extends DbClientCommonAttributesGetter<REQUEST> {
public interface DbClientAttributesGetter<REQUEST> {

@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.
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 <a
Expand All @@ -24,8 +27,7 @@
* parameters are removed.
*/
public final class SqlClientAttributesExtractor<REQUEST, RESPONSE>
extends DbClientCommonAttributesExtractor<
REQUEST, RESPONSE, SqlClientAttributesGetter<REQUEST>> {
implements AttributesExtractor<REQUEST, RESPONSE>, SpanKeyProvider {

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

private final AttributesExtractor<REQUEST, RESPONSE> delegate;
private final SqlClientAttributesGetter<REQUEST> getter;

SqlClientAttributesExtractor(
SqlClientAttributesGetter<REQUEST> getter,
AttributeKey<String> 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);
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
* from the attribute methods, but implement as many as possible for best compliance with the
* OpenTelemetry specification.
*/
public interface SqlClientAttributesGetter<REQUEST>
extends DbClientCommonAttributesGetter<REQUEST> {
public interface SqlClientAttributesGetter<REQUEST> extends DbClientAttributesGetter<REQUEST> {

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

0 comments on commit 62e11c9

Please sign in to comment.