Skip to content

Commit

Permalink
Map lettuce 5.1 db.namespace to db.name (unless using experimenta…
Browse files Browse the repository at this point in the history
…l database semconv stability opt-in) (#12609)
  • Loading branch information
trask authored Nov 13, 2024
1 parent cd65f9a commit 5059f9a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ final class OpenTelemetryTracing implements Tracing {
private static final AttributeKey<String> DB_SYSTEM = AttributeKey.stringKey("db.system");
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<Long> DB_REDIS_DATABASE_INDEX =
AttributeKey.longKey("db.redis.database_index");
// copied from DbIncubatingAttributes.DbSystemIncubatingValues
private static final String REDIS = "redis";

Expand Down Expand Up @@ -313,6 +315,16 @@ public synchronized Tracer.Span tag(String key, String value) {
argsString = value;
return this;
}
if (key.equals("db.namespace") && SemconvStability.emitOldDatabaseSemconv()) {
// map backwards into db.redis.database.index
long val = Long.parseLong(value);
if (span != null) {
span.setAttribute(DB_REDIS_DATABASE_INDEX, val);
} else {
spanBuilder.setAttribute(DB_REDIS_DATABASE_INDEX, val);
}
return this;
}
if (span != null) {
span.setAttribute(key, value);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAMESPACE;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_REDIS_DATABASE_INDEX;

import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
Expand Down Expand Up @@ -80,10 +82,15 @@ private ContainerConnection(StatefulRedisConnection<String, String> connection,
}
}

@SuppressWarnings("deprecation") // using deprecated semconv
protected static List<AttributeAssertion> addExtraAttributes(AttributeAssertion... assertions) {
List<AttributeAssertion> result = new ArrayList<>(Arrays.asList(assertions));
if (Boolean.getBoolean("testLatestDeps")) {
result.add(equalTo(DB_NAMESPACE, "0"));
if (SemconvStability.emitStableDatabaseSemconv()) {
result.add(equalTo(DB_NAMESPACE, "0"));
} else {
result.add(equalTo(DB_REDIS_DATABASE_INDEX, 0));
}
}
return result;
}
Expand Down

0 comments on commit 5059f9a

Please sign in to comment.