From 7f84218b22e52b25eb5dd160a1cd05c4471fc0a0 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Sun, 11 Feb 2024 22:04:40 +0530 Subject: [PATCH] minor refactoring --- .../javaagent/build.gradle.kts | 3 ++- .../AerospikeClientInstrumentationModule.java | 7 +----- .../v7_1/AerospikeRequestContext.java | 7 +----- .../aerospike/v7_1/AersopikeSingletons.java | 10 ++++---- .../v7_1/AsyncHandlerInstrumentation.java | 4 ++-- .../AsyncScanAllCommandInstrumentation.java | 4 ++-- .../aerospike/v7_1/SocketInstrumentation.java | 2 +- .../v7_1/SyncCommandInstrumentation.java | 24 +++++++------------ .../aerospike/v7_1/AerospikeClientTest.java | 7 +++--- 9 files changed, 27 insertions(+), 41 deletions(-) diff --git a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/build.gradle.kts b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/build.gradle.kts index a1bb22b74941..daf1a888ed37 100644 --- a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/build.gradle.kts +++ b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/build.gradle.kts @@ -6,7 +6,7 @@ muzzle { pass { group.set("com.aerospike") module.set("aerospike-client") - versions.set("[4.4.9,)") + versions.set("[7.0.0,)") assertInverse.set(true) } } @@ -23,6 +23,7 @@ tasks { test { jvmArgs("-Djava.net.preferIPv4Stack=true") jvmArgs("-Dotel.instrumentation.aerospike.experimental-span-attributes=true") + jvmArgs("-Dotel.instrumentation.aerospike.experimental-metrics=true") usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service) } } 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 bb35a17114e9..aaa964a27f23 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 @@ -23,7 +23,7 @@ public AerospikeClientInstrumentationModule() { @Override public ElementMatcher.Junction classLoaderMatcher() { - return hasClassesNamed("com.aerospike.client.AerospikeClient"); + return hasClassesNamed("com.aerospike.client.AerospikeClient").and(hasClassesNamed("com.aerospike.client.metrics.MetricsPolicy")); } @Override @@ -35,9 +35,4 @@ public List typeInstrumentations() { new AsyncHandlerInstrumentation(), new AsyncScanAllCommandInstrumentation()); } - - @Override - public boolean isIndyModule() { - return false; - } } diff --git a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeRequestContext.java b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeRequestContext.java index 8e90ae8d5ac0..078adc5bad59 100644 --- a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeRequestContext.java +++ b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeRequestContext.java @@ -29,16 +29,11 @@ public void detachAndEnd() { contextThreadLocal.remove(); } - @SuppressWarnings("unchecked") public static AerospikeRequestContext current() { return contextThreadLocal.get(); } - public void endSpan( - Instrumenter instrumenter, - Context context, - AerospikeRequest request, - Throwable throwable) { + public void endSpan(Instrumenter instrumenter, Throwable throwable) { instrumenter.end(context, request, null, throwable); } diff --git a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AersopikeSingletons.java b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AersopikeSingletons.java index 879340568a76..b0aa16fa4447 100644 --- a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AersopikeSingletons.java +++ b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AersopikeSingletons.java @@ -32,12 +32,14 @@ public final class AersopikeSingletons { INSTRUMENTATION_NAME, DbClientSpanNameExtractor.create(aerospikeDbAttributesGetter)) .addAttributesExtractor(DbClientAttributesExtractor.create(aerospikeDbAttributesGetter)) - .addAttributesExtractor(NetworkAttributesExtractor.create(netAttributesGetter)) - .addOperationMetrics(AerospikeMetrics.get()); - if (InstrumentationConfig.get() - .getBoolean("otel.instrumentation.aerospike.experimental-span-attributes", false)) { + .addAttributesExtractor(NetworkAttributesExtractor.create(netAttributesGetter)); + InstrumentationConfig instrumentationConfig = InstrumentationConfig.get(); + if (instrumentationConfig.getBoolean("otel.instrumentation.aerospike.experimental-span-attributes", false)) { builder.addAttributesExtractor(new AerospikeClientAttributeExtractor()); } + if (instrumentationConfig.getBoolean("otel.instrumentation.aerospike.experimental-metrics", false)) { + builder.addOperationMetrics(AerospikeMetrics.get()); + } INSTRUMENTER = builder.buildInstrumenter(SpanKindExtractor.alwaysClient()); } diff --git a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AsyncHandlerInstrumentation.java b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AsyncHandlerInstrumentation.java index f9c8717efd7b..146592d8978a 100644 --- a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AsyncHandlerInstrumentation.java +++ b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AsyncHandlerInstrumentation.java @@ -62,7 +62,7 @@ public static void onExit(@Advice.Thrown Throwable throwable, @Advice.This Comma } else { request.setStatus(Status.FAILURE); } - requestContext.endSpan(AersopikeSingletons.instrumenter(), context, request, throwable); + requestContext.endSpan(AersopikeSingletons.instrumenter(), throwable); } } } @@ -80,7 +80,7 @@ public static void onExit(@Advice.Thrown Throwable throwable, @Advice.This Comma AerospikeRequest request = requestContext.getRequest(); Context context = requestContext.getContext(); request.setStatus(Status.FAILURE); - requestContext.endSpan(AersopikeSingletons.instrumenter(), context, request, throwable); + requestContext.endSpan(AersopikeSingletons.instrumenter(), throwable); } } } diff --git a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AsyncScanAllCommandInstrumentation.java b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AsyncScanAllCommandInstrumentation.java index d705a74c1343..fa1328c9e667 100644 --- a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AsyncScanAllCommandInstrumentation.java +++ b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AsyncScanAllCommandInstrumentation.java @@ -92,7 +92,7 @@ public static void onExit( } else { request.setStatus(Status.FAILURE); } - requestContext.endSpan(AersopikeSingletons.instrumenter(), context, request, throwable); + requestContext.endSpan(AersopikeSingletons.instrumenter(), throwable); } } } @@ -112,7 +112,7 @@ public static void onExit( AerospikeRequest request = requestContext.getRequest(); Context context = requestContext.getContext(); request.setStatus(Status.FAILURE); - requestContext.endSpan(AersopikeSingletons.instrumenter(), context, request, throwable); + requestContext.endSpan(AersopikeSingletons.instrumenter(), throwable); } } } diff --git a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/SocketInstrumentation.java b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/SocketInstrumentation.java index d50ffc9aa4da..0f68062a09a0 100644 --- a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/SocketInstrumentation.java +++ b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/SocketInstrumentation.java @@ -78,7 +78,7 @@ public static void stopSpan( request.setNode(node); if (throwable != null) { request.setStatus(FAILURE); - requestContext.endSpan(instrumenter(), requestContext.getContext(), request, throwable); + requestContext.endSpan(instrumenter(), throwable); requestContext.detachAndEnd(); } } 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 3e2009cbe009..8dd41e13f38a 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 @@ -123,7 +123,7 @@ public static void stopSpan( scope.close(); if (requestContext != null) { - requestContext.endSpan(AersopikeSingletons.instrumenter(), context, request, throwable); + requestContext.endSpan(AersopikeSingletons.instrumenter(), throwable); requestContext.detachAndEnd(); } } @@ -159,7 +159,7 @@ public static AerospikeRequestContext onEnter( } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) - public static AerospikeRequestContext stopSpan( + public static void stopSpan( @Advice.Thrown Throwable throwable, @Advice.Enter AerospikeRequestContext requestContext, @Advice.Local("otelAerospikeRequest") AerospikeRequest request, @@ -170,16 +170,14 @@ public static AerospikeRequestContext stopSpan( } else { request.setStatus(Status.SUCCESS); } - if (scope == null) { - return requestContext; + if (scope != null) { + scope.close(); } - scope.close(); if (requestContext != null) { - requestContext.endSpan(AersopikeSingletons.instrumenter(), context, request, throwable); + requestContext.endSpan(AersopikeSingletons.instrumenter(), throwable); requestContext.detachAndEnd(); } - return requestContext; } } @@ -195,7 +193,6 @@ public static AerospikeRequestContext onEnter( @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - request = AerospikeRequest.create(methodName.toUpperCase(Locale.ROOT), namespace, setName); if (!AersopikeSingletons.instrumenter().shouldStart(parentContext, request)) { return null; @@ -206,7 +203,7 @@ public static AerospikeRequestContext onEnter( } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) - public static AerospikeRequestContext stopSpan( + public static void stopSpan( @Advice.Thrown Throwable throwable, @Advice.Enter AerospikeRequestContext requestContext, @Advice.Local("otelAerospikeRequest") AerospikeRequest request, @@ -217,16 +214,13 @@ public static AerospikeRequestContext stopSpan( } else { request.setStatus(Status.SUCCESS); } - if (scope == null) { - return requestContext; + if (scope != null) { + scope.close(); } - - scope.close(); if (requestContext != null) { - requestContext.endSpan(AersopikeSingletons.instrumenter(), context, request, throwable); + requestContext.endSpan(AersopikeSingletons.instrumenter(), throwable); requestContext.detachAndEnd(); } - return requestContext; } } } diff --git a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientTest.java b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientTest.java index dd066bf1076f..5f92cb7a7d8e 100644 --- a/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientTest.java +++ b/instrumentation/aerospike-client/aerospike-client-7.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/aerospike/v7_1/AerospikeClientTest.java @@ -15,7 +15,6 @@ 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; import com.aerospike.client.policy.ClientPolicy; @@ -62,11 +61,11 @@ static void setupSpec() { port = aerospikeServer.getMappedPort(3000); ClientPolicy clientPolicy = new ClientPolicy(); int eventLoopSize = Runtime.getRuntime().availableProcessors(); + System.out.println(eventLoopSize); EventPolicy eventPolicy = new EventPolicy(); eventPolicy.commandsPerEventLoop = 2; - EventLoops eventLoops = new NioEventLoops(eventPolicy, eventLoopSize); - clientPolicy.eventLoops = eventLoops; - clientPolicy.maxConnsPerNode = 11; + clientPolicy.eventLoops = new NioEventLoops(eventPolicy, eventLoopSize); + clientPolicy.maxConnsPerNode = eventLoopSize; clientPolicy.failIfNotConnected = true; aerospikeClient = new AerospikeClient(clientPolicy, "localhost", port); }