Skip to content
Merged
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 @@ -108,7 +108,7 @@ static <REQUEST, RESPONSE> void onStartCommon(
if (emitOldDatabaseSemconv()) {
attributes.put(DB_SYSTEM, getter.getDbSystemName(request));
attributes.put(DB_USER, getter.getUser(request));
attributes.put(DB_NAME, getter.getDbNamespace(request));
attributes.put(DB_NAME, getter.getDbName(request));
attributes.put(DB_CONNECTION_STRING, getter.getConnectionString(request));
attributes.put(DB_STATEMENT, getter.getDbQueryText(request));
attributes.put(DB_OPERATION, getter.getDbOperationName(request));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ default String getDbQuerySummary(REQUEST request) {
@Nullable
String getDbNamespace(REQUEST request);

/**
* Returns the old db.name value. This is only used for old semantic conventions.
*
* @deprecated Use {@link #getDbNamespace(Object)} instead.
*/
@Deprecated // to be removed in 3.0
@Nullable
default String getDbName(REQUEST request) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess an alternative would be to se an internal interface

Copy link
Member Author

Choose a reason for hiding this comment

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

Similar to #16304 (comment), I kind of like making it public to support anyone outside of this repo going through the transition, although maybe that's not too realistic since they'd have to stick to our timeline as we'll be removing them in 3.0

return getDbNamespace(request);
}

/**
* Returns the database user name. This is only used for old semantic conventions.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ private GenericDbClientSpanNameExtractor(DbClientAttributesGetter<REQUEST, ?> ge
this.getter = getter;
}

@SuppressWarnings("deprecation") // getDbName is used for old semconv span names
@Override
public String extract(REQUEST request) {
if (emitStableDatabaseSemconv()) {
Expand All @@ -165,9 +166,9 @@ public String extract(REQUEST request) {
String operationName = getter.getDbOperationName(request);
return computeSpanNameStable(getter, request, operationName, null, null);
}
String namespace = getter.getDbNamespace(request);
String dbName = getter.getDbName(request);
String operationName = getter.getDbOperationName(request);
return computeSpanName(namespace, operationName, null, null);
return computeSpanName(dbName, operationName, null, null);
}
}

Expand All @@ -180,9 +181,9 @@ private SqlClientSpanNameExtractor(SqlClientAttributesGetter<REQUEST, ?> getter)
this.getter = getter;
}

@SuppressWarnings("deprecation") // getDbName is used for old semconv span names
@Override
public String extract(REQUEST request) {
String namespace = getter.getDbNamespace(request);
SqlDialect dialect = getter.getSqlDialect(request);
Collection<String> rawQueryTexts = getter.getRawQueryTexts(request);

Expand All @@ -195,18 +196,20 @@ public String extract(REQUEST request) {
String operationName = getter.getDbOperationName(request);
return computeSpanNameStable(getter, request, operationName, null, null);
}
String dbName = getter.getDbName(request);
String operationName = getter.getDbOperationName(request);
return computeSpanName(namespace, operationName, null, null);
return computeSpanName(dbName, operationName, null, null);
}

if (!emitStableDatabaseSemconv()) {
String dbName = getter.getDbName(request);
if (rawQueryTexts.size() > 1) { // for backcompat(?)
return computeSpanName(namespace, null, null, null);
return computeSpanName(dbName, null, null, null);
}
SqlQuery analyzedQuery =
SqlQueryAnalyzerUtil.analyze(rawQueryTexts.iterator().next(), dialect);
return computeSpanName(
namespace,
dbName,
analyzedQuery.getOperationName(),
analyzedQuery.getCollectionName(),
analyzedQuery.getStoredProcedureName());
Expand Down Expand Up @@ -256,14 +259,15 @@ private GenericOldSemconvSqlClientSpanNameExtractor(
this.sqlDelegate = new SqlClientSpanNameExtractor<>(getter);
}

@SuppressWarnings("deprecation") // getDbName is used for old semconv span names
@Override
public String extract(REQUEST request) {
if (emitStableDatabaseSemconv()) {
return sqlDelegate.extract(request);
}
// For old semconv, use the generic span name format (operation + namespace)
// For old semconv, use the generic span name format (operation + db.name)
// without collection name to preserve backward compatibility
String namespace = getter.getDbNamespace(request);
String dbName = getter.getDbName(request);
Collection<String> rawQueryTexts = getter.getRawQueryTexts(request);
String operationName = null;
if (rawQueryTexts.size() == 1) {
Expand All @@ -275,7 +279,7 @@ public String extract(REQUEST request) {
if (operationName == null) {
operationName = getter.getDbOperationName(request);
}
return computeSpanName(namespace, operationName, null, null);
return computeSpanName(dbName, operationName, null, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.api.incubator.semconv.db;

import static io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlDialect.DOUBLE_QUOTES_ARE_STRING_LITERALS;
import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitOldDatabaseSemconv;
import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableDatabaseSemconv;
import static java.util.Collections.emptyList;
import static java.util.Collections.singleton;
Expand All @@ -22,6 +23,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@SuppressWarnings("deprecation") // getDbName is used for old semconv span names
@ExtendWith(MockitoExtension.class)
class DbClientSpanNameExtractorTest {
@Mock DbClientAttributesGetter<DbRequest, Void> dbAttributesGetter;
Expand All @@ -42,7 +44,9 @@ void shouldExtractFullSpanName() {

when(sqlAttributesGetter.getRawQueryTexts(dbRequest))
.thenReturn(singleton("SELECT * from table"));
when(sqlAttributesGetter.getDbNamespace(dbRequest)).thenReturn("database");
if (emitOldDatabaseSemconv() && !emitStableDatabaseSemconv()) {
when(sqlAttributesGetter.getDbName(dbRequest)).thenReturn("database");
}

SpanNameExtractor<DbRequest> underTest = DbClientSpanNameExtractor.create(sqlAttributesGetter);

Expand All @@ -61,7 +65,9 @@ void shouldSkipNamespaceIfTableAlreadyHasNamespacePrefix() {

when(sqlAttributesGetter.getRawQueryTexts(dbRequest))
.thenReturn(singleton("SELECT * from another.table"));
when(sqlAttributesGetter.getDbNamespace(dbRequest)).thenReturn("database");
if (emitOldDatabaseSemconv() && !emitStableDatabaseSemconv()) {
when(sqlAttributesGetter.getDbName(dbRequest)).thenReturn("database");
}

SpanNameExtractor<DbRequest> underTest = DbClientSpanNameExtractor.create(sqlAttributesGetter);

Expand Down Expand Up @@ -95,7 +101,12 @@ void shouldExtractOperationAndName() {
DbRequest dbRequest = new DbRequest();

when(dbAttributesGetter.getDbOperationName(dbRequest)).thenReturn("SELECT");
when(dbAttributesGetter.getDbNamespace(dbRequest)).thenReturn("database");
if (emitStableDatabaseSemconv()) {
when(dbAttributesGetter.getDbNamespace(dbRequest)).thenReturn("database");
}
if (emitOldDatabaseSemconv() && !emitStableDatabaseSemconv()) {
when(dbAttributesGetter.getDbName(dbRequest)).thenReturn("database");
}

SpanNameExtractor<DbRequest> underTest = DbClientSpanNameExtractor.create(dbAttributesGetter);

Expand Down Expand Up @@ -127,7 +138,12 @@ void shouldExtractNamespace() {
// given
DbRequest dbRequest = new DbRequest();

when(dbAttributesGetter.getDbNamespace(dbRequest)).thenReturn("database");
if (emitStableDatabaseSemconv()) {
when(dbAttributesGetter.getDbNamespace(dbRequest)).thenReturn("database");
}
if (emitOldDatabaseSemconv() && !emitStableDatabaseSemconv()) {
when(dbAttributesGetter.getDbName(dbRequest)).thenReturn("database");
}

SpanNameExtractor<DbRequest> underTest = DbClientSpanNameExtractor.create(dbAttributesGetter);

Expand Down Expand Up @@ -158,10 +174,13 @@ void shouldUseQuerySummaryWhenAvailable() {
DbRequest dbRequest = new DbRequest();

// Needs to be lenient because not called during this test under old semconv mode
lenient().when(dbAttributesGetter.getDbQuerySummary(dbRequest)).thenReturn("SELECT users");
// Needs to be lenient because not called during this test under new semconv mode
lenient().when(dbAttributesGetter.getDbOperationName(dbRequest)).thenReturn("SELECT");
lenient().when(dbAttributesGetter.getDbNamespace(dbRequest)).thenReturn("database");
if (emitStableDatabaseSemconv()) {
when(dbAttributesGetter.getDbQuerySummary(dbRequest)).thenReturn("SELECT users");
}
if (emitOldDatabaseSemconv() && !emitStableDatabaseSemconv()) {
when(dbAttributesGetter.getDbOperationName(dbRequest)).thenReturn("SELECT");
when(dbAttributesGetter.getDbName(dbRequest)).thenReturn("database");
}

SpanNameExtractor<DbRequest> underTest = DbClientSpanNameExtractor.create(dbAttributesGetter);

Expand All @@ -180,7 +199,9 @@ void shouldExtractFullSpanNameForBatch() {

when(sqlAttributesGetter.getRawQueryTexts(dbRequest))
.thenReturn(Arrays.asList("INSERT INTO table VALUES(1)", "INSERT INTO table VALUES(2)"));
when(sqlAttributesGetter.getDbNamespace(dbRequest)).thenReturn("database");
if (emitOldDatabaseSemconv() && !emitStableDatabaseSemconv()) {
when(sqlAttributesGetter.getDbName(dbRequest)).thenReturn("database");
}

SpanNameExtractor<DbRequest> underTest = DbClientSpanNameExtractor.create(sqlAttributesGetter);

Expand All @@ -198,7 +219,9 @@ void shouldExtractFullSpanNameForSingleQueryBatch() {

when(sqlAttributesGetter.getRawQueryTexts(dbRequest))
.thenReturn(singleton("INSERT INTO table VALUES(?)"));
when(sqlAttributesGetter.getDbNamespace(dbRequest)).thenReturn("database");
if (emitOldDatabaseSemconv() && !emitStableDatabaseSemconv()) {
when(sqlAttributesGetter.getDbName(dbRequest)).thenReturn("database");
}
if (emitStableDatabaseSemconv()) {
when(sqlAttributesGetter.getDbOperationBatchSize(dbRequest)).thenReturn(2L);
}
Expand All @@ -220,7 +243,12 @@ void shouldFallBackToExplicitOperationNameForEmptySqlQuery() {

when(sqlAttributesGetter.getRawQueryTexts(dbRequest)).thenReturn(emptyList());
when(sqlAttributesGetter.getDbOperationName(dbRequest)).thenReturn("WRITE");
when(sqlAttributesGetter.getDbNamespace(dbRequest)).thenReturn("mydb");
if (emitStableDatabaseSemconv()) {
when(sqlAttributesGetter.getDbNamespace(dbRequest)).thenReturn("mydb");
}
if (emitOldDatabaseSemconv() && !emitStableDatabaseSemconv()) {
when(sqlAttributesGetter.getDbName(dbRequest)).thenReturn("mydb");
}

SpanNameExtractor<DbRequest> underTest = DbClientSpanNameExtractor.create(sqlAttributesGetter);

Expand All @@ -239,7 +267,9 @@ void shouldPreserveOldSemconvSpanNameForMigration() {

when(sqlAttributesGetter.getRawQueryTexts(dbRequest))
.thenReturn(singleton("SELECT * from table"));
lenient().when(sqlAttributesGetter.getDbNamespace(dbRequest)).thenReturn("database");
if (emitOldDatabaseSemconv() && !emitStableDatabaseSemconv()) {
when(sqlAttributesGetter.getDbName(dbRequest)).thenReturn("database");
}

SpanNameExtractor<DbRequest> underTest =
DbClientSpanNameExtractor.createWithGenericOldSpanName(sqlAttributesGetter);
Expand All @@ -260,7 +290,12 @@ void shouldFallBackToExplicitOperationForEmptySqlQueryInMigration() {

when(sqlAttributesGetter.getRawQueryTexts(dbRequest)).thenReturn(emptyList());
when(sqlAttributesGetter.getDbOperationName(dbRequest)).thenReturn("WRITE");
when(sqlAttributesGetter.getDbNamespace(dbRequest)).thenReturn("mydb");
if (emitStableDatabaseSemconv()) {
when(sqlAttributesGetter.getDbNamespace(dbRequest)).thenReturn("mydb");
}
if (emitOldDatabaseSemconv() && !emitStableDatabaseSemconv()) {
when(sqlAttributesGetter.getDbName(dbRequest)).thenReturn("mydb");
}

SpanNameExtractor<DbRequest> underTest =
DbClientSpanNameExtractor.createWithGenericOldSpanName(sqlAttributesGetter);
Expand Down
Loading