Skip to content
Open
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 @@ -6,15 +6,22 @@
package io.opentelemetry.instrumentation.api.incubator.semconv.db;

import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
import static io.opentelemetry.semconv.DbAttributes.DB_NAMESPACE;
import static io.opentelemetry.semconv.DbAttributes.DB_OPERATION_NAME;
import static io.opentelemetry.semconv.DbAttributes.DB_QUERY_SUMMARY;
import static io.opentelemetry.semconv.DbAttributes.DB_QUERY_TEXT;
import static io.opentelemetry.semconv.DbAttributes.DB_RESPONSE_STATUS_CODE;
import static io.opentelemetry.semconv.DbAttributes.DB_SYSTEM_NAME;
import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE;

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;

/**
* Extractor of <a
Expand All @@ -25,12 +32,18 @@
* attribute extraction from request/response objects.
*/
public final class DbClientAttributesExtractor<REQUEST, RESPONSE>
extends DbClientCommonAttributesExtractor<
REQUEST, RESPONSE, DbClientAttributesGetter<REQUEST, RESPONSE>> {
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_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");
static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");
private static final AttributeKey<String> DB_OPERATION = AttributeKey.stringKey("db.operation");

private final DbClientAttributesGetter<REQUEST, RESPONSE> getter;

/** Creates the database client attributes extractor with default configuration. */
public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
Expand All @@ -39,21 +52,55 @@ public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
}

DbClientAttributesExtractor(DbClientAttributesGetter<REQUEST, RESPONSE> getter) {
super(getter);
this.getter = getter;
}

@SuppressWarnings("deprecation") // until old db semconv are dropped
@Override
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
super.onStart(attributes, parentContext, request);

