From 14f1109623de0184aa39d058cfdc8db6c55ec677 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Fri, 10 Nov 2023 22:30:30 +0530 Subject: [PATCH] refactors tests --- .../AerospikeClientInstrumentationModule.java | 1 - .../v7_1/SyncCommandInstrumentation.java | 9 ++ .../v7_1/TransferSizeIntrumentation.java | 44 ------- .../v7_1/AerospikeClientSyncCommandTest.java | 110 ------------------ ...mandTest.java => AerospikeClientTest.java} | 41 +++++-- 5 files changed, 41 insertions(+), 164 deletions(-) delete mode 100644 instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/TransferSizeIntrumentation.java delete mode 100644 instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientSyncCommandTest.java rename instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/{AerospikeClientAsyncCommandTest.java => AerospikeClientTest.java} (73%) diff --git a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientInstrumentationModule.java b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientInstrumentationModule.java index 1edccfc3b8ea..bb35a17114e9 100644 --- a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientInstrumentationModule.java +++ b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientInstrumentationModule.java @@ -31,7 +31,6 @@ public List typeInstrumentations() { return asList( new SyncCommandInstrumentation(), new SocketInstrumentation(), - new TransferSizeIntrumentation(), new AsyncCommandInstrumentation(), new AsyncHandlerInstrumentation(), new AsyncScanAllCommandInstrumentation()); diff --git a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/SyncCommandInstrumentation.java b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/SyncCommandInstrumentation.java index 44c74f234bb6..3e2009cbe009 100644 --- a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/SyncCommandInstrumentation.java +++ b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/SyncCommandInstrumentation.java @@ -108,6 +108,15 @@ public static void stopSpan( } else { request.setStatus(Status.SUCCESS); } + if (record == null || record.bins.isEmpty()) { + request.setSize(0); + } else { + int size = 0; + for (Object value : record.bins.values()) { + size += ((String) value).length(); + } + request.setSize(size); + } if (scope == null) { return; } diff --git a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/TransferSizeIntrumentation.java b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/TransferSizeIntrumentation.java deleted file mode 100644 index ce5b8e376e53..000000000000 --- a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/TransferSizeIntrumentation.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.aerospike.v7_1; - -import static net.bytebuddy.matcher.ElementMatchers.hasSuperClass; -import static net.bytebuddy.matcher.ElementMatchers.isMethod; -import static net.bytebuddy.matcher.ElementMatchers.isPublic; -import static net.bytebuddy.matcher.ElementMatchers.named; -import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; - -import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; -import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import net.bytebuddy.asm.Advice; -import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.matcher.ElementMatcher; - -public class TransferSizeIntrumentation implements TypeInstrumentation { - @Override - public ElementMatcher typeMatcher() { - return hasSuperClass(named("com.aerospike.client.command.Command")); - } - - @Override - public void transform(TypeTransformer transformer) { - transformer.applyAdviceToMethod( - isMethod().and(isPublic()).and(named("execute")).and(takesNoArguments()), - this.getClass().getName() + "$SizeAdvice"); - } - - @SuppressWarnings("unused") - public static class SizeAdvice { - @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) - public static void stopSpan(@Advice.FieldValue("dataOffset") int dataOffset) { - AerospikeRequestContext context = AerospikeRequestContext.current(); - if (context != null) { - AerospikeRequest request = context.getRequest(); - request.setSize(dataOffset); - } - } - } -} diff --git a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientSyncCommandTest.java b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientSyncCommandTest.java deleted file mode 100644 index 85e8e83dca9e..000000000000 --- a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientSyncCommandTest.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.aerospike.v7_1; - -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; -import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; -import static java.util.Collections.singletonList; -import static org.assertj.core.api.Assertions.assertThat; - -import com.aerospike.client.AerospikeClient; -import com.aerospike.client.Bin; -import com.aerospike.client.Key; -import com.aerospike.client.Record; -import io.opentelemetry.api.trace.SpanKind; -import io.opentelemetry.instrumentation.api.instrumenter.db.AerospikeSemanticAttributes; -import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; -import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import io.opentelemetry.semconv.SemanticAttributes; -import java.util.List; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.wait.strategy.Wait; - -@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0 -class AerospikeClientSyncCommandTest { - @RegisterExtension - static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - - static GenericContainer aerospikeServer = - new GenericContainer<>("aerospike/aerospike-server:6.2.0.0") - .withExposedPorts(3000) - .waitingFor(Wait.forLogMessage(".*replication factor is 1.*", 1)); - static int port; - - static AerospikeClient aerospikeClient; - - @BeforeAll - static void setupSpec() { - aerospikeServer.start(); - port = aerospikeServer.getMappedPort(3000); - aerospikeClient = new AerospikeClient("localhost", port); - } - - @AfterAll - static void cleanupSpec() { - aerospikeClient.close(); - aerospikeServer.stop(); - } - - @BeforeEach - void setup() { - testing.clearData(); - } - - @Test - void putAndGetCommand() { - Key aerospikeKey = new Key("test", "test-set", "data1"); - aerospikeClient.put(null, aerospikeKey, new Bin("bin1", "value1")); - List bins = singletonList("bin1"); - Record aerospikeRecord = aerospikeClient.get(null, aerospikeKey, bins.toArray(new String[0])); - assertThat(aerospikeRecord.getString("bin1")).isEqualTo("value1"); - - testing.waitAndAssertTraces( - trace -> - trace.hasSpansSatisfyingExactly( - span -> - span.hasName("PUT") - .hasKind(SpanKind.CLIENT) - .hasAttributesSatisfyingExactly( - equalTo(SemanticAttributes.DB_SYSTEM, "aerospike"), - equalTo(SemanticAttributes.DB_OPERATION, "PUT"), - equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port), - equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"), - equalTo(AerospikeSemanticAttributes.AEROSPIKE_NAMESPACE, "test"), - equalTo(AerospikeSemanticAttributes.AEROSPIKE_SET_NAME, "test-set"), - equalTo(AerospikeSemanticAttributes.AEROSPIKE_USER_KEY, "data1"), - equalTo(AerospikeSemanticAttributes.AEROSPIKE_STATUS, "SUCCESS"), - equalTo(AerospikeSemanticAttributes.AEROSPIKE_ERROR_CODE, 0), - equalTo(AerospikeSemanticAttributes.AEROSPIKE_TRANSFER_SIZE, 95), - satisfies( - SemanticAttributes.NET_SOCK_PEER_NAME, - val -> val.isIn("localhost", "127.0.0.1")))), - trace -> - trace.hasSpansSatisfyingExactly( - span -> - span.hasName("GET") - .hasKind(SpanKind.CLIENT) - .hasAttributesSatisfyingExactly( - equalTo(SemanticAttributes.DB_SYSTEM, "aerospike"), - equalTo(SemanticAttributes.DB_OPERATION, "GET"), - equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port), - equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"), - equalTo(AerospikeSemanticAttributes.AEROSPIKE_NAMESPACE, "test"), - equalTo(AerospikeSemanticAttributes.AEROSPIKE_SET_NAME, "test-set"), - equalTo(AerospikeSemanticAttributes.AEROSPIKE_USER_KEY, "data1"), - equalTo(AerospikeSemanticAttributes.AEROSPIKE_STATUS, "SUCCESS"), - equalTo(AerospikeSemanticAttributes.AEROSPIKE_ERROR_CODE, 0), - equalTo(AerospikeSemanticAttributes.AEROSPIKE_TRANSFER_SIZE, 40), - satisfies( - SemanticAttributes.NET_SOCK_PEER_NAME, - val -> val.isIn("localhost", "127.0.0.1"))))); - } -} diff --git a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientAsyncCommandTest.java b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientTest.java similarity index 73% rename from instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientAsyncCommandTest.java rename to instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientTest.java index 30a91aae92ba..760d0321d07a 100644 --- a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientAsyncCommandTest.java +++ b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientTest.java @@ -8,10 +8,12 @@ import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; import com.aerospike.client.AerospikeClient; import com.aerospike.client.Bin; import com.aerospike.client.Key; +import com.aerospike.client.Record; import com.aerospike.client.async.EventLoops; import com.aerospike.client.async.EventPolicy; import com.aerospike.client.async.NioEventLoops; @@ -31,7 +33,7 @@ import org.testcontainers.containers.wait.strategy.Wait; @SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0 -class AerospikeClientAsyncCommandTest { +class AerospikeClientTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); @@ -71,7 +73,7 @@ void setup() { } @Test - void putCommand() { + void asyncCommand() { Key aerospikeKey = new Key("test", "test-set", "data1"); aerospikeClient.put(null, null, null, aerospikeKey, new Bin("bin1", "value1")); @@ -97,27 +99,48 @@ void putCommand() { } @Test - void getCommand() { - Key aerospikeKey = new Key("test", "test-set", "data1"); - List bins = singletonList("bin1"); - aerospikeClient.get(null, null, null, aerospikeKey, bins.toArray(new String[0])); + void syncCommand() { + Key aerospikeKey = new Key("test", "test-set", "data2"); + aerospikeClient.put(null, aerospikeKey, new Bin("bin2", "value2")); + List bins = singletonList("bin2"); + Record aerospikeRecord = aerospikeClient.get(null, aerospikeKey, bins.toArray(new String[0])); + assertThat(aerospikeRecord.getString("bin2")).isEqualTo("value2"); testing.waitAndAssertTraces( trace -> trace.hasSpansSatisfyingExactly( span -> - span.hasName("ASYNCREAD") + span.hasName("PUT") .hasKind(SpanKind.CLIENT) .hasAttributesSatisfyingExactly( equalTo(SemanticAttributes.DB_SYSTEM, "aerospike"), - equalTo(SemanticAttributes.DB_OPERATION, "ASYNCREAD"), + equalTo(SemanticAttributes.DB_OPERATION, "PUT"), equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port), equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"), equalTo(AerospikeSemanticAttributes.AEROSPIKE_NAMESPACE, "test"), equalTo(AerospikeSemanticAttributes.AEROSPIKE_SET_NAME, "test-set"), - equalTo(AerospikeSemanticAttributes.AEROSPIKE_USER_KEY, "data1"), + equalTo(AerospikeSemanticAttributes.AEROSPIKE_USER_KEY, "data2"), + equalTo(AerospikeSemanticAttributes.AEROSPIKE_STATUS, "SUCCESS"), + equalTo(AerospikeSemanticAttributes.AEROSPIKE_ERROR_CODE, 0), + satisfies( + SemanticAttributes.NET_SOCK_PEER_NAME, + val -> val.isIn("localhost", "127.0.0.1")))), + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("GET") + .hasKind(SpanKind.CLIENT) + .hasAttributesSatisfyingExactly( + equalTo(SemanticAttributes.DB_SYSTEM, "aerospike"), + equalTo(SemanticAttributes.DB_OPERATION, "GET"), + equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port), + equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"), + equalTo(AerospikeSemanticAttributes.AEROSPIKE_NAMESPACE, "test"), + equalTo(AerospikeSemanticAttributes.AEROSPIKE_SET_NAME, "test-set"), + equalTo(AerospikeSemanticAttributes.AEROSPIKE_USER_KEY, "data2"), equalTo(AerospikeSemanticAttributes.AEROSPIKE_STATUS, "SUCCESS"), equalTo(AerospikeSemanticAttributes.AEROSPIKE_ERROR_CODE, 0), + equalTo(AerospikeSemanticAttributes.AEROSPIKE_TRANSFER_SIZE, 6), satisfies( SemanticAttributes.NET_SOCK_PEER_NAME, val -> val.isIn("localhost", "127.0.0.1")))));