Skip to content

Commit

Permalink
fix stuff tht broke after rebase, address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit committed Dec 6, 2023
1 parent 9daa008 commit 8b38aea
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.vertx.core.Future;
Expand Down Expand Up @@ -54,8 +53,7 @@ public static void onEnter(
}

String commandName = VertxRedisClientSingletons.getCommandName(request.command());
RedisURI redisUri =
VirtualField.find(RedisStandaloneConnection.class, RedisURI.class).get(connection);
RedisURI redisUri = VertxRedisClientSingletons.getRedisUri(connection);
if (commandName == null || redisUri == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

package io.opentelemetry.javaagent.instrumentation.vertx.v4_0.redis;

import io.opentelemetry.instrumentation.api.db.RedisCommandSanitizer;
import io.opentelemetry.instrumentation.api.instrumenter.db.DbClientAttributesGetter;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesGetter;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.RedisCommandSanitizer;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.opentelemetry.semconv.SemanticAttributes;
import javax.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

package io.opentelemetry.javaagent.instrumentation.vertx.v4_0.redis;

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

public enum VertxRedisClientNetAttributesGetter
implements
ServerAttributesGetter<VertxRedisClientRequest, Void>,
ServerAttributesGetter<VertxRedisClientRequest>,
NetworkAttributesGetter<VertxRedisClientRequest, Void> {
INSTANCE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.net.PeerServiceAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.db.DbClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.net.PeerServiceAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.NetworkAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.ServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.vertx.core.Future;
Expand All @@ -29,6 +29,12 @@ public final class VertxRedisClientSingletons {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.vertx-redis-client-4.0";
private static final Instrumenter<VertxRedisClientRequest, Void> INSTRUMENTER;

private static final ThreadLocal<RedisURI> redisUriThreadLocal = new ThreadLocal<>();
private static final VirtualField<Command, String> commandNameField =
VirtualField.find(Command.class, String.class);
private static final VirtualField<RedisStandaloneConnection, RedisURI> redisUriField =
VirtualField.find(RedisStandaloneConnection.class, RedisURI.class);

static {
SpanNameExtractor<VertxRedisClientRequest> spanNameExtractor =
DbClientSpanNameExtractor.create(VertxRedisClientAttributesGetter.INSTANCE);
Expand Down Expand Up @@ -74,8 +80,6 @@ public static <T> Future<T> wrapEndSpan(
return Future.fromCompletionStage(result);
}

private static final ThreadLocal<RedisURI> redisUriThreadLocal = new ThreadLocal<>();

public static RedisURI getRedisUriThreadLocal() {
return redisUriThreadLocal.get();
}
Expand All @@ -84,9 +88,6 @@ public static void setRedisUriThreadLocal(RedisURI redisUri) {
redisUriThreadLocal.set(redisUri);
}

private static final VirtualField<Command, String> commandNameField =
VirtualField.find(Command.class, String.class);

public static void setCommandName(Command command, String commandName) {
commandNameField.set(command, commandName);
}
Expand All @@ -95,9 +96,6 @@ public static String getCommandName(Command command) {
return commandNameField.get(command);
}

private static final VirtualField<RedisStandaloneConnection, RedisURI> redisUriField =
VirtualField.find(RedisStandaloneConnection.class, RedisURI.class);

public static void setRedisUri(RedisStandaloneConnection connection, RedisURI redisUri) {
redisUriField.set(connection, redisUri);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.api.semconv.network.internal.NetworkAttributes;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
import io.opentelemetry.semconv.SemanticAttributes;
import io.vertx.core.Vertx;
import io.vertx.redis.client.Redis;
Expand All @@ -25,7 +27,6 @@
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testcontainers.containers.GenericContainer;

@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
class VertxRedisClientTest {
@RegisterExtension
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
Expand Down Expand Up @@ -70,14 +71,7 @@ void setCommand() throws Exception {
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"))));
.hasAttributesSatisfyingExactly(redisSpanAttributes("SET", "SET foo ?"))));
}

@Test
Expand All @@ -103,27 +97,13 @@ void getCommand() throws Exception {
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"))),
.hasAttributesSatisfyingExactly(redisSpanAttributes("SET", "SET foo ?"))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "GET foo"),
equalTo(SemanticAttributes.DB_OPERATION, "GET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"))));
.hasAttributesSatisfyingExactly(redisSpanAttributes("GET", "GET foo"))));
}

@Test
Expand Down Expand Up @@ -163,29 +143,15 @@ void getCommandWithParent() throws Exception {
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"))),
.hasAttributesSatisfyingExactly(redisSpanAttributes("SET", "SET foo ?"))),
trace ->
trace.hasSpansSatisfyingExactly(
span -> span.hasName("parent").hasKind(SpanKind.INTERNAL),
span ->
span.hasName("GET")
.hasKind(SpanKind.CLIENT)
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "GET foo"),
equalTo(SemanticAttributes.DB_OPERATION, "GET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1")),
.hasAttributesSatisfyingExactly(redisSpanAttributes("GET", "GET foo")),
span ->
span.hasName("callback")
.hasKind(SpanKind.INTERNAL)
Expand Down Expand Up @@ -216,26 +182,25 @@ void commandWithNoArguments() throws Exception {
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"))),
.hasAttributesSatisfyingExactly(redisSpanAttributes("SET", "SET foo ?"))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("RANDOMKEY")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "RANDOMKEY"),
equalTo(SemanticAttributes.DB_OPERATION, "RANDOMKEY"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"))));
redisSpanAttributes("RANDOMKEY", "RANDOMKEY"))));
}

private static AttributeAssertion[] redisSpanAttributes(String operation, String statement) {
return new AttributeAssertion[] {
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, statement),
equalTo(SemanticAttributes.DB_OPERATION, operation),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1")
};
}
}

0 comments on commit 8b38aea

Please sign in to comment.