if (SemconvStability.emitStableDatabaseSemconv()) {
internalSet(
attributes,
DB_SYSTEM_NAME,
SemconvStability.stableDbSystemName(getter.getDbSystem(request)));
internalSet(attributes, DB_NAMESPACE, getter.getDbNamespace(request));
internalSet(attributes, DB_QUERY_TEXT, getter.getDbQueryText(request));
internalSet(attributes, DB_OPERATION_NAME, getter.getDbOperationName(request));
internalSet(attributes, DB_QUERY_SUMMARY, getter.getDbQuerySummary(request));
}
if (SemconvStability.emitOldDatabaseSemconv()) {
internalSet(attributes, DB_SYSTEM, getter.getDbSystem(request));
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) {
if (SemconvStability.emitStableDatabaseSemconv()) {
if (error != null) {
internalSet(attributes, ERROR_TYPE, error.getClass().getName());
}
if (error != null || response != null) {
internalSet(attributes, DB_RESPONSE_STATUS_CODE, getter.getResponseStatus(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,6 +18,7 @@
* from the attribute methods, but implement as many as possible for best compliance with the
* OpenTelemetry specification.
*/
@SuppressWarnings("deprecation") // extending deprecated interface for backward compatibility
public interface DbClientAttributesGetter<REQUEST, RESPONSE>
extends DbClientCommonAttributesGetter<REQUEST, RESPONSE> {

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

import javax.annotation.Nullable;

/** An interface for getting attributes common to database clients. */
/**
* An interface for getting attributes common to database clients.
*
* @deprecated Use {@link DbClientAttributesGetter} instead.
*/
@Deprecated
public interface DbClientCommonAttributesGetter<REQUEST, RESPONSE> {

String getDbSystem(REQUEST request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@

import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
import static io.opentelemetry.semconv.DbAttributes.DB_COLLECTION_NAME;
import static io.opentelemetry.semconv.DbAttributes.DB_NAMESPACE;
import static io.opentelemetry.semconv.DbAttributes.DB_OPERATION_BATCH_SIZE;
import static io.opentelemetry.semconv.DbAttributes.DB_OPERATION_NAME;
import static io.opentelemetry.semconv.DbAttributes.DB_QUERY_TEXT;
import static io.opentelemetry.semconv.DbAttributes.DB_RESPONSE_STATUS_CODE;
import static io.opentelemetry.semconv.DbAttributes.DB_SYSTEM_NAME;
import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE;

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 io.opentelemetry.semconv.AttributeKeyTemplate;
import java.util.Collection;
import java.util.Map;
import javax.annotation.Nullable;

/**
* Extractor of <a
Expand All @@ -31,10 +38,14 @@
* statement parameters are removed.
*/
public final class SqlClientAttributesExtractor<REQUEST, RESPONSE>
extends DbClientCommonAttributesExtractor<
REQUEST, RESPONSE, SqlClientAttributesGetter<REQUEST, RESPONSE>> {
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_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_OPERATION = AttributeKey.stringKey("db.operation");
private static final AttributeKey<String> DB_STATEMENT = AttributeKey.stringKey("db.statement");
private static final AttributeKeyTemplate<String> DB_QUERY_PARAMETER =
Expand All @@ -57,6 +68,7 @@ public static <REQUEST, RESPONSE> SqlClientAttributesExtractorBuilder<REQUEST, R

private static final String SQL_CALL = "CALL";

private final SqlClientAttributesGetter<REQUEST, RESPONSE> getter;
private final AttributeKey<String> oldSemconvTableAttribute;
private final boolean statementSanitizationEnabled;
private final boolean captureQueryParameters;
Expand All @@ -66,17 +78,32 @@ public static <REQUEST, RESPONSE> SqlClientAttributesExtractorBuilder<REQUEST, R
AttributeKey<String> oldSemconvTableAttribute,
boolean statementSanitizationEnabled,
boolean captureQueryParameters) {
super(getter);
this.getter = getter;
this.oldSemconvTableAttribute = oldSemconvTableAttribute;
// capturing query parameters disables statement sanitization
this.statementSanitizationEnabled = !captureQueryParameters && statementSanitizationEnabled;
this.captureQueryParameters = captureQueryParameters;
}

@SuppressWarnings("deprecation") // until old db semconv are dropped
@Override
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
super.onStart(attributes, parentContext, request);
// Common attributes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could consider placing the common part to util class or perhaps even a static package private method in DbClientAttributesExtractor that could also be called from here.

if (SemconvStability.emitStableDatabaseSemconv()) {
internalSet(
attributes,
DB_SYSTEM_NAME,
SemconvStability.stableDbSystemName(getter.getDbSystem(request)));
internalSet(attributes, DB_NAMESPACE, getter.getDbNamespace(request));
}
if (SemconvStability.emitOldDatabaseSemconv()) {
internalSet(attributes, DB_SYSTEM, getter.getDbSystem(request));
internalSet(attributes, DB_USER, getter.getUser(request));
internalSet(attributes, DB_NAME, getter.getDbNamespace(request));
internalSet(attributes, DB_CONNECTION_STRING, getter.getConnectionString(request));
}

// SQL-specific attributes
Collection<String> rawQueryTexts = getter.getRawQueryTexts(request);

if (rawQueryTexts.isEmpty()) {
Expand Down Expand Up @@ -160,4 +187,30 @@ private static String join(String delimiter, Collection<String> collection) {
}
return builder.toString();
}

@Override
public void onEnd(
AttributesBuilder attributes,
Context context,
REQUEST request,
@Nullable RESPONSE response,
@Nullable Throwable error) {
if (SemconvStability.emitStableDatabaseSemconv()) {
if (error != null) {
internalSet(attributes, ERROR_TYPE, error.getClass().getName());
}
if (error != null || response != null) {
internalSet(attributes, DB_RESPONSE_STATUS_CODE, getter.getResponseStatus(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 @@ -22,7 +22,37 @@
* OpenTelemetry specification.
*/
public interface SqlClientAttributesGetter<REQUEST, RESPONSE>
extends DbClientCommonAttributesGetter<REQUEST, RESPONSE> {
extends DbClientAttributesGetter<REQUEST, RESPONSE> {

/**
* SqlClientAttributesExtractor will try to populate db.operation.name based on {@link
* #getRawQueryTexts(REQUEST)}, but this can be used to explicitly provide the operation name.
*/
@Override
@Nullable
default String getDbOperationName(REQUEST request) {
return null;
}

/**
* SqlClientAttributesExtractor will try to populate db.operation.name based on {@link
* #getRawQueryTexts(REQUEST)}, but this can be used to explicitly provide the operation name.
*/
Comment on lines +37 to +40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a copy-paste mistake

@Override
@Nullable
default String getDbQueryText(REQUEST request) {
return null;
}

/**
* SqlClientAttributesExtractor will try to populate db.operation.name based on {@link
* #getRawQueryTexts(REQUEST)}, but this can be used to explicitly provide the operation name.
*/
Comment on lines +47 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a copy-paste mistake

@Override
@Nullable
default String getDbQuerySummary(REQUEST request) {
return null;
}

/**
* Get the raw SQL query texts. The values returned by this method is later sanitized by the
Expand Down
Loading