Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Db network attributes #12605

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
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 io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.internal.InternalNetworkAttributesExtractor;
import javax.annotation.Nullable;

/**
* Extractor of <a
Expand All @@ -22,38 +27,75 @@
* 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, RESPONSE> getter;
private final InternalNetworkAttributesExtractor<REQUEST, RESPONSE> internalNetworkExtractor;
private final ServerAttributesExtractor<REQUEST, RESPONSE> serverAttributesExtractor;

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

DbClientAttributesExtractor(DbClientAttributesGetter<REQUEST> getter) {
super(getter);
DbClientAttributesExtractor(DbClientAttributesGetter<REQUEST, RESPONSE> getter) {
this.getter = getter;
internalNetworkExtractor =
new InternalNetworkAttributesExtractor<>(
getter, SemconvStability.emitOldDatabaseSemconv(), false);
serverAttributesExtractor = ServerAttributesExtractor.create(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));
}
serverAttributesExtractor.onStart(attributes, parentContext, request);
}

@Override
public void onEnd(
AttributesBuilder attributes,
Context context,
REQUEST request,
@Nullable RESPONSE response,
@Nullable Throwable error) {
internalNetworkExtractor.onEnd(attributes, request, response);
}

/**
* 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 @@ -5,6 +5,8 @@

package io.opentelemetry.instrumentation.api.incubator.semconv.db;

import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesGetter;
import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesGetter;
import javax.annotation.Nullable;

/**
Expand All @@ -18,7 +20,43 @@
* 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, RESPONSE>
extends NetworkAttributesGetter<REQUEST, RESPONSE>, ServerAttributesGetter<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 @@ -18,7 +18,7 @@ public abstract class DbClientSpanNameExtractor<REQUEST> implements SpanNameExtr
* @see DbClientAttributesGetter#getDbNamespace(Object) used to extract {@code <db.namespace>}.
*/
public static <REQUEST> SpanNameExtractor<REQUEST> create(
DbClientAttributesGetter<REQUEST> getter) {
DbClientAttributesGetter<REQUEST, ?> getter) {
return new GenericDbClientSpanNameExtractor<>(getter);
}

Expand All @@ -32,7 +32,7 @@ public static <REQUEST> SpanNameExtractor<REQUEST> create(
* procedure name.
*/
public static <REQUEST> SpanNameExtractor<REQUEST> create(
SqlClientAttributesGetter<REQUEST> getter) {
SqlClientAttributesGetter<REQUEST, ?> getter) {
return new SqlClientSpanNameExtractor<>(getter);
}

Expand Down Expand Up @@ -65,9 +65,9 @@ protected String computeSpanName(String dbName, String operation, String mainIde
private static final class GenericDbClientSpanNameExtractor<REQUEST>
extends DbClientSpanNameExtractor<REQUEST> {

private final DbClientAttributesGetter<REQUEST> getter;
private final DbClientAttributesGetter<REQUEST, ?> getter;

private GenericDbClientSpanNameExtractor(DbClientAttributesGetter<REQUEST> getter) {
private GenericDbClientSpanNameExtractor(DbClientAttributesGetter<REQUEST, ?> getter) {
this.getter = getter;
}

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

private final SqlClientAttributesGetter<REQUEST> getter;
private final SqlClientAttributesGetter<REQUEST, ?> getter;

private SqlClientSpanNameExtractor(SqlClientAttributesGetter<REQUEST> getter) {
private SqlClientSpanNameExtractor(SqlClientAttributesGetter<REQUEST, ?> getter) {
this.getter = getter;
}

Expand Down
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 All @@ -38,7 +40,7 @@ public final class SqlClientAttributesExtractor<REQUEST, RESPONSE>

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

Expand All @@ -47,7 +49,7 @@ public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
* client attributes extractor.
*/
public static <REQUEST, RESPONSE> SqlClientAttributesExtractorBuilder<REQUEST, RESPONSE> builder(
SqlClientAttributesGetter<REQUEST> getter) {
SqlClientAttributesGetter<REQUEST, RESPONSE> getter) {
return new SqlClientAttributesExtractorBuilder<>(getter);
}

Expand All @@ -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, RESPONSE> getter;

SqlClientAttributesExtractor(
SqlClientAttributesGetter<REQUEST> getter,
SqlClientAttributesGetter<REQUEST, RESPONSE> 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;
}
}
Loading
Loading