From 72e3a8e3782a589f1267a795ca6984ccdddb171b Mon Sep 17 00:00:00 2001 From: huangweilong <13622993145@163.com> Date: Sun, 8 Dec 2024 13:11:39 +0800 Subject: [PATCH 1/5] Add support for Nacos Client (#9961) --- docs/supported-libraries.md | 1 + instrumentation/nacos-client-2.0.3/README.md | 55 ++++ .../javaagent/build.gradle.kts | 23 ++ .../client/v2_0_3/NacosClientConstants.java | 47 ++++ .../client/v2_0_3/NacosClientHelper.java | 156 +++++++++++ .../NacosClientInstrumentationModule.java | 26 ++ .../client/v2_0_3/NacosClientRequest.java | 46 ++++ .../v2_0_3/NacosClientRequestOperator.java | 33 +++ .../client/v2_0_3/NacosClientSingletons.java | 43 +++ .../advices/GrpcConnectionRequestAdvice.java | 46 ++++ .../RpcClientHandleServerRequestAdvice.java | 46 ++++ .../NacosClientCodeAttributesGetter.java | 19 ++ ...sClientExperimentalAttributeExtractor.java | 24 ++ .../NacosClientSpanNameExtractor.java | 12 + .../NacosClientSpanStatusExtractor.java | 22 ++ .../GrpcConnectionInstrumentation.java | 33 +++ .../RpcClientInstrumentation.java | 33 +++ .../common/remote/client/RpcClientTest.java | 108 ++++++++ .../client/grpc/GrpcConnectionTest.java | 136 ++++++++++ .../errorprone/annotations/DoNotMock.java | 15 ++ .../client/v2_0_3/NacosClientTestHelper.java | 254 ++++++++++++++++++ settings.gradle.kts | 1 + 22 files changed, 1179 insertions(+) create mode 100644 instrumentation/nacos-client-2.0.3/README.md create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientConstants.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientHelper.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientInstrumentationModule.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequest.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequestOperator.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientSingletons.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/GrpcConnectionRequestAdvice.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/RpcClientHandleServerRequestAdvice.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientCodeAttributesGetter.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientExperimentalAttributeExtractor.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanNameExtractor.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanStatusExtractor.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/GrpcConnectionInstrumentation.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/RpcClientInstrumentation.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java create mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientTestHelper.java diff --git a/docs/supported-libraries.md b/docs/supported-libraries.md index 74bea2ded278..f62ab13f3c4f 100644 --- a/docs/supported-libraries.md +++ b/docs/supported-libraries.md @@ -101,6 +101,7 @@ These are the supported libraries and frameworks: | [Micrometer](https://micrometer.io/) | 1.5+ | [opentelemetry-micrometer-1.5](../instrumentation/micrometer/micrometer-1.5/library) | none | | [MongoDB Driver](https://mongodb.github.io/mongo-java-driver/) | 3.1+ | [opentelemetry-mongo-3.1](../instrumentation/mongo/mongo-3.1/library) | [Database Client Spans] | | [MyBatis](https://mybatis.org/mybatis-3/) | 3.2+ | N/A | none | +| [Nacos](https://nacos.io/) | 2.0.3+ | N/A | none | | [Netty HTTP codec [5]](https://github.com/netty/netty) | 3.8+ | [opentelemetry-netty-4.1](../instrumentation/netty/netty-4.1/library) | [HTTP Client Spans], [HTTP Client Metrics], [HTTP Server Spans], [HTTP Server Metrics] | | [OkHttp](https://github.com/square/okhttp/) | 2.2+ | [opentelemetry-okhttp-3.0](../instrumentation/okhttp/okhttp-3.0/library) | [HTTP Client Spans], [HTTP Client Metrics] | | [Oracle UCP](https://docs.oracle.com/database/121/JJUCP/) | 11.2+ | [opentelemetry-oracle-ucp-11.2](../instrumentation/oracle-ucp-11.2/library) | [Database Pool Metrics] | diff --git a/instrumentation/nacos-client-2.0.3/README.md b/instrumentation/nacos-client-2.0.3/README.md new file mode 100644 index 000000000000..09eae09ba1b0 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/README.md @@ -0,0 +1,55 @@ +## Enhancement Methods +- `com.alibaba.nacos.common.remote.client.grpc.GrpcConnection#request` +- `com.alibaba.nacos.common.remote.client.RpcClient#handleServerRequest` + +## Span Info Details + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Request Child ClassSpanNameAdditional Tags
InstanceRequestNacos/{$(lnstanceRequest.getType()}nacos.namespace nacos.group nacos.service.name
ServiceQueryRequestNacos/queryService
SubscribeServiceRequestNacos/subscribeService,Nacos/unsubscribeService
ServicelistRequestNacos/getServicelist
ConfigQueryRequestNacos/queryConfig
ConfigPublishRequestNacos/publishConfignacos.data.id nacos.group nacos.tenant
ConfigRemoveRequestNacos/removeConfig
ConfigChangeNotifyRequestNacos/notifyConfigChange
NotifySubscriberRequestNacos/notifySubscribeChangenacos.group nacos.service.name
diff --git a/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts b/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts new file mode 100644 index 000000000000..c45832ba5e14 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts @@ -0,0 +1,23 @@ +plugins { + id("otel.javaagent-instrumentation") +} + +muzzle { + pass { + group.set("com.alibaba.nacos") + module.set("nacos-client") + versions.set("[2.0.3,)") + assertInverse.set(true) + } +} + +dependencies { + implementation("com.alibaba.nacos:nacos-client:2.0.3") + testImplementation("javax.annotation:javax.annotation-api:1.3.2") +} + + +tasks.withType().configureEach { + jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED") + jvmArgs("-XX:+IgnoreUnrecognizedVMOptions") +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientConstants.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientConstants.java new file mode 100644 index 000000000000..290237cf3533 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientConstants.java @@ -0,0 +1,47 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; + +import io.opentelemetry.api.common.AttributeKey; + +public class NacosClientConstants { + private NacosClientConstants() { + } + + public static final String NACOS_PREFIX = "Nacos/"; + + public static final String SERVER_CHECK = "serverCheck"; + + public static final String QUERY_SERVICE = "queryService"; + + public static final String SUBSCRIBE_SERVICE = "subscribeService"; + + public static final String UNSUBSCRIBE_SERVICE = "unsubscribeService"; + + public static final String QUERY_CONFIG = "queryConfig"; + + public static final String PUBLISH_CONFIG = "publishConfig"; + + public static final String REMOVE_CONFIG = "removeConfig"; + + public static final String GET_SERVICE_LIST = "getServiceList"; + + public static final String NOTIFY_SUBSCRIBE_CHANGE = "notifySubscribeChange"; + + public static final String NOTIFY_CONFIG_CHANGE = "notifyConfigChange"; + + public static final AttributeKey NACOS_NAME_SPACE_ATTR = AttributeKey.stringKey( + "nacos.namespace"); + + public static final AttributeKey NACOS_GROUP_NAME_ATTR = AttributeKey.stringKey("nacos.group.name"); + + public static final AttributeKey NACOS_SERVICE_NAME_ATTR = AttributeKey.stringKey( + "nacos.service.name"); + + public static final AttributeKey NACOS_DATA_ID_ATTR = AttributeKey.stringKey( + "nacos.data.id"); + + public static final AttributeKey NACOS_GROUP_ATTR = AttributeKey.stringKey("nacos.group"); + + public static final AttributeKey NACOS_TENANT_ATTR = AttributeKey.stringKey( + "nacos.tenant"); + +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientHelper.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientHelper.java new file mode 100644 index 000000000000..4d1525f8adc0 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientHelper.java @@ -0,0 +1,156 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; + +import com.alibaba.nacos.api.config.remote.request.ConfigChangeNotifyRequest; +import com.alibaba.nacos.api.config.remote.request.ConfigPublishRequest; +import com.alibaba.nacos.api.config.remote.request.ConfigQueryRequest; +import com.alibaba.nacos.api.config.remote.request.ConfigRemoveRequest; +import com.alibaba.nacos.api.naming.remote.request.InstanceRequest; +import com.alibaba.nacos.api.naming.remote.request.NotifySubscriberRequest; +import com.alibaba.nacos.api.naming.remote.request.ServiceListRequest; +import com.alibaba.nacos.api.naming.remote.request.ServiceQueryRequest; +import com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest; +import com.alibaba.nacos.api.remote.request.Request; +import java.util.HashMap; +import java.util.Map; +import javax.annotation.Nonnull; + +public class NacosClientHelper { + private static final NacosClientRequestOperator UNKNOWN_OPERATOR = new NacosClientRequestOperator( + request -> request.getClass().getSimpleName(), null); + private static final Map, NacosClientRequestOperator> KNOWN_OPERATOR_MAP = new HashMap<>(); + + private NacosClientHelper() {} + + static { + KNOWN_OPERATOR_MAP.put(InstanceRequest.class, new NacosClientRequestOperator( + request -> ((InstanceRequest) request).getType(), + (attributesBuilder, request) -> { + InstanceRequest instanceRequest = (InstanceRequest) request; + attributesBuilder.put(NacosClientConstants.NACOS_NAME_SPACE_ATTR, + instanceRequest.getNamespace()); + attributesBuilder.put(NacosClientConstants.NACOS_GROUP_NAME_ATTR, + instanceRequest.getGroupName()); + attributesBuilder.put(NacosClientConstants.NACOS_SERVICE_NAME_ATTR, + instanceRequest.getServiceName()); + }) + ); + + KNOWN_OPERATOR_MAP.put(ServiceQueryRequest.class, new NacosClientRequestOperator( + request -> NacosClientConstants.QUERY_SERVICE, + (attributesBuilder, request) -> { + ServiceQueryRequest serviceQueryRequest = (ServiceQueryRequest) request; + attributesBuilder.put(NacosClientConstants.NACOS_NAME_SPACE_ATTR, + serviceQueryRequest.getNamespace()); + attributesBuilder.put(NacosClientConstants.NACOS_GROUP_NAME_ATTR, + serviceQueryRequest.getGroupName()); + attributesBuilder.put(NacosClientConstants.NACOS_SERVICE_NAME_ATTR, + serviceQueryRequest.getServiceName()); + }) + ); + + KNOWN_OPERATOR_MAP.put(SubscribeServiceRequest.class, new NacosClientRequestOperator( + request -> ((SubscribeServiceRequest) request).isSubscribe() + ? NacosClientConstants.SUBSCRIBE_SERVICE + : NacosClientConstants.UNSUBSCRIBE_SERVICE, + (attributesBuilder, request) -> { + SubscribeServiceRequest subscribeServiceRequest = (SubscribeServiceRequest) request; + attributesBuilder.put(NacosClientConstants.NACOS_NAME_SPACE_ATTR, + subscribeServiceRequest.getNamespace()); + attributesBuilder.put(NacosClientConstants.NACOS_GROUP_NAME_ATTR, + subscribeServiceRequest.getGroupName()); + attributesBuilder.put(NacosClientConstants.NACOS_SERVICE_NAME_ATTR, + subscribeServiceRequest.getServiceName()); + }) + ); + + KNOWN_OPERATOR_MAP.put(ServiceListRequest.class, new NacosClientRequestOperator( + request -> NacosClientConstants.GET_SERVICE_LIST, + (attributesBuilder, request) -> { + ServiceListRequest serviceListRequest = (ServiceListRequest) request; + attributesBuilder.put(NacosClientConstants.NACOS_NAME_SPACE_ATTR, + serviceListRequest.getNamespace()); + attributesBuilder.put(NacosClientConstants.NACOS_GROUP_NAME_ATTR, + serviceListRequest.getGroupName()); + attributesBuilder.put(NacosClientConstants.NACOS_SERVICE_NAME_ATTR, + serviceListRequest.getServiceName()); + }) + ); + + KNOWN_OPERATOR_MAP.put(ConfigQueryRequest.class, new NacosClientRequestOperator( + request -> NacosClientConstants.QUERY_CONFIG, + (attributesBuilder, request) -> { + ConfigQueryRequest configQueryRequest = (ConfigQueryRequest) request; + attributesBuilder.put(NacosClientConstants.NACOS_DATA_ID_ATTR, + configQueryRequest.getDataId()); + attributesBuilder.put(NacosClientConstants.NACOS_GROUP_ATTR, + configQueryRequest.getGroup()); + attributesBuilder.put(NacosClientConstants.NACOS_TENANT_ATTR, + configQueryRequest.getTenant()); + }) + ); + + KNOWN_OPERATOR_MAP.put(ConfigPublishRequest.class, new NacosClientRequestOperator( + request -> NacosClientConstants.PUBLISH_CONFIG, + (attributesBuilder, request) -> { + ConfigPublishRequest configPublishRequest = (ConfigPublishRequest) request; + attributesBuilder.put(NacosClientConstants.NACOS_DATA_ID_ATTR, + configPublishRequest.getDataId()); + attributesBuilder.put(NacosClientConstants.NACOS_GROUP_ATTR, + configPublishRequest.getGroup()); + attributesBuilder.put(NacosClientConstants.NACOS_TENANT_ATTR, + configPublishRequest.getTenant()); + }) + ); + + KNOWN_OPERATOR_MAP.put(ConfigRemoveRequest.class, new NacosClientRequestOperator( + request -> NacosClientConstants.REMOVE_CONFIG, + (attributesBuilder, request) -> { + ConfigRemoveRequest configRemoveRequest = (ConfigRemoveRequest) request; + attributesBuilder.put(NacosClientConstants.NACOS_DATA_ID_ATTR, + configRemoveRequest.getDataId()); + attributesBuilder.put(NacosClientConstants.NACOS_GROUP_ATTR, + configRemoveRequest.getGroup()); + attributesBuilder.put(NacosClientConstants.NACOS_TENANT_ATTR, + configRemoveRequest.getTenant()); + }) + ); + + KNOWN_OPERATOR_MAP.put(NotifySubscriberRequest.class, new NacosClientRequestOperator( + request -> NacosClientConstants.NOTIFY_SUBSCRIBE_CHANGE, + (attributesBuilder, request) -> { + NotifySubscriberRequest notifySubscriberRequest = (NotifySubscriberRequest) request; + attributesBuilder.put(NacosClientConstants.NACOS_NAME_SPACE_ATTR, + notifySubscriberRequest.getNamespace()); + attributesBuilder.put(NacosClientConstants.NACOS_GROUP_NAME_ATTR, + notifySubscriberRequest.getGroupName()); + attributesBuilder.put(NacosClientConstants.NACOS_SERVICE_NAME_ATTR, + notifySubscriberRequest.getServiceName()); + }) + ); + + KNOWN_OPERATOR_MAP.put(ConfigChangeNotifyRequest.class, new NacosClientRequestOperator( + request -> NacosClientConstants.NOTIFY_CONFIG_CHANGE, + (attributesBuilder, request) -> { + ConfigChangeNotifyRequest configChangeNotifyRequest = (ConfigChangeNotifyRequest) request; + attributesBuilder.put(NacosClientConstants.NACOS_DATA_ID_ATTR, + configChangeNotifyRequest.getDataId()); + attributesBuilder.put(NacosClientConstants.NACOS_GROUP_ATTR, + configChangeNotifyRequest.getGroup()); + attributesBuilder.put(NacosClientConstants.NACOS_TENANT_ATTR, + configChangeNotifyRequest.getTenant()); + }) + ); + } + + @Nonnull + public static NacosClientRequestOperator getOperator(@Nonnull Request request) { + NacosClientRequestOperator nacosClientRequestOperator = KNOWN_OPERATOR_MAP.get( + request.getClass()); + if (nacosClientRequestOperator != null) { + return nacosClientRequestOperator; + } + return UNKNOWN_OPERATOR; + } + + +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientInstrumentationModule.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientInstrumentationModule.java new file mode 100644 index 000000000000..48009a6287fb --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientInstrumentationModule.java @@ -0,0 +1,26 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; + +import com.google.auto.service.AutoService; +import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.instrumentations.GrpcConnectionInstrumentation; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.instrumentations.RpcClientInstrumentation; +import java.util.Arrays; +import java.util.List; + +@AutoService(InstrumentationModule.class) +public class NacosClientInstrumentationModule extends InstrumentationModule implements + ExperimentalInstrumentationModule { + public NacosClientInstrumentationModule() { + super("nacos-client", "nacos-client-2.0.3"); + } + + @Override + public List typeInstrumentations() { + return Arrays.asList( + new GrpcConnectionInstrumentation(), + new RpcClientInstrumentation() + ); + } +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequest.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequest.java new file mode 100644 index 000000000000..ffca33c293d7 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequest.java @@ -0,0 +1,46 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; + +import com.alibaba.nacos.api.remote.request.Request; +import io.opentelemetry.api.common.Attributes; +import javax.annotation.Nonnull; + +public final class NacosClientRequest { + private final String methodName; + private final Class declaringClass; + private final String spanName; + private final Attributes attributes; + + public NacosClientRequest(String methodName, Class declaringClass, String spanName, + Attributes attributes) { + this.methodName = methodName; + this.declaringClass = declaringClass; + this.spanName = spanName; + this.attributes = attributes; + } + + @Nonnull + public static NacosClientRequest createRequest(@Nonnull String methodName, + @Nonnull Class declaringClass, @Nonnull Request request) { + NacosClientRequestOperator operator = NacosClientHelper.getOperator(request); + String spanName = operator.getName(request); + Attributes attributes = operator.getAttributes(request); + return new NacosClientRequest(methodName, declaringClass, spanName, attributes); + } + + public String getMethodName() { + return methodName; + } + + public Class getDeclaringClass() { + return declaringClass; + } + + public String getSpanName() { + return spanName; + } + + public Attributes getAttributes() { + return attributes; + } + +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequestOperator.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequestOperator.java new file mode 100644 index 000000000000..0d6a83387daf --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequestOperator.java @@ -0,0 +1,33 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; + +import com.alibaba.nacos.api.remote.request.Request; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.common.AttributesBuilder; +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class NacosClientRequestOperator { + private final Function spanNameHandler; + private final BiConsumer attributesHandler; + + public NacosClientRequestOperator( + Function spanNameHandler, + BiConsumer attributesHandler) { + this.spanNameHandler = spanNameHandler; + this.attributesHandler = attributesHandler; + } + + public String getName(Request request) { + return spanNameHandler == null ? "" + : NacosClientConstants.NACOS_PREFIX + spanNameHandler.apply(request); + } + + public Attributes getAttributes(Request request) { + AttributesBuilder builder = Attributes.builder(); + if (attributesHandler != null) { + attributesHandler.accept(builder, request); + } + return builder.build(); + } + +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientSingletons.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientSingletons.java new file mode 100644 index 000000000000..1dcbd0ffb7bd --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientSingletons.java @@ -0,0 +1,43 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; + +import com.alibaba.nacos.api.remote.response.Response; +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesExtractor; +import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesGetter; +import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; +import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; +import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors.NacosClientCodeAttributesGetter; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors.NacosClientExperimentalAttributeExtractor; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors.NacosClientSpanNameExtractor; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors.NacosClientSpanStatusExtractor; + +public final class NacosClientSingletons { + private static final String INSTRUMENTATION_NAME = "io.opentelemetry.nacos-client-2.0.3"; + private static final Instrumenter INSTRUMENTER = create(); + + public static Instrumenter instrumenter() { + return INSTRUMENTER; + } + + private static Instrumenter create() { + CodeAttributesGetter codeAttributesGetter = new NacosClientCodeAttributesGetter(); + SpanNameExtractor spanNameExtractor = new NacosClientSpanNameExtractor(); + SpanStatusExtractor spanStatusExtractor = new NacosClientSpanStatusExtractor(); + InstrumenterBuilder builder = + Instrumenter.builder( + GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, spanNameExtractor) + .addAttributesExtractor(CodeAttributesExtractor.create(codeAttributesGetter)) + .setSpanStatusExtractor(spanStatusExtractor); + builder.addAttributesExtractor( + AttributesExtractor.constant(AttributeKey.stringKey("service.discovery.system"), "nacos") + ); + builder.addAttributesExtractor(new NacosClientExperimentalAttributeExtractor()); + return builder.buildInstrumenter(); + } + + private NacosClientSingletons() {} +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/GrpcConnectionRequestAdvice.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/GrpcConnectionRequestAdvice.java new file mode 100644 index 000000000000..f4aaf3546321 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/GrpcConnectionRequestAdvice.java @@ -0,0 +1,46 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.advices; + +import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; +import static io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientSingletons.instrumenter; + +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.api.remote.response.Response; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientRequest; +import net.bytebuddy.asm.Advice; + +public class GrpcConnectionRequestAdvice { + @SuppressWarnings("unused") + @Advice.OnMethodEnter(suppress = Throwable.class) + public static void requestEnter( + @Advice.This Object thisObject, + @Advice.Argument(0) Request request, + @Advice.Local("otelRequest") NacosClientRequest nacosClientRequest, + @Advice.Local("otelContext") Context context, + @Advice.Local("otelScope") Scope scope) { + Context parentContext = currentContext(); + nacosClientRequest = NacosClientRequest.createRequest("request", thisObject.getClass(), + request); + if (!instrumenter().shouldStart(parentContext, nacosClientRequest)) { + return; + } + context = instrumenter().start(parentContext, nacosClientRequest); + scope = context.makeCurrent(); + } + + @SuppressWarnings("unused") + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) + public static void requestExit( + @Advice.Return Response response, + @Advice.Thrown Throwable throwable, + @Advice.Local("otelRequest") NacosClientRequest nacosClientRequest, + @Advice.Local("otelContext") Context context, + @Advice.Local("otelScope") Scope scope) { + if (scope == null) { + return; + } + scope.close(); + instrumenter().end(context, nacosClientRequest, response, throwable); + } +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/RpcClientHandleServerRequestAdvice.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/RpcClientHandleServerRequestAdvice.java new file mode 100644 index 000000000000..c21d4cfa6226 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/RpcClientHandleServerRequestAdvice.java @@ -0,0 +1,46 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.advices; + +import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; +import static io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientSingletons.instrumenter; + +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.api.remote.response.Response; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientRequest; +import net.bytebuddy.asm.Advice; + +public class RpcClientHandleServerRequestAdvice { + @SuppressWarnings("unused") + @Advice.OnMethodEnter(suppress = Throwable.class) + public static void handleServerRequestEnter( + @Advice.This Object thisObject, + @Advice.Argument(0) Request request, + @Advice.Local("otelRequest") NacosClientRequest nacosClientRequest, + @Advice.Local("otelContext") Context context, + @Advice.Local("otelScope") Scope scope) { + Context parentContext = currentContext(); + nacosClientRequest = NacosClientRequest.createRequest("handleServerRequest", + thisObject.getClass(), request); + if (!instrumenter().shouldStart(parentContext, nacosClientRequest)) { + return; + } + context = instrumenter().start(parentContext, nacosClientRequest); + scope = context.makeCurrent(); + } + + @SuppressWarnings("unused") + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) + public static void handleServerRequestExit( + @Advice.Return Response response, + @Advice.Thrown Throwable throwable, + @Advice.Local("otelRequest") NacosClientRequest nacosClientRequest, + @Advice.Local("otelContext") Context context, + @Advice.Local("otelScope") Scope scope) { + if (scope == null) { + return; + } + scope.close(); + instrumenter().end(context, nacosClientRequest, response, throwable); + } +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientCodeAttributesGetter.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientCodeAttributesGetter.java new file mode 100644 index 000000000000..3b2514b56484 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientCodeAttributesGetter.java @@ -0,0 +1,19 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors; + +import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesGetter; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientRequest; +import javax.annotation.Nullable; + +public class NacosClientCodeAttributesGetter implements CodeAttributesGetter { + @Nullable + @Override + public Class getCodeClass(NacosClientRequest nacosClientRequest) { + return nacosClientRequest.getDeclaringClass(); + } + + @Nullable + @Override + public String getMethodName(NacosClientRequest nacosClientRequest) { + return nacosClientRequest.getMethodName(); + } +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientExperimentalAttributeExtractor.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientExperimentalAttributeExtractor.java new file mode 100644 index 000000000000..9097b5391770 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientExperimentalAttributeExtractor.java @@ -0,0 +1,24 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors; + +import com.alibaba.nacos.api.remote.response.Response; +import io.opentelemetry.api.common.AttributesBuilder; +import io.opentelemetry.context.Context; +import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientRequest; +import javax.annotation.Nullable; + +public class NacosClientExperimentalAttributeExtractor + implements AttributesExtractor { + @Override + public void onStart(AttributesBuilder attributes, Context parentContext, + NacosClientRequest nacosClientRequest) { + attributes.putAll(nacosClientRequest.getAttributes()); + } + + @Override + public void onEnd(AttributesBuilder attributes, Context context, + NacosClientRequest nacosClientRequest, @Nullable Response response, + @Nullable Throwable error) { + + } +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanNameExtractor.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanNameExtractor.java new file mode 100644 index 000000000000..5a5f53fbe6e3 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanNameExtractor.java @@ -0,0 +1,12 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors; + +import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientRequest; + +public class NacosClientSpanNameExtractor implements SpanNameExtractor { + @Override + public String extract(NacosClientRequest nacosClientRequest) { + return nacosClientRequest.getSpanName(); + } + +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanStatusExtractor.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanStatusExtractor.java new file mode 100644 index 000000000000..07a0b44550bc --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanStatusExtractor.java @@ -0,0 +1,22 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors; + +import com.alibaba.nacos.api.remote.response.Response; +import io.opentelemetry.api.trace.StatusCode; +import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusBuilder; +import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientRequest; +import javax.annotation.Nullable; + +public class NacosClientSpanStatusExtractor implements + SpanStatusExtractor { + @Override + public void extract(SpanStatusBuilder spanStatusBuilder, NacosClientRequest nacosClientRequest, + @Nullable Response response, @Nullable Throwable error) { + if (response == null || !response.isSuccess()) { + spanStatusBuilder.setStatus(StatusCode.ERROR); + } else { + SpanStatusExtractor.getDefault() + .extract(spanStatusBuilder, nacosClientRequest, response, error); + } + } +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/GrpcConnectionInstrumentation.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/GrpcConnectionInstrumentation.java new file mode 100644 index 000000000000..64f48e4df4df --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/GrpcConnectionInstrumentation.java @@ -0,0 +1,33 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.instrumentations; + +import static net.bytebuddy.matcher.ElementMatchers.isMethod; +import static net.bytebuddy.matcher.ElementMatchers.isPublic; +import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; +import static net.bytebuddy.matcher.ElementMatchers.returns; + +import com.alibaba.nacos.api.remote.response.Response; +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.advices.GrpcConnectionRequestAdvice; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import net.bytebuddy.matcher.ElementMatchers; + +public class GrpcConnectionInstrumentation implements TypeInstrumentation { + @Override + public ElementMatcher typeMatcher() { + return ElementMatchers.named("com.alibaba.nacos.common.remote.client.grpc.GrpcConnection"); + } + + @Override + public void transform(TypeTransformer transformer) { + transformer.applyAdviceToMethod( + isMethod() + .and(isPublic()) + .and(namedOneOf("request")) + .and(returns(Response.class)), + GrpcConnectionRequestAdvice.class.getName() + ); + } + +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/RpcClientInstrumentation.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/RpcClientInstrumentation.java new file mode 100644 index 000000000000..d3a3d041eb03 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/RpcClientInstrumentation.java @@ -0,0 +1,33 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.instrumentations; + +import static net.bytebuddy.matcher.ElementMatchers.isMethod; +import static net.bytebuddy.matcher.ElementMatchers.isProtected; +import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; +import static net.bytebuddy.matcher.ElementMatchers.returns; + +import com.alibaba.nacos.api.remote.response.Response; +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.advices.RpcClientHandleServerRequestAdvice; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; +import net.bytebuddy.matcher.ElementMatchers; + +public class RpcClientInstrumentation implements TypeInstrumentation { + @Override + public ElementMatcher typeMatcher() { + return ElementMatchers.named("com.alibaba.nacos.common.remote.client.RpcClient"); + } + + @Override + public void transform(TypeTransformer transformer) { + transformer.applyAdviceToMethod( + isMethod() + .and(isProtected()) + .and(namedOneOf("handleServerRequest")) + .and(returns(Response.class)), + RpcClientHandleServerRequestAdvice.class.getName() + ); + } + +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java new file mode 100644 index 000000000000..e88c51d87722 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java @@ -0,0 +1,108 @@ +package com.alibaba.nacos.common.remote.client; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.api.remote.response.Response; +import com.alibaba.nacos.common.remote.ConnectionType; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientTestHelper; +import io.opentelemetry.sdk.trace.data.StatusData; +import java.util.Collections; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +public class RpcClientTest { + @RegisterExtension + private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); + private RpcClient rpcClient; + + @Mock + private ServerRequestHandler serverRequestHandler; + + private List nacosClientRequestList; + + @BeforeEach + public void setUp() { + nacosClientRequestList = NacosClientTestHelper.REQUEST_LIST; + rpcClient = new RpcClient("testRpcClient") { + @Override + public ConnectionType getConnectionType() { + return ConnectionType.GRPC; + } + + @Override + public int rpcPortOffset() { + return 0; + } + + @Override + public Connection connectToServer(ServerInfo serverInfo) throws Exception { + return null; + } + }; + rpcClient.serverRequestHandlers = Collections.singletonList(serverRequestHandler); + } + + + @Test + public void handleServerRequestSuccessResponse() { + when(serverRequestHandler.requestReply(any(Request.class))).thenReturn( + NacosClientTestHelper.SUCCESS_RESPONSE); + for (Request request : nacosClientRequestList) { + Response response = rpcClient.handleServerRequest(request); + assertNotNull(response); + assertTrue(response.isSuccess()); + testing.waitAndAssertTraces( + trace -> { + trace.hasSpansSatisfyingExactly( + span -> { + span.hasName(NacosClientTestHelper.NACOS_CLIENT_REQUEST_NAME_MAP.get(request)) + .hasKind(SpanKind.INTERNAL) + .hasStatus(StatusData.unset()) + .hasAttributesSatisfyingExactly( + NacosClientTestHelper.requestAttributeAssertions( + rpcClient.getClass().getName(), "handleServerRequest", request)); + }); + }); + testing.clearData(); + } + } + + @Test + public void handleServerRequestErrorResponse() { + when(serverRequestHandler.requestReply(any(Request.class))).thenReturn( + NacosClientTestHelper.ERROR_RESPONSE); + for (Request request : nacosClientRequestList) { + Response response = rpcClient.handleServerRequest(request); + assertNotNull(response); + assertFalse(response.isSuccess()); + testing.waitAndAssertTraces( + trace -> { + trace.hasSpansSatisfyingExactly( + span -> { + span.hasName(NacosClientTestHelper.NACOS_CLIENT_REQUEST_NAME_MAP.get(request)) + .hasKind(SpanKind.INTERNAL) + .hasStatus(StatusData.error()) + .hasAttributesSatisfyingExactly( + NacosClientTestHelper.requestAttributeAssertions( + rpcClient.getClass().getName(), "handleServerRequest", request)); + }); + }); + testing.clearData(); + } + } +} + diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java new file mode 100644 index 000000000000..6051952538a8 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java @@ -0,0 +1,136 @@ +package com.alibaba.nacos.common.remote.client.grpc; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.grpc.auto.Payload; +import com.alibaba.nacos.api.grpc.auto.RequestGrpc; +import com.alibaba.nacos.api.remote.PayloadRegistry; +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.api.remote.response.Response; +import com.alibaba.nacos.common.remote.client.RpcClient; +import com.alibaba.nacos.shaded.com.google.common.util.concurrent.ListenableFuture; +import com.alibaba.nacos.shaded.io.grpc.ManagedChannel; +import com.alibaba.nacos.shaded.io.grpc.stub.StreamObserver; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientTestHelper; +import io.opentelemetry.sdk.trace.data.StatusData; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +public class GrpcConnectionTest { + + @RegisterExtension + private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); + @Mock + private Executor executor; + + @Mock + private ManagedChannel channel; + + @Mock + private StreamObserver payloadStreamObserver; + + @Mock + RequestGrpc.RequestFutureStub grpcFutureServiceStub; + + @Mock + ListenableFuture future; + + Payload responsePayload; + + Payload errorResponsePayload; + + GrpcConnection connection; + + private List nacosClientRequestList; + + @BeforeAll + public static void setUpBeforeClass() { + PayloadRegistry.init(); + } + + @BeforeEach + public void setUp() throws Exception { + nacosClientRequestList = NacosClientTestHelper.REQUEST_LIST; + connection = new GrpcConnection(new RpcClient.ServerInfo(), executor); + connection.setChannel(channel); + connection.setPayloadStreamObserver(payloadStreamObserver); + connection.setGrpcFutureServiceStub(grpcFutureServiceStub); + when(grpcFutureServiceStub.request(any(Payload.class))).thenReturn(future); + responsePayload = GrpcUtils.convert(NacosClientTestHelper.SUCCESS_RESPONSE); + errorResponsePayload = GrpcUtils.convert(NacosClientTestHelper.ERROR_RESPONSE); + } + + @AfterEach + public void tearDown() { + connection.close(); + } + + @Test + public void requestSuccessResponse() + throws NacosException, ExecutionException, InterruptedException, TimeoutException { + when(future.get(-1, TimeUnit.MILLISECONDS)).thenReturn(responsePayload); + for (Request request : nacosClientRequestList) { + Response response = connection.request(request, -1); + assertNotNull(response); + assertTrue(response.isSuccess()); + testing.waitAndAssertTraces( + trace -> { + trace.hasSpansSatisfyingExactly( + span -> { + span.hasName(NacosClientTestHelper.NACOS_CLIENT_REQUEST_NAME_MAP.get(request)) + .hasKind(SpanKind.INTERNAL) + .hasStatus(StatusData.unset()) + .hasAttributesSatisfyingExactly( + NacosClientTestHelper.requestAttributeAssertions( + connection.getClass().getName(), "request", request)); + }); + }); + testing.clearData(); + } + } + + @Test + public void requestErrorResponse() + throws NacosException, ExecutionException, InterruptedException, TimeoutException { + when(future.get(-1, TimeUnit.MILLISECONDS)).thenReturn(errorResponsePayload); + for (Request request : nacosClientRequestList) { + Response response = connection.request(request, -1); + assertNotNull(response); + assertFalse(response.isSuccess()); + testing.waitAndAssertTraces( + trace -> { + trace.hasSpansSatisfyingExactly( + span -> { + span.hasName(NacosClientTestHelper.NACOS_CLIENT_REQUEST_NAME_MAP.get(request)) + .hasKind(SpanKind.INTERNAL) + .hasStatus(StatusData.error()) + .hasAttributesSatisfyingExactly( + NacosClientTestHelper.requestAttributeAssertions( + connection.getClass().getName(), "request", request)); + }); + }); + testing.clearData(); + } + } + +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java new file mode 100644 index 000000000000..5f2a4fc93113 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java @@ -0,0 +1,15 @@ +package com.alibaba.nacos.shaded.com.google.errorprone.annotations; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target(TYPE) +@Retention(RUNTIME) +@Documented +public @interface DoNotMock { + String value(); +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientTestHelper.java b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientTestHelper.java new file mode 100644 index 000000000000..cb96ac4e5533 --- /dev/null +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientTestHelper.java @@ -0,0 +1,254 @@ +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; + +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static java.util.Arrays.asList; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.alibaba.nacos.api.config.remote.request.ConfigChangeNotifyRequest; +import com.alibaba.nacos.api.config.remote.request.ConfigPublishRequest; +import com.alibaba.nacos.api.config.remote.request.ConfigQueryRequest; +import com.alibaba.nacos.api.config.remote.request.ConfigRemoveRequest; +import com.alibaba.nacos.api.naming.remote.request.InstanceRequest; +import com.alibaba.nacos.api.naming.remote.request.NotifySubscriberRequest; +import com.alibaba.nacos.api.naming.remote.request.ServiceListRequest; +import com.alibaba.nacos.api.naming.remote.request.ServiceQueryRequest; +import com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest; +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.api.remote.response.HealthCheckResponse; +import com.alibaba.nacos.api.remote.response.Response; +import com.alibaba.nacos.api.remote.response.ResponseCode; +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.sdk.testing.assertj.AttributeAssertion; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class NacosClientTestHelper { + public static final List RPC_CLIENT_HANDLE_SERVER_REQUEST_REQUEST_LIST = new ArrayList<>(); + public static final List GRPC_CONNECTION_REQUEST_LIST = new ArrayList<>(); + public static final List REQUEST_LIST = new ArrayList<>(); + public static final Map NACOS_CLIENT_REQUEST_MAP = new HashMap<>(); + public static final Map NACOS_CLIENT_REQUEST_NAME_MAP = new HashMap<>(); + + public static final String NAMESPACE = "namespace"; + public static final String GROUP_NAME = "groupName"; + public static final String SERVICE_NAME = "serviceName"; + public static final String DATA_ID = "dataId"; + public static final String GROUP = "group"; + public static final String TENANT = "tenant"; + public static final String INSTANCE_REQUEST_TYPE = "instanceRequestType"; + public static final ConfigChangeNotifyRequest CONFIG_CHANGE_NOTIFY_REQUEST; + public static final ConfigPublishRequest CONFIG_PUBLISH_REQUEST; + public static final ConfigQueryRequest CONFIG_QUERY_REQUEST; + public static final ConfigRemoveRequest CONFIG_REMOVE_REQUEST; + public static final InstanceRequest INSTANCE_REQUEST; + public static final NotifySubscriberRequest NOTIFY_SUBSCRIBER_REQUEST; + public static final ServiceListRequest SERVICE_LIST_REQUEST; + public static final ServiceQueryRequest SERVICE_QUERY_REQUEST; + public static final SubscribeServiceRequest SUBSCRIBE_SERVICE_REQUEST; + public static final SubscribeServiceRequest UN_SUBSCRIBE_SERVICE_REQUEST; + + public static final Response SUCCESS_RESPONSE; + public static final Response ERROR_RESPONSE; + public static final Response NULL_RESPONSE; + + public static final String NACOS_PREFIX = "Nacos/"; + + public static final String QUERY_SERVICE = "queryService"; + + public static final String SUBSCRIBE_SERVICE = "subscribeService"; + + public static final String UNSUBSCRIBE_SERVICE = "unsubscribeService"; + + public static final String QUERY_CONFIG = "queryConfig"; + + public static final String PUBLISH_CONFIG = "publishConfig"; + + public static final String REMOVE_CONFIG = "removeConfig"; + + public static final String GET_SERVICE_LIST = "getServiceList"; + + public static final String NOTIFY_SUBSCRIBE_CHANGE = "notifySubscribeChange"; + + public static final String NOTIFY_CONFIG_CHANGE = "notifyConfigChange"; + + public static final AttributeKey NACOS_NAME_SPACE_ATTR = AttributeKey.stringKey( + "nacos.namespace"); + + public static final AttributeKey NACOS_GROUP_NAME_ATTR = AttributeKey.stringKey("nacos.group.name"); + + public static final AttributeKey NACOS_SERVICE_NAME_ATTR = AttributeKey.stringKey( + "nacos.service.name"); + + public static final AttributeKey NACOS_DATA_ID_ATTR = AttributeKey.stringKey( + "nacos.data.id"); + + public static final AttributeKey NACOS_GROUP_ATTR = AttributeKey.stringKey("nacos.group"); + + public static final AttributeKey NACOS_TENANT_ATTR = AttributeKey.stringKey( + "nacos.tenant"); + + private NacosClientTestHelper() {} + + static { + SUCCESS_RESPONSE = new HealthCheckResponse(); + HealthCheckResponse errorResponse = new HealthCheckResponse(); + errorResponse.setResultCode(ResponseCode.FAIL.getCode()); + ERROR_RESPONSE = errorResponse; + NULL_RESPONSE = null; + + INSTANCE_REQUEST = mock(InstanceRequest.class); + when(INSTANCE_REQUEST.getType()).thenReturn(INSTANCE_REQUEST_TYPE); + when(INSTANCE_REQUEST.getNamespace()).thenReturn(NAMESPACE); + when(INSTANCE_REQUEST.getGroupName()).thenReturn(GROUP_NAME); + when(INSTANCE_REQUEST.getServiceName()).thenReturn(SERVICE_NAME); + + SERVICE_QUERY_REQUEST = mock(ServiceQueryRequest.class); + when(SERVICE_QUERY_REQUEST.getNamespace()).thenReturn(NAMESPACE); + when(SERVICE_QUERY_REQUEST.getGroupName()).thenReturn(GROUP_NAME); + when(SERVICE_QUERY_REQUEST.getServiceName()).thenReturn(SERVICE_NAME); + + SUBSCRIBE_SERVICE_REQUEST = mock(SubscribeServiceRequest.class); + when(SUBSCRIBE_SERVICE_REQUEST.isSubscribe()).thenReturn(true); + when(SUBSCRIBE_SERVICE_REQUEST.getNamespace()).thenReturn(NAMESPACE); + when(SUBSCRIBE_SERVICE_REQUEST.getGroupName()).thenReturn(GROUP_NAME); + when(SUBSCRIBE_SERVICE_REQUEST.getServiceName()).thenReturn(SERVICE_NAME); + + UN_SUBSCRIBE_SERVICE_REQUEST = mock(SubscribeServiceRequest.class); + when(UN_SUBSCRIBE_SERVICE_REQUEST.isSubscribe()).thenReturn(false); + when(UN_SUBSCRIBE_SERVICE_REQUEST.getNamespace()).thenReturn(NAMESPACE); + when(UN_SUBSCRIBE_SERVICE_REQUEST.getGroupName()).thenReturn(GROUP_NAME); + when(UN_SUBSCRIBE_SERVICE_REQUEST.getServiceName()).thenReturn(SERVICE_NAME); + + SERVICE_LIST_REQUEST = mock(ServiceListRequest.class); + when(SERVICE_LIST_REQUEST.getNamespace()).thenReturn(NAMESPACE); + when(SERVICE_LIST_REQUEST.getGroupName()).thenReturn(GROUP_NAME); + when(SERVICE_LIST_REQUEST.getServiceName()).thenReturn(SERVICE_NAME); + + CONFIG_QUERY_REQUEST = mock(ConfigQueryRequest.class); + when(CONFIG_QUERY_REQUEST.getDataId()).thenReturn(DATA_ID); + when(CONFIG_QUERY_REQUEST.getGroup()).thenReturn(GROUP); + when(CONFIG_QUERY_REQUEST.getTenant()).thenReturn(TENANT); + + CONFIG_PUBLISH_REQUEST = mock(ConfigPublishRequest.class); + when(CONFIG_PUBLISH_REQUEST.getDataId()).thenReturn(DATA_ID); + when(CONFIG_PUBLISH_REQUEST.getGroup()).thenReturn(GROUP); + when(CONFIG_PUBLISH_REQUEST.getTenant()).thenReturn(TENANT); + + CONFIG_REMOVE_REQUEST = mock(ConfigRemoveRequest.class); + when(CONFIG_REMOVE_REQUEST.getDataId()).thenReturn(DATA_ID); + when(CONFIG_REMOVE_REQUEST.getGroup()).thenReturn(GROUP); + when(CONFIG_REMOVE_REQUEST.getTenant()).thenReturn(TENANT); + + NOTIFY_SUBSCRIBER_REQUEST = mock(NotifySubscriberRequest.class); + when(NOTIFY_SUBSCRIBER_REQUEST.getNamespace()).thenReturn(NAMESPACE); + when(NOTIFY_SUBSCRIBER_REQUEST.getGroupName()).thenReturn(GROUP_NAME); + when(NOTIFY_SUBSCRIBER_REQUEST.getServiceName()).thenReturn(SERVICE_NAME); + + CONFIG_CHANGE_NOTIFY_REQUEST = mock(ConfigChangeNotifyRequest.class); + when(CONFIG_CHANGE_NOTIFY_REQUEST.getDataId()).thenReturn(DATA_ID); + when(CONFIG_CHANGE_NOTIFY_REQUEST.getGroup()).thenReturn(GROUP); + when(CONFIG_CHANGE_NOTIFY_REQUEST.getTenant()).thenReturn(TENANT); + + NACOS_CLIENT_REQUEST_MAP.put( + NACOS_PREFIX + NOTIFY_CONFIG_CHANGE, + CONFIG_CHANGE_NOTIFY_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put( + NACOS_PREFIX + PUBLISH_CONFIG, + CONFIG_PUBLISH_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put( + NACOS_PREFIX + QUERY_CONFIG, + CONFIG_QUERY_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put( + NACOS_PREFIX + REMOVE_CONFIG, + CONFIG_REMOVE_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put(NACOS_PREFIX + INSTANCE_REQUEST_TYPE, + INSTANCE_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put( + NACOS_PREFIX + NOTIFY_SUBSCRIBE_CHANGE, + NOTIFY_SUBSCRIBER_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put( + NACOS_PREFIX + GET_SERVICE_LIST, + SERVICE_LIST_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put( + NACOS_PREFIX + QUERY_SERVICE, + SERVICE_QUERY_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put( + NACOS_PREFIX + SUBSCRIBE_SERVICE, + SUBSCRIBE_SERVICE_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put( + NACOS_PREFIX + UNSUBSCRIBE_SERVICE, + UN_SUBSCRIBE_SERVICE_REQUEST); + + for (Request value : NACOS_CLIENT_REQUEST_MAP.values()) { + REQUEST_LIST.add(value); + if (value instanceof NotifySubscriberRequest || value instanceof ConfigChangeNotifyRequest) { + RPC_CLIENT_HANDLE_SERVER_REQUEST_REQUEST_LIST.add(value); + } else { + GRPC_CONNECTION_REQUEST_LIST.add(value); + } + } + NACOS_CLIENT_REQUEST_NAME_MAP.putAll(NACOS_CLIENT_REQUEST_MAP.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey))); + } + + private static final String[] methodNames = { + "getNamespace", + "getGroupName", + "getServiceName", + "getDataId", + "getGroup", + "getTenant" + }; + + private static final Map> methodToAttributeMap = new HashMap<>(); + + static { + methodToAttributeMap.put("getNamespace", NACOS_NAME_SPACE_ATTR); + methodToAttributeMap.put("getGroupName", NACOS_GROUP_NAME_ATTR); + methodToAttributeMap.put("getServiceName", NACOS_SERVICE_NAME_ATTR); + methodToAttributeMap.put("getDataId", NACOS_DATA_ID_ATTR); + methodToAttributeMap.put("getGroup", NACOS_GROUP_ATTR); + methodToAttributeMap.put("getTenant", NACOS_TENANT_ATTR); + } + + + public static List requestAttributeAssertions ( + String codeNamespace, String codeFunction, Request request) { + List attributeAssertions = + new ArrayList<>(asList( + equalTo(AttributeKey.stringKey("code.namespace"), codeNamespace), + equalTo(AttributeKey.stringKey("code.function"), codeFunction), + equalTo(AttributeKey.stringKey("service.discovery.system"), "nacos")) + ); + + for (String methodName : methodNames) { + Method method = null; + try { + method = request.getClass().getMethod(methodName); + } catch (NoSuchMethodException e) { + continue; + } + if (method != null) { + method.setAccessible(true); + Object result = null; + try { + result = method.invoke(request); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } + attributeAssertions.add( + equalTo(methodToAttributeMap.get(methodName), String.valueOf(result))); + } + } + + return attributeAssertions; + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index f3a17f03591a..51da2c23c312 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -391,6 +391,7 @@ include(":instrumentation:mongo:mongo-4.0:javaagent") include(":instrumentation:mongo:mongo-async-3.3:javaagent") include(":instrumentation:mongo:mongo-common:testing") include(":instrumentation:mybatis-3.2:javaagent") +include(":instrumentation:nacos-client-2.0.3:javaagent") include(":instrumentation:netty:netty-3.8:javaagent") include(":instrumentation:netty:netty-4.0:javaagent") include(":instrumentation:netty:netty-4.1:javaagent") From 016e9d649dd5c18aafe4efaf5189a30fedb4bf3c Mon Sep 17 00:00:00 2001 From: huangweilong <13622993145@163.com> Date: Sun, 8 Dec 2024 14:50:20 +0800 Subject: [PATCH 2/5] refactor(nacos-client): Improve code formatting and muzzle validation - Improved code formatting - Enhanced muzzle validation - Managed dependency versions --- .../javaagent/build.gradle.kts | 5 +- .../client/v2_0_3/NacosClientConstants.java | 28 +- .../client/v2_0_3/NacosClientHelper.java | 257 ++++++++++-------- .../NacosClientInstrumentationModule.java | 14 +- .../client/v2_0_3/NacosClientRequest.java | 14 +- .../v2_0_3/NacosClientRequestOperator.java | 9 +- .../client/v2_0_3/NacosClientSingletons.java | 14 +- .../advices/GrpcConnectionRequestAdvice.java | 9 +- .../RpcClientHandleServerRequestAdvice.java | 9 +- .../NacosClientCodeAttributesGetter.java | 5 + ...sClientExperimentalAttributeExtractor.java | 20 +- .../NacosClientSpanNameExtractor.java | 6 +- .../NacosClientSpanStatusExtractor.java | 16 +- .../GrpcConnectionInstrumentation.java | 14 +- .../RpcClientInstrumentation.java | 9 +- .../common/remote/client/RpcClientTest.java | 48 ++-- .../client/grpc/GrpcConnectionTest.java | 24 +- .../errorprone/annotations/DoNotMock.java | 11 +- .../client/v2_0_3/NacosClientTestHelper.java | 91 +++---- 19 files changed, 338 insertions(+), 265 deletions(-) diff --git a/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts b/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts index c45832ba5e14..fae790405484 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts +++ b/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts @@ -7,16 +7,17 @@ muzzle { group.set("com.alibaba.nacos") module.set("nacos-client") versions.set("[2.0.3,)") + skip("0.5.0","1.1.2", "1.1.4", "1.4.7", "2.0.1","2.0.2") assertInverse.set(true) } } dependencies { - implementation("com.alibaba.nacos:nacos-client:2.0.3") + val nacosClientVersion = "2.0.3" + implementation("com.alibaba.nacos:nacos-client:$nacosClientVersion") testImplementation("javax.annotation:javax.annotation-api:1.3.2") } - tasks.withType().configureEach { jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED") jvmArgs("-XX:+IgnoreUnrecognizedVMOptions") diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientConstants.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientConstants.java index 290237cf3533..ee8feebe0d52 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientConstants.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientConstants.java @@ -1,10 +1,14 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; import io.opentelemetry.api.common.AttributeKey; public class NacosClientConstants { - private NacosClientConstants() { - } + private NacosClientConstants() {} public static final String NACOS_PREFIX = "Nacos/"; @@ -28,20 +32,20 @@ private NacosClientConstants() { public static final String NOTIFY_CONFIG_CHANGE = "notifyConfigChange"; - public static final AttributeKey NACOS_NAME_SPACE_ATTR = AttributeKey.stringKey( - "nacos.namespace"); + public static final AttributeKey NACOS_NAME_SPACE_ATTR = + AttributeKey.stringKey("nacos.namespace"); - public static final AttributeKey NACOS_GROUP_NAME_ATTR = AttributeKey.stringKey("nacos.group.name"); + public static final AttributeKey NACOS_GROUP_NAME_ATTR = + AttributeKey.stringKey("nacos.group.name"); - public static final AttributeKey NACOS_SERVICE_NAME_ATTR = AttributeKey.stringKey( - "nacos.service.name"); + public static final AttributeKey NACOS_SERVICE_NAME_ATTR = + AttributeKey.stringKey("nacos.service.name"); - public static final AttributeKey NACOS_DATA_ID_ATTR = AttributeKey.stringKey( - "nacos.data.id"); + public static final AttributeKey NACOS_DATA_ID_ATTR = + AttributeKey.stringKey("nacos.data.id"); public static final AttributeKey NACOS_GROUP_ATTR = AttributeKey.stringKey("nacos.group"); - public static final AttributeKey NACOS_TENANT_ATTR = AttributeKey.stringKey( - "nacos.tenant"); - + public static final AttributeKey NACOS_TENANT_ATTR = + AttributeKey.stringKey("nacos.tenant"); } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientHelper.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientHelper.java index 4d1525f8adc0..83d3b62688a1 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientHelper.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientHelper.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; import com.alibaba.nacos.api.config.remote.request.ConfigChangeNotifyRequest; @@ -15,142 +20,160 @@ import javax.annotation.Nonnull; public class NacosClientHelper { - private static final NacosClientRequestOperator UNKNOWN_OPERATOR = new NacosClientRequestOperator( - request -> request.getClass().getSimpleName(), null); - private static final Map, NacosClientRequestOperator> KNOWN_OPERATOR_MAP = new HashMap<>(); + private static final NacosClientRequestOperator UNKNOWN_OPERATOR = + new NacosClientRequestOperator(request -> request.getClass().getSimpleName(), null); + private static final Map, NacosClientRequestOperator> + KNOWN_OPERATOR_MAP = new HashMap<>(); private NacosClientHelper() {} static { - KNOWN_OPERATOR_MAP.put(InstanceRequest.class, new NacosClientRequestOperator( - request -> ((InstanceRequest) request).getType(), - (attributesBuilder, request) -> { - InstanceRequest instanceRequest = (InstanceRequest) request; - attributesBuilder.put(NacosClientConstants.NACOS_NAME_SPACE_ATTR, - instanceRequest.getNamespace()); - attributesBuilder.put(NacosClientConstants.NACOS_GROUP_NAME_ATTR, - instanceRequest.getGroupName()); - attributesBuilder.put(NacosClientConstants.NACOS_SERVICE_NAME_ATTR, - instanceRequest.getServiceName()); - }) - ); + KNOWN_OPERATOR_MAP.put( + InstanceRequest.class, + new NacosClientRequestOperator( + request -> ((InstanceRequest) request).getType(), + (attributesBuilder, request) -> { + InstanceRequest instanceRequest = (InstanceRequest) request; + attributesBuilder.put( + NacosClientConstants.NACOS_NAME_SPACE_ATTR, instanceRequest.getNamespace()); + attributesBuilder.put( + NacosClientConstants.NACOS_GROUP_NAME_ATTR, instanceRequest.getGroupName()); + attributesBuilder.put( + NacosClientConstants.NACOS_SERVICE_NAME_ATTR, instanceRequest.getServiceName()); + })); - KNOWN_OPERATOR_MAP.put(ServiceQueryRequest.class, new NacosClientRequestOperator( - request -> NacosClientConstants.QUERY_SERVICE, - (attributesBuilder, request) -> { - ServiceQueryRequest serviceQueryRequest = (ServiceQueryRequest) request; - attributesBuilder.put(NacosClientConstants.NACOS_NAME_SPACE_ATTR, - serviceQueryRequest.getNamespace()); - attributesBuilder.put(NacosClientConstants.NACOS_GROUP_NAME_ATTR, - serviceQueryRequest.getGroupName()); - attributesBuilder.put(NacosClientConstants.NACOS_SERVICE_NAME_ATTR, - serviceQueryRequest.getServiceName()); - }) - ); + KNOWN_OPERATOR_MAP.put( + ServiceQueryRequest.class, + new NacosClientRequestOperator( + request -> NacosClientConstants.QUERY_SERVICE, + (attributesBuilder, request) -> { + ServiceQueryRequest serviceQueryRequest = (ServiceQueryRequest) request; + attributesBuilder.put( + NacosClientConstants.NACOS_NAME_SPACE_ATTR, serviceQueryRequest.getNamespace()); + attributesBuilder.put( + NacosClientConstants.NACOS_GROUP_NAME_ATTR, serviceQueryRequest.getGroupName()); + attributesBuilder.put( + NacosClientConstants.NACOS_SERVICE_NAME_ATTR, + serviceQueryRequest.getServiceName()); + })); - KNOWN_OPERATOR_MAP.put(SubscribeServiceRequest.class, new NacosClientRequestOperator( - request -> ((SubscribeServiceRequest) request).isSubscribe() - ? NacosClientConstants.SUBSCRIBE_SERVICE - : NacosClientConstants.UNSUBSCRIBE_SERVICE, - (attributesBuilder, request) -> { - SubscribeServiceRequest subscribeServiceRequest = (SubscribeServiceRequest) request; - attributesBuilder.put(NacosClientConstants.NACOS_NAME_SPACE_ATTR, - subscribeServiceRequest.getNamespace()); - attributesBuilder.put(NacosClientConstants.NACOS_GROUP_NAME_ATTR, - subscribeServiceRequest.getGroupName()); - attributesBuilder.put(NacosClientConstants.NACOS_SERVICE_NAME_ATTR, - subscribeServiceRequest.getServiceName()); - }) - ); + KNOWN_OPERATOR_MAP.put( + SubscribeServiceRequest.class, + new NacosClientRequestOperator( + request -> + ((SubscribeServiceRequest) request).isSubscribe() + ? NacosClientConstants.SUBSCRIBE_SERVICE + : NacosClientConstants.UNSUBSCRIBE_SERVICE, + (attributesBuilder, request) -> { + SubscribeServiceRequest subscribeServiceRequest = (SubscribeServiceRequest) request; + attributesBuilder.put( + NacosClientConstants.NACOS_NAME_SPACE_ATTR, + subscribeServiceRequest.getNamespace()); + attributesBuilder.put( + NacosClientConstants.NACOS_GROUP_NAME_ATTR, + subscribeServiceRequest.getGroupName()); + attributesBuilder.put( + NacosClientConstants.NACOS_SERVICE_NAME_ATTR, + subscribeServiceRequest.getServiceName()); + })); - KNOWN_OPERATOR_MAP.put(ServiceListRequest.class, new NacosClientRequestOperator( - request -> NacosClientConstants.GET_SERVICE_LIST, - (attributesBuilder, request) -> { - ServiceListRequest serviceListRequest = (ServiceListRequest) request; - attributesBuilder.put(NacosClientConstants.NACOS_NAME_SPACE_ATTR, - serviceListRequest.getNamespace()); - attributesBuilder.put(NacosClientConstants.NACOS_GROUP_NAME_ATTR, - serviceListRequest.getGroupName()); - attributesBuilder.put(NacosClientConstants.NACOS_SERVICE_NAME_ATTR, - serviceListRequest.getServiceName()); - }) - ); + KNOWN_OPERATOR_MAP.put( + ServiceListRequest.class, + new NacosClientRequestOperator( + request -> NacosClientConstants.GET_SERVICE_LIST, + (attributesBuilder, request) -> { + ServiceListRequest serviceListRequest = (ServiceListRequest) request; + attributesBuilder.put( + NacosClientConstants.NACOS_NAME_SPACE_ATTR, serviceListRequest.getNamespace()); + attributesBuilder.put( + NacosClientConstants.NACOS_GROUP_NAME_ATTR, serviceListRequest.getGroupName()); + attributesBuilder.put( + NacosClientConstants.NACOS_SERVICE_NAME_ATTR, + serviceListRequest.getServiceName()); + })); - KNOWN_OPERATOR_MAP.put(ConfigQueryRequest.class, new NacosClientRequestOperator( - request -> NacosClientConstants.QUERY_CONFIG, - (attributesBuilder, request) -> { - ConfigQueryRequest configQueryRequest = (ConfigQueryRequest) request; - attributesBuilder.put(NacosClientConstants.NACOS_DATA_ID_ATTR, - configQueryRequest.getDataId()); - attributesBuilder.put(NacosClientConstants.NACOS_GROUP_ATTR, - configQueryRequest.getGroup()); - attributesBuilder.put(NacosClientConstants.NACOS_TENANT_ATTR, - configQueryRequest.getTenant()); - }) - ); + KNOWN_OPERATOR_MAP.put( + ConfigQueryRequest.class, + new NacosClientRequestOperator( + request -> NacosClientConstants.QUERY_CONFIG, + (attributesBuilder, request) -> { + ConfigQueryRequest configQueryRequest = (ConfigQueryRequest) request; + attributesBuilder.put( + NacosClientConstants.NACOS_DATA_ID_ATTR, configQueryRequest.getDataId()); + attributesBuilder.put( + NacosClientConstants.NACOS_GROUP_ATTR, configQueryRequest.getGroup()); + attributesBuilder.put( + NacosClientConstants.NACOS_TENANT_ATTR, configQueryRequest.getTenant()); + })); - KNOWN_OPERATOR_MAP.put(ConfigPublishRequest.class, new NacosClientRequestOperator( - request -> NacosClientConstants.PUBLISH_CONFIG, - (attributesBuilder, request) -> { - ConfigPublishRequest configPublishRequest = (ConfigPublishRequest) request; - attributesBuilder.put(NacosClientConstants.NACOS_DATA_ID_ATTR, - configPublishRequest.getDataId()); - attributesBuilder.put(NacosClientConstants.NACOS_GROUP_ATTR, - configPublishRequest.getGroup()); - attributesBuilder.put(NacosClientConstants.NACOS_TENANT_ATTR, - configPublishRequest.getTenant()); - }) - ); + KNOWN_OPERATOR_MAP.put( + ConfigPublishRequest.class, + new NacosClientRequestOperator( + request -> NacosClientConstants.PUBLISH_CONFIG, + (attributesBuilder, request) -> { + ConfigPublishRequest configPublishRequest = (ConfigPublishRequest) request; + attributesBuilder.put( + NacosClientConstants.NACOS_DATA_ID_ATTR, configPublishRequest.getDataId()); + attributesBuilder.put( + NacosClientConstants.NACOS_GROUP_ATTR, configPublishRequest.getGroup()); + attributesBuilder.put( + NacosClientConstants.NACOS_TENANT_ATTR, configPublishRequest.getTenant()); + })); - KNOWN_OPERATOR_MAP.put(ConfigRemoveRequest.class, new NacosClientRequestOperator( - request -> NacosClientConstants.REMOVE_CONFIG, - (attributesBuilder, request) -> { - ConfigRemoveRequest configRemoveRequest = (ConfigRemoveRequest) request; - attributesBuilder.put(NacosClientConstants.NACOS_DATA_ID_ATTR, - configRemoveRequest.getDataId()); - attributesBuilder.put(NacosClientConstants.NACOS_GROUP_ATTR, - configRemoveRequest.getGroup()); - attributesBuilder.put(NacosClientConstants.NACOS_TENANT_ATTR, - configRemoveRequest.getTenant()); - }) - ); + KNOWN_OPERATOR_MAP.put( + ConfigRemoveRequest.class, + new NacosClientRequestOperator( + request -> NacosClientConstants.REMOVE_CONFIG, + (attributesBuilder, request) -> { + ConfigRemoveRequest configRemoveRequest = (ConfigRemoveRequest) request; + attributesBuilder.put( + NacosClientConstants.NACOS_DATA_ID_ATTR, configRemoveRequest.getDataId()); + attributesBuilder.put( + NacosClientConstants.NACOS_GROUP_ATTR, configRemoveRequest.getGroup()); + attributesBuilder.put( + NacosClientConstants.NACOS_TENANT_ATTR, configRemoveRequest.getTenant()); + })); - KNOWN_OPERATOR_MAP.put(NotifySubscriberRequest.class, new NacosClientRequestOperator( - request -> NacosClientConstants.NOTIFY_SUBSCRIBE_CHANGE, - (attributesBuilder, request) -> { - NotifySubscriberRequest notifySubscriberRequest = (NotifySubscriberRequest) request; - attributesBuilder.put(NacosClientConstants.NACOS_NAME_SPACE_ATTR, - notifySubscriberRequest.getNamespace()); - attributesBuilder.put(NacosClientConstants.NACOS_GROUP_NAME_ATTR, - notifySubscriberRequest.getGroupName()); - attributesBuilder.put(NacosClientConstants.NACOS_SERVICE_NAME_ATTR, - notifySubscriberRequest.getServiceName()); - }) - ); + KNOWN_OPERATOR_MAP.put( + NotifySubscriberRequest.class, + new NacosClientRequestOperator( + request -> NacosClientConstants.NOTIFY_SUBSCRIBE_CHANGE, + (attributesBuilder, request) -> { + NotifySubscriberRequest notifySubscriberRequest = (NotifySubscriberRequest) request; + attributesBuilder.put( + NacosClientConstants.NACOS_NAME_SPACE_ATTR, + notifySubscriberRequest.getNamespace()); + attributesBuilder.put( + NacosClientConstants.NACOS_GROUP_NAME_ATTR, + notifySubscriberRequest.getGroupName()); + attributesBuilder.put( + NacosClientConstants.NACOS_SERVICE_NAME_ATTR, + notifySubscriberRequest.getServiceName()); + })); - KNOWN_OPERATOR_MAP.put(ConfigChangeNotifyRequest.class, new NacosClientRequestOperator( - request -> NacosClientConstants.NOTIFY_CONFIG_CHANGE, - (attributesBuilder, request) -> { - ConfigChangeNotifyRequest configChangeNotifyRequest = (ConfigChangeNotifyRequest) request; - attributesBuilder.put(NacosClientConstants.NACOS_DATA_ID_ATTR, - configChangeNotifyRequest.getDataId()); - attributesBuilder.put(NacosClientConstants.NACOS_GROUP_ATTR, - configChangeNotifyRequest.getGroup()); - attributesBuilder.put(NacosClientConstants.NACOS_TENANT_ATTR, - configChangeNotifyRequest.getTenant()); - }) - ); + KNOWN_OPERATOR_MAP.put( + ConfigChangeNotifyRequest.class, + new NacosClientRequestOperator( + request -> NacosClientConstants.NOTIFY_CONFIG_CHANGE, + (attributesBuilder, request) -> { + ConfigChangeNotifyRequest configChangeNotifyRequest = + (ConfigChangeNotifyRequest) request; + attributesBuilder.put( + NacosClientConstants.NACOS_DATA_ID_ATTR, configChangeNotifyRequest.getDataId()); + attributesBuilder.put( + NacosClientConstants.NACOS_GROUP_ATTR, configChangeNotifyRequest.getGroup()); + attributesBuilder.put( + NacosClientConstants.NACOS_TENANT_ATTR, configChangeNotifyRequest.getTenant()); + })); } @Nonnull public static NacosClientRequestOperator getOperator(@Nonnull Request request) { - NacosClientRequestOperator nacosClientRequestOperator = KNOWN_OPERATOR_MAP.get( - request.getClass()); + NacosClientRequestOperator nacosClientRequestOperator = + KNOWN_OPERATOR_MAP.get(request.getClass()); if (nacosClientRequestOperator != null) { return nacosClientRequestOperator; } return UNKNOWN_OPERATOR; } - - } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientInstrumentationModule.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientInstrumentationModule.java index 48009a6287fb..42f5a26a68fa 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientInstrumentationModule.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientInstrumentationModule.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; import com.google.auto.service.AutoService; @@ -10,17 +15,14 @@ import java.util.List; @AutoService(InstrumentationModule.class) -public class NacosClientInstrumentationModule extends InstrumentationModule implements - ExperimentalInstrumentationModule { +public class NacosClientInstrumentationModule extends InstrumentationModule + implements ExperimentalInstrumentationModule { public NacosClientInstrumentationModule() { super("nacos-client", "nacos-client-2.0.3"); } @Override public List typeInstrumentations() { - return Arrays.asList( - new GrpcConnectionInstrumentation(), - new RpcClientInstrumentation() - ); + return Arrays.asList(new GrpcConnectionInstrumentation(), new RpcClientInstrumentation()); } } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequest.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequest.java index ffca33c293d7..a5258ac799fc 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequest.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequest.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; import com.alibaba.nacos.api.remote.request.Request; @@ -10,8 +15,8 @@ public final class NacosClientRequest { private final String spanName; private final Attributes attributes; - public NacosClientRequest(String methodName, Class declaringClass, String spanName, - Attributes attributes) { + public NacosClientRequest( + String methodName, Class declaringClass, String spanName, Attributes attributes) { this.methodName = methodName; this.declaringClass = declaringClass; this.spanName = spanName; @@ -19,8 +24,8 @@ public NacosClientRequest(String methodName, Class declaringClass, String spa } @Nonnull - public static NacosClientRequest createRequest(@Nonnull String methodName, - @Nonnull Class declaringClass, @Nonnull Request request) { + public static NacosClientRequest createRequest( + @Nonnull String methodName, @Nonnull Class declaringClass, @Nonnull Request request) { NacosClientRequestOperator operator = NacosClientHelper.getOperator(request); String spanName = operator.getName(request); Attributes attributes = operator.getAttributes(request); @@ -42,5 +47,4 @@ public String getSpanName() { public Attributes getAttributes() { return attributes; } - } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequestOperator.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequestOperator.java index 0d6a83387daf..2c945574d1aa 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequestOperator.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequestOperator.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; import com.alibaba.nacos.api.remote.request.Request; @@ -18,7 +23,8 @@ public NacosClientRequestOperator( } public String getName(Request request) { - return spanNameHandler == null ? "" + return spanNameHandler == null + ? "" : NacosClientConstants.NACOS_PREFIX + spanNameHandler.apply(request); } @@ -29,5 +35,4 @@ public Attributes getAttributes(Request request) { } return builder.build(); } - } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientSingletons.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientSingletons.java index 1dcbd0ffb7bd..6f5d6e5cc372 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientSingletons.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientSingletons.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; import com.alibaba.nacos.api.remote.response.Response; @@ -24,17 +29,18 @@ public static Instrumenter instrumenter() { } private static Instrumenter create() { - CodeAttributesGetter codeAttributesGetter = new NacosClientCodeAttributesGetter(); + CodeAttributesGetter codeAttributesGetter = + new NacosClientCodeAttributesGetter(); SpanNameExtractor spanNameExtractor = new NacosClientSpanNameExtractor(); - SpanStatusExtractor spanStatusExtractor = new NacosClientSpanStatusExtractor(); + SpanStatusExtractor spanStatusExtractor = + new NacosClientSpanStatusExtractor(); InstrumenterBuilder builder = Instrumenter.builder( GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, spanNameExtractor) .addAttributesExtractor(CodeAttributesExtractor.create(codeAttributesGetter)) .setSpanStatusExtractor(spanStatusExtractor); builder.addAttributesExtractor( - AttributesExtractor.constant(AttributeKey.stringKey("service.discovery.system"), "nacos") - ); + AttributesExtractor.constant(AttributeKey.stringKey("service.discovery.system"), "nacos")); builder.addAttributesExtractor(new NacosClientExperimentalAttributeExtractor()); return builder.buildInstrumenter(); } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/GrpcConnectionRequestAdvice.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/GrpcConnectionRequestAdvice.java index f4aaf3546321..e00f53fd4be2 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/GrpcConnectionRequestAdvice.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/GrpcConnectionRequestAdvice.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.advices; import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; @@ -20,8 +25,8 @@ public static void requestEnter( @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - nacosClientRequest = NacosClientRequest.createRequest("request", thisObject.getClass(), - request); + nacosClientRequest = + NacosClientRequest.createRequest("request", thisObject.getClass(), request); if (!instrumenter().shouldStart(parentContext, nacosClientRequest)) { return; } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/RpcClientHandleServerRequestAdvice.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/RpcClientHandleServerRequestAdvice.java index c21d4cfa6226..72fafe5783ec 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/RpcClientHandleServerRequestAdvice.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/RpcClientHandleServerRequestAdvice.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.advices; import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; @@ -20,8 +25,8 @@ public static void handleServerRequestEnter( @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - nacosClientRequest = NacosClientRequest.createRequest("handleServerRequest", - thisObject.getClass(), request); + nacosClientRequest = + NacosClientRequest.createRequest("handleServerRequest", thisObject.getClass(), request); if (!instrumenter().shouldStart(parentContext, nacosClientRequest)) { return; } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientCodeAttributesGetter.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientCodeAttributesGetter.java index 3b2514b56484..f94a4317498e 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientCodeAttributesGetter.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientCodeAttributesGetter.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors; import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesGetter; diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientExperimentalAttributeExtractor.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientExperimentalAttributeExtractor.java index 9097b5391770..de3845985c7a 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientExperimentalAttributeExtractor.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientExperimentalAttributeExtractor.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors; import com.alibaba.nacos.api.remote.response.Response; @@ -10,15 +15,16 @@ public class NacosClientExperimentalAttributeExtractor implements AttributesExtractor { @Override - public void onStart(AttributesBuilder attributes, Context parentContext, - NacosClientRequest nacosClientRequest) { + public void onStart( + AttributesBuilder attributes, Context parentContext, NacosClientRequest nacosClientRequest) { attributes.putAll(nacosClientRequest.getAttributes()); } @Override - public void onEnd(AttributesBuilder attributes, Context context, - NacosClientRequest nacosClientRequest, @Nullable Response response, - @Nullable Throwable error) { - - } + public void onEnd( + AttributesBuilder attributes, + Context context, + NacosClientRequest nacosClientRequest, + @Nullable Response response, + @Nullable Throwable error) {} } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanNameExtractor.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanNameExtractor.java index 5a5f53fbe6e3..1625269f9045 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanNameExtractor.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanNameExtractor.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors; import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; @@ -8,5 +13,4 @@ public class NacosClientSpanNameExtractor implements SpanNameExtractor { +public class NacosClientSpanStatusExtractor + implements SpanStatusExtractor { @Override - public void extract(SpanStatusBuilder spanStatusBuilder, NacosClientRequest nacosClientRequest, - @Nullable Response response, @Nullable Throwable error) { + public void extract( + SpanStatusBuilder spanStatusBuilder, + NacosClientRequest nacosClientRequest, + @Nullable Response response, + @Nullable Throwable error) { if (response == null || !response.isSuccess()) { spanStatusBuilder.setStatus(StatusCode.ERROR); } else { diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/GrpcConnectionInstrumentation.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/GrpcConnectionInstrumentation.java index 64f48e4df4df..6b8368eb0e19 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/GrpcConnectionInstrumentation.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/GrpcConnectionInstrumentation.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.instrumentations; import static net.bytebuddy.matcher.ElementMatchers.isMethod; @@ -22,12 +27,7 @@ public ElementMatcher typeMatcher() { @Override public void transform(TypeTransformer transformer) { transformer.applyAdviceToMethod( - isMethod() - .and(isPublic()) - .and(namedOneOf("request")) - .and(returns(Response.class)), - GrpcConnectionRequestAdvice.class.getName() - ); + isMethod().and(isPublic()).and(namedOneOf("request")).and(returns(Response.class)), + GrpcConnectionRequestAdvice.class.getName()); } - } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/RpcClientInstrumentation.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/RpcClientInstrumentation.java index d3a3d041eb03..f893d75ff6d8 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/RpcClientInstrumentation.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/RpcClientInstrumentation.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.instrumentations; import static net.bytebuddy.matcher.ElementMatchers.isMethod; @@ -26,8 +31,6 @@ public void transform(TypeTransformer transformer) { .and(isProtected()) .and(namedOneOf("handleServerRequest")) .and(returns(Response.class)), - RpcClientHandleServerRequestAdvice.class.getName() - ); + RpcClientHandleServerRequestAdvice.class.getName()); } - } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java index e88c51d87722..12f489253e1c 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package com.alibaba.nacos.common.remote.client; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -27,40 +32,40 @@ public class RpcClientTest { @RegisterExtension private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); + private RpcClient rpcClient; - @Mock - private ServerRequestHandler serverRequestHandler; + @Mock private ServerRequestHandler serverRequestHandler; private List nacosClientRequestList; @BeforeEach public void setUp() { nacosClientRequestList = NacosClientTestHelper.REQUEST_LIST; - rpcClient = new RpcClient("testRpcClient") { - @Override - public ConnectionType getConnectionType() { - return ConnectionType.GRPC; - } + rpcClient = + new RpcClient("testRpcClient") { + @Override + public ConnectionType getConnectionType() { + return ConnectionType.GRPC; + } - @Override - public int rpcPortOffset() { - return 0; - } + @Override + public int rpcPortOffset() { + return 0; + } - @Override - public Connection connectToServer(ServerInfo serverInfo) throws Exception { - return null; - } - }; + @Override + public Connection connectToServer(ServerInfo serverInfo) throws Exception { + return null; + } + }; rpcClient.serverRequestHandlers = Collections.singletonList(serverRequestHandler); } - @Test public void handleServerRequestSuccessResponse() { - when(serverRequestHandler.requestReply(any(Request.class))).thenReturn( - NacosClientTestHelper.SUCCESS_RESPONSE); + when(serverRequestHandler.requestReply(any(Request.class))) + .thenReturn(NacosClientTestHelper.SUCCESS_RESPONSE); for (Request request : nacosClientRequestList) { Response response = rpcClient.handleServerRequest(request); assertNotNull(response); @@ -83,8 +88,8 @@ public void handleServerRequestSuccessResponse() { @Test public void handleServerRequestErrorResponse() { - when(serverRequestHandler.requestReply(any(Request.class))).thenReturn( - NacosClientTestHelper.ERROR_RESPONSE); + when(serverRequestHandler.requestReply(any(Request.class))) + .thenReturn(NacosClientTestHelper.ERROR_RESPONSE); for (Request request : nacosClientRequestList) { Response response = rpcClient.handleServerRequest(request); assertNotNull(response); @@ -105,4 +110,3 @@ public void handleServerRequestErrorResponse() { } } } - diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java index 6051952538a8..79313aeac150 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package com.alibaba.nacos.common.remote.client.grpc; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -40,20 +45,16 @@ public class GrpcConnectionTest { @RegisterExtension private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - @Mock - private Executor executor; - @Mock - private ManagedChannel channel; + @Mock private Executor executor; + + @Mock private ManagedChannel channel; - @Mock - private StreamObserver payloadStreamObserver; - - @Mock - RequestGrpc.RequestFutureStub grpcFutureServiceStub; + @Mock private StreamObserver payloadStreamObserver; - @Mock - ListenableFuture future; + @Mock RequestGrpc.RequestFutureStub grpcFutureServiceStub; + + @Mock ListenableFuture future; Payload responsePayload; @@ -132,5 +133,4 @@ public void requestErrorResponse() testing.clearData(); } } - } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java index 5f2a4fc93113..6ffdaf0a6a19 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java @@ -1,12 +1,17 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package com.alibaba.nacos.shaded.com.google.errorprone.annotations; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - @Target(TYPE) @Retention(RUNTIME) @Documented diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientTestHelper.java b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientTestHelper.java index cb96ac4e5533..8dd6c2b9cfe4 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientTestHelper.java +++ b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientTestHelper.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; @@ -29,7 +34,8 @@ import java.util.stream.Collectors; public class NacosClientTestHelper { - public static final List RPC_CLIENT_HANDLE_SERVER_REQUEST_REQUEST_LIST = new ArrayList<>(); + public static final List RPC_CLIENT_HANDLE_SERVER_REQUEST_REQUEST_LIST = + new ArrayList<>(); public static final List GRPC_CONNECTION_REQUEST_LIST = new ArrayList<>(); public static final List REQUEST_LIST = new ArrayList<>(); public static final Map NACOS_CLIENT_REQUEST_MAP = new HashMap<>(); @@ -77,21 +83,22 @@ public class NacosClientTestHelper { public static final String NOTIFY_CONFIG_CHANGE = "notifyConfigChange"; - public static final AttributeKey NACOS_NAME_SPACE_ATTR = AttributeKey.stringKey( - "nacos.namespace"); + public static final AttributeKey NACOS_NAME_SPACE_ATTR = + AttributeKey.stringKey("nacos.namespace"); - public static final AttributeKey NACOS_GROUP_NAME_ATTR = AttributeKey.stringKey("nacos.group.name"); + public static final AttributeKey NACOS_GROUP_NAME_ATTR = + AttributeKey.stringKey("nacos.group.name"); - public static final AttributeKey NACOS_SERVICE_NAME_ATTR = AttributeKey.stringKey( - "nacos.service.name"); + public static final AttributeKey NACOS_SERVICE_NAME_ATTR = + AttributeKey.stringKey("nacos.service.name"); - public static final AttributeKey NACOS_DATA_ID_ATTR = AttributeKey.stringKey( - "nacos.data.id"); + public static final AttributeKey NACOS_DATA_ID_ATTR = + AttributeKey.stringKey("nacos.data.id"); public static final AttributeKey NACOS_GROUP_ATTR = AttributeKey.stringKey("nacos.group"); - public static final AttributeKey NACOS_TENANT_ATTR = AttributeKey.stringKey( - "nacos.tenant"); + public static final AttributeKey NACOS_TENANT_ATTR = + AttributeKey.stringKey("nacos.tenant"); private NacosClientTestHelper() {} @@ -155,35 +162,16 @@ private NacosClientTestHelper() {} when(CONFIG_CHANGE_NOTIFY_REQUEST.getGroup()).thenReturn(GROUP); when(CONFIG_CHANGE_NOTIFY_REQUEST.getTenant()).thenReturn(TENANT); - NACOS_CLIENT_REQUEST_MAP.put( - NACOS_PREFIX + NOTIFY_CONFIG_CHANGE, - CONFIG_CHANGE_NOTIFY_REQUEST); - NACOS_CLIENT_REQUEST_MAP.put( - NACOS_PREFIX + PUBLISH_CONFIG, - CONFIG_PUBLISH_REQUEST); - NACOS_CLIENT_REQUEST_MAP.put( - NACOS_PREFIX + QUERY_CONFIG, - CONFIG_QUERY_REQUEST); - NACOS_CLIENT_REQUEST_MAP.put( - NACOS_PREFIX + REMOVE_CONFIG, - CONFIG_REMOVE_REQUEST); - NACOS_CLIENT_REQUEST_MAP.put(NACOS_PREFIX + INSTANCE_REQUEST_TYPE, - INSTANCE_REQUEST); - NACOS_CLIENT_REQUEST_MAP.put( - NACOS_PREFIX + NOTIFY_SUBSCRIBE_CHANGE, - NOTIFY_SUBSCRIBER_REQUEST); - NACOS_CLIENT_REQUEST_MAP.put( - NACOS_PREFIX + GET_SERVICE_LIST, - SERVICE_LIST_REQUEST); - NACOS_CLIENT_REQUEST_MAP.put( - NACOS_PREFIX + QUERY_SERVICE, - SERVICE_QUERY_REQUEST); - NACOS_CLIENT_REQUEST_MAP.put( - NACOS_PREFIX + SUBSCRIBE_SERVICE, - SUBSCRIBE_SERVICE_REQUEST); - NACOS_CLIENT_REQUEST_MAP.put( - NACOS_PREFIX + UNSUBSCRIBE_SERVICE, - UN_SUBSCRIBE_SERVICE_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put(NACOS_PREFIX + NOTIFY_CONFIG_CHANGE, CONFIG_CHANGE_NOTIFY_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put(NACOS_PREFIX + PUBLISH_CONFIG, CONFIG_PUBLISH_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put(NACOS_PREFIX + QUERY_CONFIG, CONFIG_QUERY_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put(NACOS_PREFIX + REMOVE_CONFIG, CONFIG_REMOVE_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put(NACOS_PREFIX + INSTANCE_REQUEST_TYPE, INSTANCE_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put(NACOS_PREFIX + NOTIFY_SUBSCRIBE_CHANGE, NOTIFY_SUBSCRIBER_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put(NACOS_PREFIX + GET_SERVICE_LIST, SERVICE_LIST_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put(NACOS_PREFIX + QUERY_SERVICE, SERVICE_QUERY_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put(NACOS_PREFIX + SUBSCRIBE_SERVICE, SUBSCRIBE_SERVICE_REQUEST); + NACOS_CLIENT_REQUEST_MAP.put(NACOS_PREFIX + UNSUBSCRIBE_SERVICE, UN_SUBSCRIBE_SERVICE_REQUEST); for (Request value : NACOS_CLIENT_REQUEST_MAP.values()) { REQUEST_LIST.add(value); @@ -193,17 +181,13 @@ private NacosClientTestHelper() {} GRPC_CONNECTION_REQUEST_LIST.add(value); } } - NACOS_CLIENT_REQUEST_NAME_MAP.putAll(NACOS_CLIENT_REQUEST_MAP.entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey))); + NACOS_CLIENT_REQUEST_NAME_MAP.putAll( + NACOS_CLIENT_REQUEST_MAP.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey))); } private static final String[] methodNames = { - "getNamespace", - "getGroupName", - "getServiceName", - "getDataId", - "getGroup", - "getTenant" + "getNamespace", "getGroupName", "getServiceName", "getDataId", "getGroup", "getTenant" }; private static final Map> methodToAttributeMap = new HashMap<>(); @@ -217,15 +201,14 @@ private NacosClientTestHelper() {} methodToAttributeMap.put("getTenant", NACOS_TENANT_ATTR); } - - public static List requestAttributeAssertions ( + public static List requestAttributeAssertions( String codeNamespace, String codeFunction, Request request) { List attributeAssertions = - new ArrayList<>(asList( - equalTo(AttributeKey.stringKey("code.namespace"), codeNamespace), - equalTo(AttributeKey.stringKey("code.function"), codeFunction), - equalTo(AttributeKey.stringKey("service.discovery.system"), "nacos")) - ); + new ArrayList<>( + asList( + equalTo(AttributeKey.stringKey("code.namespace"), codeNamespace), + equalTo(AttributeKey.stringKey("code.function"), codeFunction), + equalTo(AttributeKey.stringKey("service.discovery.system"), "nacos"))); for (String methodName : methodNames) { Method method = null; From 5af892d61be4895a840f9dee0ed1617de8478d4b Mon Sep 17 00:00:00 2001 From: huangweilong <13622993145@163.com> Date: Sun, 8 Dec 2024 15:27:45 +0800 Subject: [PATCH 3/5] Update(nacos-client): Improve code formatting and muzzle validation --- instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts b/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts index fae790405484..201deff6ce47 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts +++ b/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts @@ -7,7 +7,7 @@ muzzle { group.set("com.alibaba.nacos") module.set("nacos-client") versions.set("[2.0.3,)") - skip("0.5.0","1.1.2", "1.1.4", "1.4.7", "2.0.1","2.0.2") + skip("0.5.0", "0.6.1", "1.1.2", "1.1.4", "1.4.7", "2.0.1", "2.0.2") assertInverse.set(true) } } From afa008072f16206cca4a77ab2089eaaf60bfb9d6 Mon Sep 17 00:00:00 2001 From: huangweilong <13622993145@163.com> Date: Sat, 21 Dec 2024 13:00:29 +0800 Subject: [PATCH 4/5] fix: Update version and code style - Updated version to support Nacos client version 2.0.0 - Default the plugin to be disabled --- docs/supported-libraries.md | 2 +- .../README.md | 6 ++ .../javaagent/build.gradle.kts | 22 +++++ .../client/v2_0_0}/NacosClientConstants.java | 5 +- .../client/v2_0_0}/NacosClientHelper.java | 2 +- .../NacosClientInstrumentationModule.java | 23 +++-- .../client/v2_0_0}/NacosClientRequest.java | 2 +- .../v2_0_0}/NacosClientRequestOperator.java | 2 +- .../client/v2_0_0}/NacosClientSingletons.java | 12 +-- .../NacosClientCodeAttributesGetter.java | 4 +- ...sClientExperimentalAttributeExtractor.java | 4 +- .../NacosClientSpanNameExtractor.java | 4 +- .../NacosClientSpanStatusExtractor.java | 4 +- .../GrpcConnectionInstrumentation.java | 76 ++++++++++++++++ .../RpcClientInstrumentation.java | 76 ++++++++++++++++ .../common/remote/client/RpcClientTest.java | 90 +++++++++---------- .../client/grpc/GrpcConnectionTest.java | 90 +++++++++---------- .../client/v2_0_0}/NacosClientTestHelper.java | 2 +- .../javaagent/build.gradle.kts | 24 ----- .../advices/GrpcConnectionRequestAdvice.java | 51 ----------- .../RpcClientHandleServerRequestAdvice.java | 51 ----------- .../GrpcConnectionInstrumentation.java | 33 ------- .../RpcClientInstrumentation.java | 36 -------- .../errorprone/annotations/DoNotMock.java | 20 ----- settings.gradle.kts | 2 +- 25 files changed, 308 insertions(+), 335 deletions(-) rename instrumentation/{nacos-client-2.0.3 => nacos-client-2.0.0}/README.md (85%) create mode 100644 instrumentation/nacos-client-2.0.0/javaagent/build.gradle.kts rename instrumentation/{nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3 => nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0}/NacosClientConstants.java (93%) rename instrumentation/{nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3 => nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0}/NacosClientHelper.java (99%) rename instrumentation/{nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3 => nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0}/NacosClientInstrumentationModule.java (58%) rename instrumentation/{nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3 => nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0}/NacosClientRequest.java (99%) rename instrumentation/{nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3 => nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0}/NacosClientRequestOperator.java (99%) rename instrumentation/{nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3 => nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0}/NacosClientSingletons.java (91%) rename instrumentation/{nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3 => nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0}/extractors/NacosClientCodeAttributesGetter.java (94%) rename instrumentation/{nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3 => nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0}/extractors/NacosClientExperimentalAttributeExtractor.java (95%) rename instrumentation/{nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3 => nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0}/extractors/NacosClientSpanNameExtractor.java (91%) rename instrumentation/{nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3 => nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0}/extractors/NacosClientSpanStatusExtractor.java (95%) create mode 100644 instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/instrumentations/GrpcConnectionInstrumentation.java create mode 100644 instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/instrumentations/RpcClientInstrumentation.java rename instrumentation/{nacos-client-2.0.3 => nacos-client-2.0.0}/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java (54%) rename instrumentation/{nacos-client-2.0.3 => nacos-client-2.0.0}/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java (64%) rename instrumentation/{nacos-client-2.0.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3 => nacos-client-2.0.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0}/NacosClientTestHelper.java (99%) delete mode 100644 instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts delete mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/GrpcConnectionRequestAdvice.java delete mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/RpcClientHandleServerRequestAdvice.java delete mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/GrpcConnectionInstrumentation.java delete mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/RpcClientInstrumentation.java delete mode 100644 instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java diff --git a/docs/supported-libraries.md b/docs/supported-libraries.md index f62ab13f3c4f..2e7410ac411d 100644 --- a/docs/supported-libraries.md +++ b/docs/supported-libraries.md @@ -101,7 +101,7 @@ These are the supported libraries and frameworks: | [Micrometer](https://micrometer.io/) | 1.5+ | [opentelemetry-micrometer-1.5](../instrumentation/micrometer/micrometer-1.5/library) | none | | [MongoDB Driver](https://mongodb.github.io/mongo-java-driver/) | 3.1+ | [opentelemetry-mongo-3.1](../instrumentation/mongo/mongo-3.1/library) | [Database Client Spans] | | [MyBatis](https://mybatis.org/mybatis-3/) | 3.2+ | N/A | none | -| [Nacos](https://nacos.io/) | 2.0.3+ | N/A | none | +| [Nacos Client](https://nacos.io/) | 2.0.0+ | N/A | none | | [Netty HTTP codec [5]](https://github.com/netty/netty) | 3.8+ | [opentelemetry-netty-4.1](../instrumentation/netty/netty-4.1/library) | [HTTP Client Spans], [HTTP Client Metrics], [HTTP Server Spans], [HTTP Server Metrics] | | [OkHttp](https://github.com/square/okhttp/) | 2.2+ | [opentelemetry-okhttp-3.0](../instrumentation/okhttp/okhttp-3.0/library) | [HTTP Client Spans], [HTTP Client Metrics] | | [Oracle UCP](https://docs.oracle.com/database/121/JJUCP/) | 11.2+ | [opentelemetry-oracle-ucp-11.2](../instrumentation/oracle-ucp-11.2/library) | [Database Pool Metrics] | diff --git a/instrumentation/nacos-client-2.0.3/README.md b/instrumentation/nacos-client-2.0.0/README.md similarity index 85% rename from instrumentation/nacos-client-2.0.3/README.md rename to instrumentation/nacos-client-2.0.0/README.md index 09eae09ba1b0..cc20ad50f794 100644 --- a/instrumentation/nacos-client-2.0.3/README.md +++ b/instrumentation/nacos-client-2.0.0/README.md @@ -2,6 +2,12 @@ - `com.alibaba.nacos.common.remote.client.grpc.GrpcConnection#request` - `com.alibaba.nacos.common.remote.client.RpcClient#handleServerRequest` +## Enable Configuration + +| Configuration Items | Default Value | +|:----------------------------------------------------|:---------------| +| `otel.instrumentation.nacos-client.default-enabled` | `false` | + ## Span Info Details diff --git a/instrumentation/nacos-client-2.0.0/javaagent/build.gradle.kts b/instrumentation/nacos-client-2.0.0/javaagent/build.gradle.kts new file mode 100644 index 000000000000..a141dd6c72e1 --- /dev/null +++ b/instrumentation/nacos-client-2.0.0/javaagent/build.gradle.kts @@ -0,0 +1,22 @@ +plugins { + id("otel.javaagent-instrumentation") +} + +muzzle { + pass { + group.set("com.alibaba.nacos") + module.set("nacos-client") + versions.set("[2.0.0,)") + skip("0.5.0", "0.6.1", "1.1.2", "1.1.4", "1.4.7") + assertInverse.set(true) + } +} + +dependencies { + library("com.alibaba.nacos:nacos-client:2.0.0") + testImplementation("javax.annotation:javax.annotation-api:1.3.2") +} + +tasks.withType().configureEach { + jvmArgs("-Dotel.instrumentation.nacos-client.default-enabled=true") +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientConstants.java b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientConstants.java similarity index 93% rename from instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientConstants.java rename to instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientConstants.java index ee8feebe0d52..c080371d2366 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientConstants.java +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientConstants.java @@ -3,13 +3,16 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0; import io.opentelemetry.api.common.AttributeKey; public class NacosClientConstants { private NacosClientConstants() {} + public static final String OTEL_NACOS_CLIENT_ENABLED = + "otel.instrumentation.nacos-client.default-enabled"; + public static final String NACOS_PREFIX = "Nacos/"; public static final String SERVER_CHECK = "serverCheck"; diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientHelper.java b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientHelper.java similarity index 99% rename from instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientHelper.java rename to instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientHelper.java index 83d3b62688a1..d9cad42288c5 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientHelper.java +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientHelper.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0; import com.alibaba.nacos.api.config.remote.request.ConfigChangeNotifyRequest; import com.alibaba.nacos.api.config.remote.request.ConfigPublishRequest; diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientInstrumentationModule.java b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientInstrumentationModule.java similarity index 58% rename from instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientInstrumentationModule.java rename to instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientInstrumentationModule.java index 42f5a26a68fa..9fbb7e4efda9 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientInstrumentationModule.java +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientInstrumentationModule.java @@ -3,26 +3,31 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0; + +import static java.util.Arrays.asList; import com.google.auto.service.AutoService; import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; -import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.instrumentations.GrpcConnectionInstrumentation; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.instrumentations.RpcClientInstrumentation; -import java.util.Arrays; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.instrumentations.GrpcConnectionInstrumentation; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.instrumentations.RpcClientInstrumentation; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import java.util.List; @AutoService(InstrumentationModule.class) -public class NacosClientInstrumentationModule extends InstrumentationModule - implements ExperimentalInstrumentationModule { +public class NacosClientInstrumentationModule extends InstrumentationModule { public NacosClientInstrumentationModule() { - super("nacos-client", "nacos-client-2.0.3"); + super("nacos-client", "nacos-client-2.0.0"); } @Override public List typeInstrumentations() { - return Arrays.asList(new GrpcConnectionInstrumentation(), new RpcClientInstrumentation()); + return asList(new GrpcConnectionInstrumentation(), new RpcClientInstrumentation()); + } + + @Override + public boolean defaultEnabled(ConfigProperties config) { + return config.getBoolean(NacosClientConstants.OTEL_NACOS_CLIENT_ENABLED, false); } } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequest.java b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientRequest.java similarity index 99% rename from instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequest.java rename to instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientRequest.java index a5258ac799fc..313b3f487c51 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequest.java +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientRequest.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0; import com.alibaba.nacos.api.remote.request.Request; import io.opentelemetry.api.common.Attributes; diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequestOperator.java b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientRequestOperator.java similarity index 99% rename from instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequestOperator.java rename to instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientRequestOperator.java index 2c945574d1aa..600b058f6058 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientRequestOperator.java +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientRequestOperator.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0; import com.alibaba.nacos.api.remote.request.Request; import io.opentelemetry.api.common.Attributes; diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientSingletons.java b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientSingletons.java similarity index 91% rename from instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientSingletons.java rename to instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientSingletons.java index 6f5d6e5cc372..984d927d7e67 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientSingletons.java +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientSingletons.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0; import com.alibaba.nacos.api.remote.response.Response; import io.opentelemetry.api.GlobalOpenTelemetry; @@ -15,13 +15,13 @@ import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors.NacosClientCodeAttributesGetter; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors.NacosClientExperimentalAttributeExtractor; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors.NacosClientSpanNameExtractor; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors.NacosClientSpanStatusExtractor; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.extractors.NacosClientCodeAttributesGetter; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.extractors.NacosClientExperimentalAttributeExtractor; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.extractors.NacosClientSpanNameExtractor; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.extractors.NacosClientSpanStatusExtractor; public final class NacosClientSingletons { - private static final String INSTRUMENTATION_NAME = "io.opentelemetry.nacos-client-2.0.3"; + private static final String INSTRUMENTATION_NAME = "io.opentelemetry.nacos-client-2.0.0"; private static final Instrumenter INSTRUMENTER = create(); public static Instrumenter instrumenter() { diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientCodeAttributesGetter.java b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/extractors/NacosClientCodeAttributesGetter.java similarity index 94% rename from instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientCodeAttributesGetter.java rename to instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/extractors/NacosClientCodeAttributesGetter.java index f94a4317498e..897514e20262 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientCodeAttributesGetter.java +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/extractors/NacosClientCodeAttributesGetter.java @@ -3,10 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors; +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.extractors; import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesGetter; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientRequest; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.NacosClientRequest; import javax.annotation.Nullable; public class NacosClientCodeAttributesGetter implements CodeAttributesGetter { diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientExperimentalAttributeExtractor.java b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/extractors/NacosClientExperimentalAttributeExtractor.java similarity index 95% rename from instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientExperimentalAttributeExtractor.java rename to instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/extractors/NacosClientExperimentalAttributeExtractor.java index de3845985c7a..c476bdb573be 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientExperimentalAttributeExtractor.java +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/extractors/NacosClientExperimentalAttributeExtractor.java @@ -3,13 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors; +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.extractors; import com.alibaba.nacos.api.remote.response.Response; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientRequest; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.NacosClientRequest; import javax.annotation.Nullable; public class NacosClientExperimentalAttributeExtractor diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanNameExtractor.java b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/extractors/NacosClientSpanNameExtractor.java similarity index 91% rename from instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanNameExtractor.java rename to instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/extractors/NacosClientSpanNameExtractor.java index 1625269f9045..61c43cf3abd9 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanNameExtractor.java +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/extractors/NacosClientSpanNameExtractor.java @@ -3,10 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors; +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.extractors; import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientRequest; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.NacosClientRequest; public class NacosClientSpanNameExtractor implements SpanNameExtractor { @Override diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanStatusExtractor.java b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/extractors/NacosClientSpanStatusExtractor.java similarity index 95% rename from instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanStatusExtractor.java rename to instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/extractors/NacosClientSpanStatusExtractor.java index 1a3322ccc4e2..0b8158f9784a 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/extractors/NacosClientSpanStatusExtractor.java +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/extractors/NacosClientSpanStatusExtractor.java @@ -3,13 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.extractors; +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.extractors; import com.alibaba.nacos.api.remote.response.Response; import io.opentelemetry.api.trace.StatusCode; import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusBuilder; import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientRequest; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.NacosClientRequest; import javax.annotation.Nullable; public class NacosClientSpanStatusExtractor diff --git a/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/instrumentations/GrpcConnectionInstrumentation.java b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/instrumentations/GrpcConnectionInstrumentation.java new file mode 100644 index 000000000000..f86e4a5e4d95 --- /dev/null +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/instrumentations/GrpcConnectionInstrumentation.java @@ -0,0 +1,76 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.instrumentations; + +import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; +import static io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.NacosClientSingletons.instrumenter; +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.returns; + +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.api.remote.response.Response; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.NacosClientRequest; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; + +public class GrpcConnectionInstrumentation implements TypeInstrumentation { + @Override + public ElementMatcher typeMatcher() { + return named("com.alibaba.nacos.common.remote.client.grpc.GrpcConnection"); + } + + @Override + public void transform(TypeTransformer transformer) { + transformer.applyAdviceToMethod( + isMethod() + .and(isPublic()) + .and(named("request")) + .and(returns(named("com.alibaba.nacos.api.remote.response.Response"))), + GrpcConnectionInstrumentation.class.getName() + "$GrpcConnectionRequestAdvice"); + } + + public static class GrpcConnectionRequestAdvice { + @SuppressWarnings("unused") + @Advice.OnMethodEnter(suppress = Throwable.class) + public static void requestEnter( + @Advice.This Object thisObject, + @Advice.Argument(0) Request request, + @Advice.Local("otelRequest") NacosClientRequest nacosClientRequest, + @Advice.Local("otelContext") Context context, + @Advice.Local("otelScope") Scope scope) { + Context parentContext = currentContext(); + nacosClientRequest = + NacosClientRequest.createRequest("request", thisObject.getClass(), request); + if (!instrumenter().shouldStart(parentContext, nacosClientRequest)) { + return; + } + context = instrumenter().start(parentContext, nacosClientRequest); + scope = context.makeCurrent(); + } + + @SuppressWarnings("unused") + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) + public static void requestExit( + @Advice.Return Response response, + @Advice.Thrown Throwable throwable, + @Advice.Local("otelRequest") NacosClientRequest nacosClientRequest, + @Advice.Local("otelContext") Context context, + @Advice.Local("otelScope") Scope scope) { + if (scope == null) { + return; + } + scope.close(); + instrumenter().end(context, nacosClientRequest, response, throwable); + } + } +} diff --git a/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/instrumentations/RpcClientInstrumentation.java b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/instrumentations/RpcClientInstrumentation.java new file mode 100644 index 000000000000..06e9df1c0afa --- /dev/null +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/instrumentations/RpcClientInstrumentation.java @@ -0,0 +1,76 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.instrumentations; + +import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; +import static io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.NacosClientSingletons.instrumenter; +import static net.bytebuddy.matcher.ElementMatchers.isMethod; +import static net.bytebuddy.matcher.ElementMatchers.isProtected; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.returns; + +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.api.remote.response.Response; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.NacosClientRequest; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; + +public class RpcClientInstrumentation implements TypeInstrumentation { + @Override + public ElementMatcher typeMatcher() { + return named("com.alibaba.nacos.common.remote.client.RpcClient"); + } + + @Override + public void transform(TypeTransformer transformer) { + transformer.applyAdviceToMethod( + isMethod() + .and(isProtected()) + .and(named("handleServerRequest")) + .and(returns(named("com.alibaba.nacos.api.remote.response.Response"))), + RpcClientInstrumentation.class.getName() + "$RpcClientHandleServerRequestAdvice"); + } + + public static class RpcClientHandleServerRequestAdvice { + @SuppressWarnings("unused") + @Advice.OnMethodEnter(suppress = Throwable.class) + public static void handleServerRequestEnter( + @Advice.This Object thisObject, + @Advice.Argument(0) Request request, + @Advice.Local("otelRequest") NacosClientRequest nacosClientRequest, + @Advice.Local("otelContext") Context context, + @Advice.Local("otelScope") Scope scope) { + Context parentContext = currentContext(); + nacosClientRequest = + NacosClientRequest.createRequest("handleServerRequest", thisObject.getClass(), request); + if (!instrumenter().shouldStart(parentContext, nacosClientRequest)) { + return; + } + context = instrumenter().start(parentContext, nacosClientRequest); + scope = context.makeCurrent(); + } + + @SuppressWarnings("unused") + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) + public static void handleServerRequestExit( + @Advice.Return Response response, + @Advice.Thrown Throwable throwable, + @Advice.Local("otelRequest") NacosClientRequest nacosClientRequest, + @Advice.Local("otelContext") Context context, + @Advice.Local("otelScope") Scope scope) { + if (scope == null) { + return; + } + scope.close(); + instrumenter().end(context, nacosClientRequest, response, throwable); + } + } +} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java b/instrumentation/nacos-client-2.0.0/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java similarity index 54% rename from instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java rename to instrumentation/nacos-client-2.0.0/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java index 12f489253e1c..9b3425a133c4 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java @@ -17,14 +17,15 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientTestHelper; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.NacosClientTestHelper; import io.opentelemetry.sdk.trace.data.StatusData; import java.util.Collections; import java.util.List; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -37,11 +38,8 @@ public class RpcClientTest { @Mock private ServerRequestHandler serverRequestHandler; - private List nacosClientRequestList; - @BeforeEach public void setUp() { - nacosClientRequestList = NacosClientTestHelper.REQUEST_LIST; rpcClient = new RpcClient("testRpcClient") { @Override @@ -62,51 +60,53 @@ public Connection connectToServer(ServerInfo serverInfo) throws Exception { rpcClient.serverRequestHandlers = Collections.singletonList(serverRequestHandler); } - @Test - public void handleServerRequestSuccessResponse() { + @ParameterizedTest + @MethodSource("requestProvider") + public void handleServerRequestSuccessResponse(Request request) { when(serverRequestHandler.requestReply(any(Request.class))) .thenReturn(NacosClientTestHelper.SUCCESS_RESPONSE); - for (Request request : nacosClientRequestList) { - Response response = rpcClient.handleServerRequest(request); - assertNotNull(response); - assertTrue(response.isSuccess()); - testing.waitAndAssertTraces( - trace -> { - trace.hasSpansSatisfyingExactly( - span -> { - span.hasName(NacosClientTestHelper.NACOS_CLIENT_REQUEST_NAME_MAP.get(request)) - .hasKind(SpanKind.INTERNAL) - .hasStatus(StatusData.unset()) - .hasAttributesSatisfyingExactly( - NacosClientTestHelper.requestAttributeAssertions( - rpcClient.getClass().getName(), "handleServerRequest", request)); - }); - }); - testing.clearData(); - } + Response response = rpcClient.handleServerRequest(request); + assertNotNull(response); + assertTrue(response.isSuccess()); + testing.waitAndAssertTraces( + trace -> { + trace.hasSpansSatisfyingExactly( + span -> { + span.hasName(NacosClientTestHelper.NACOS_CLIENT_REQUEST_NAME_MAP.get(request)) + .hasKind(SpanKind.INTERNAL) + .hasStatus(StatusData.unset()) + .hasAttributesSatisfyingExactly( + NacosClientTestHelper.requestAttributeAssertions( + rpcClient.getClass().getName(), "handleServerRequest", request)); + }); + }); + testing.clearData(); } - @Test - public void handleServerRequestErrorResponse() { + @ParameterizedTest + @MethodSource("requestProvider") + public void handleServerRequestErrorResponse(Request request) { when(serverRequestHandler.requestReply(any(Request.class))) .thenReturn(NacosClientTestHelper.ERROR_RESPONSE); - for (Request request : nacosClientRequestList) { - Response response = rpcClient.handleServerRequest(request); - assertNotNull(response); - assertFalse(response.isSuccess()); - testing.waitAndAssertTraces( - trace -> { - trace.hasSpansSatisfyingExactly( - span -> { - span.hasName(NacosClientTestHelper.NACOS_CLIENT_REQUEST_NAME_MAP.get(request)) - .hasKind(SpanKind.INTERNAL) - .hasStatus(StatusData.error()) - .hasAttributesSatisfyingExactly( - NacosClientTestHelper.requestAttributeAssertions( - rpcClient.getClass().getName(), "handleServerRequest", request)); - }); - }); - testing.clearData(); - } + Response response = rpcClient.handleServerRequest(request); + assertNotNull(response); + assertFalse(response.isSuccess()); + testing.waitAndAssertTraces( + trace -> { + trace.hasSpansSatisfyingExactly( + span -> { + span.hasName(NacosClientTestHelper.NACOS_CLIENT_REQUEST_NAME_MAP.get(request)) + .hasKind(SpanKind.INTERNAL) + .hasStatus(StatusData.error()) + .hasAttributesSatisfyingExactly( + NacosClientTestHelper.requestAttributeAssertions( + rpcClient.getClass().getName(), "handleServerRequest", request)); + }); + }); + testing.clearData(); + } + + private static List requestProvider() { + return NacosClientTestHelper.REQUEST_LIST; } } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java b/instrumentation/nacos-client-2.0.0/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java similarity index 64% rename from instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java rename to instrumentation/nacos-client-2.0.0/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java index 79313aeac150..3e82fc99e0d8 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/test/java/com/alibaba/nacos/common/remote/client/grpc/GrpcConnectionTest.java @@ -24,7 +24,7 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientTestHelper; +import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0.NacosClientTestHelper; import io.opentelemetry.sdk.trace.data.StatusData; import java.util.List; import java.util.concurrent.ExecutionException; @@ -34,9 +34,10 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -62,8 +63,6 @@ public class GrpcConnectionTest { GrpcConnection connection; - private List nacosClientRequestList; - @BeforeAll public static void setUpBeforeClass() { PayloadRegistry.init(); @@ -71,7 +70,6 @@ public static void setUpBeforeClass() { @BeforeEach public void setUp() throws Exception { - nacosClientRequestList = NacosClientTestHelper.REQUEST_LIST; connection = new GrpcConnection(new RpcClient.ServerInfo(), executor); connection.setChannel(channel); connection.setPayloadStreamObserver(payloadStreamObserver); @@ -86,51 +84,53 @@ public void tearDown() { connection.close(); } - @Test - public void requestSuccessResponse() + @ParameterizedTest + @MethodSource("requestProvider") + public void requestSuccessResponse(Request request) throws NacosException, ExecutionException, InterruptedException, TimeoutException { when(future.get(-1, TimeUnit.MILLISECONDS)).thenReturn(responsePayload); - for (Request request : nacosClientRequestList) { - Response response = connection.request(request, -1); - assertNotNull(response); - assertTrue(response.isSuccess()); - testing.waitAndAssertTraces( - trace -> { - trace.hasSpansSatisfyingExactly( - span -> { - span.hasName(NacosClientTestHelper.NACOS_CLIENT_REQUEST_NAME_MAP.get(request)) - .hasKind(SpanKind.INTERNAL) - .hasStatus(StatusData.unset()) - .hasAttributesSatisfyingExactly( - NacosClientTestHelper.requestAttributeAssertions( - connection.getClass().getName(), "request", request)); - }); - }); - testing.clearData(); - } + Response response = connection.request(request, -1); + assertNotNull(response); + assertTrue(response.isSuccess()); + testing.waitAndAssertTraces( + trace -> { + trace.hasSpansSatisfyingExactly( + span -> { + span.hasName(NacosClientTestHelper.NACOS_CLIENT_REQUEST_NAME_MAP.get(request)) + .hasKind(SpanKind.INTERNAL) + .hasStatus(StatusData.unset()) + .hasAttributesSatisfyingExactly( + NacosClientTestHelper.requestAttributeAssertions( + connection.getClass().getName(), "request", request)); + }); + }); + testing.clearData(); } - @Test - public void requestErrorResponse() + @ParameterizedTest + @MethodSource("requestProvider") + public void requestErrorResponse(Request request) throws NacosException, ExecutionException, InterruptedException, TimeoutException { when(future.get(-1, TimeUnit.MILLISECONDS)).thenReturn(errorResponsePayload); - for (Request request : nacosClientRequestList) { - Response response = connection.request(request, -1); - assertNotNull(response); - assertFalse(response.isSuccess()); - testing.waitAndAssertTraces( - trace -> { - trace.hasSpansSatisfyingExactly( - span -> { - span.hasName(NacosClientTestHelper.NACOS_CLIENT_REQUEST_NAME_MAP.get(request)) - .hasKind(SpanKind.INTERNAL) - .hasStatus(StatusData.error()) - .hasAttributesSatisfyingExactly( - NacosClientTestHelper.requestAttributeAssertions( - connection.getClass().getName(), "request", request)); - }); - }); - testing.clearData(); - } + Response response = connection.request(request, -1); + assertNotNull(response); + assertFalse(response.isSuccess()); + testing.waitAndAssertTraces( + trace -> { + trace.hasSpansSatisfyingExactly( + span -> { + span.hasName(NacosClientTestHelper.NACOS_CLIENT_REQUEST_NAME_MAP.get(request)) + .hasKind(SpanKind.INTERNAL) + .hasStatus(StatusData.error()) + .hasAttributesSatisfyingExactly( + NacosClientTestHelper.requestAttributeAssertions( + connection.getClass().getName(), "request", request)); + }); + }); + testing.clearData(); + } + + private static List requestProvider() { + return NacosClientTestHelper.REQUEST_LIST; } } diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientTestHelper.java b/instrumentation/nacos-client-2.0.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientTestHelper.java similarity index 99% rename from instrumentation/nacos-client-2.0.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientTestHelper.java rename to instrumentation/nacos-client-2.0.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientTestHelper.java index 8dd6c2b9cfe4..46a5ad6aaacf 100644 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/NacosClientTestHelper.java +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_0/NacosClientTestHelper.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3; +package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_0; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; import static java.util.Arrays.asList; diff --git a/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts b/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts deleted file mode 100644 index 201deff6ce47..000000000000 --- a/instrumentation/nacos-client-2.0.3/javaagent/build.gradle.kts +++ /dev/null @@ -1,24 +0,0 @@ -plugins { - id("otel.javaagent-instrumentation") -} - -muzzle { - pass { - group.set("com.alibaba.nacos") - module.set("nacos-client") - versions.set("[2.0.3,)") - skip("0.5.0", "0.6.1", "1.1.2", "1.1.4", "1.4.7", "2.0.1", "2.0.2") - assertInverse.set(true) - } -} - -dependencies { - val nacosClientVersion = "2.0.3" - implementation("com.alibaba.nacos:nacos-client:$nacosClientVersion") - testImplementation("javax.annotation:javax.annotation-api:1.3.2") -} - -tasks.withType().configureEach { - jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED") - jvmArgs("-XX:+IgnoreUnrecognizedVMOptions") -} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/GrpcConnectionRequestAdvice.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/GrpcConnectionRequestAdvice.java deleted file mode 100644 index e00f53fd4be2..000000000000 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/GrpcConnectionRequestAdvice.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.advices; - -import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; -import static io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientSingletons.instrumenter; - -import com.alibaba.nacos.api.remote.request.Request; -import com.alibaba.nacos.api.remote.response.Response; -import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientRequest; -import net.bytebuddy.asm.Advice; - -public class GrpcConnectionRequestAdvice { - @SuppressWarnings("unused") - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void requestEnter( - @Advice.This Object thisObject, - @Advice.Argument(0) Request request, - @Advice.Local("otelRequest") NacosClientRequest nacosClientRequest, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - Context parentContext = currentContext(); - nacosClientRequest = - NacosClientRequest.createRequest("request", thisObject.getClass(), request); - if (!instrumenter().shouldStart(parentContext, nacosClientRequest)) { - return; - } - context = instrumenter().start(parentContext, nacosClientRequest); - scope = context.makeCurrent(); - } - - @SuppressWarnings("unused") - @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) - public static void requestExit( - @Advice.Return Response response, - @Advice.Thrown Throwable throwable, - @Advice.Local("otelRequest") NacosClientRequest nacosClientRequest, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - if (scope == null) { - return; - } - scope.close(); - instrumenter().end(context, nacosClientRequest, response, throwable); - } -} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/RpcClientHandleServerRequestAdvice.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/RpcClientHandleServerRequestAdvice.java deleted file mode 100644 index 72fafe5783ec..000000000000 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/advices/RpcClientHandleServerRequestAdvice.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.advices; - -import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; -import static io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientSingletons.instrumenter; - -import com.alibaba.nacos.api.remote.request.Request; -import com.alibaba.nacos.api.remote.response.Response; -import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.NacosClientRequest; -import net.bytebuddy.asm.Advice; - -public class RpcClientHandleServerRequestAdvice { - @SuppressWarnings("unused") - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void handleServerRequestEnter( - @Advice.This Object thisObject, - @Advice.Argument(0) Request request, - @Advice.Local("otelRequest") NacosClientRequest nacosClientRequest, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - Context parentContext = currentContext(); - nacosClientRequest = - NacosClientRequest.createRequest("handleServerRequest", thisObject.getClass(), request); - if (!instrumenter().shouldStart(parentContext, nacosClientRequest)) { - return; - } - context = instrumenter().start(parentContext, nacosClientRequest); - scope = context.makeCurrent(); - } - - @SuppressWarnings("unused") - @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) - public static void handleServerRequestExit( - @Advice.Return Response response, - @Advice.Thrown Throwable throwable, - @Advice.Local("otelRequest") NacosClientRequest nacosClientRequest, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - if (scope == null) { - return; - } - scope.close(); - instrumenter().end(context, nacosClientRequest, response, throwable); - } -} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/GrpcConnectionInstrumentation.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/GrpcConnectionInstrumentation.java deleted file mode 100644 index 6b8368eb0e19..000000000000 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/GrpcConnectionInstrumentation.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.instrumentations; - -import static net.bytebuddy.matcher.ElementMatchers.isMethod; -import static net.bytebuddy.matcher.ElementMatchers.isPublic; -import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; -import static net.bytebuddy.matcher.ElementMatchers.returns; - -import com.alibaba.nacos.api.remote.response.Response; -import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; -import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.advices.GrpcConnectionRequestAdvice; -import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.matcher.ElementMatcher; -import net.bytebuddy.matcher.ElementMatchers; - -public class GrpcConnectionInstrumentation implements TypeInstrumentation { - @Override - public ElementMatcher typeMatcher() { - return ElementMatchers.named("com.alibaba.nacos.common.remote.client.grpc.GrpcConnection"); - } - - @Override - public void transform(TypeTransformer transformer) { - transformer.applyAdviceToMethod( - isMethod().and(isPublic()).and(namedOneOf("request")).and(returns(Response.class)), - GrpcConnectionRequestAdvice.class.getName()); - } -} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/RpcClientInstrumentation.java b/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/RpcClientInstrumentation.java deleted file mode 100644 index f893d75ff6d8..000000000000 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/nacos/client/v2_0_3/instrumentations/RpcClientInstrumentation.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.instrumentations; - -import static net.bytebuddy.matcher.ElementMatchers.isMethod; -import static net.bytebuddy.matcher.ElementMatchers.isProtected; -import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; -import static net.bytebuddy.matcher.ElementMatchers.returns; - -import com.alibaba.nacos.api.remote.response.Response; -import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; -import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.nacos.client.v2_0_3.advices.RpcClientHandleServerRequestAdvice; -import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.matcher.ElementMatcher; -import net.bytebuddy.matcher.ElementMatchers; - -public class RpcClientInstrumentation implements TypeInstrumentation { - @Override - public ElementMatcher typeMatcher() { - return ElementMatchers.named("com.alibaba.nacos.common.remote.client.RpcClient"); - } - - @Override - public void transform(TypeTransformer transformer) { - transformer.applyAdviceToMethod( - isMethod() - .and(isProtected()) - .and(namedOneOf("handleServerRequest")) - .and(returns(Response.class)), - RpcClientHandleServerRequestAdvice.class.getName()); - } -} diff --git a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java b/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java deleted file mode 100644 index 6ffdaf0a6a19..000000000000 --- a/instrumentation/nacos-client-2.0.3/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package com.alibaba.nacos.shaded.com.google.errorprone.annotations; - -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -@Target(TYPE) -@Retention(RUNTIME) -@Documented -public @interface DoNotMock { - String value(); -} diff --git a/settings.gradle.kts b/settings.gradle.kts index 51da2c23c312..c29911cb079d 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -391,7 +391,7 @@ include(":instrumentation:mongo:mongo-4.0:javaagent") include(":instrumentation:mongo:mongo-async-3.3:javaagent") include(":instrumentation:mongo:mongo-common:testing") include(":instrumentation:mybatis-3.2:javaagent") -include(":instrumentation:nacos-client-2.0.3:javaagent") +include(":instrumentation:nacos-client-2.0.0:javaagent") include(":instrumentation:netty:netty-3.8:javaagent") include(":instrumentation:netty:netty-4.0:javaagent") include(":instrumentation:netty:netty-4.1:javaagent") From e376621d275c50e4aa4cf44989d2eb58246b29fd Mon Sep 17 00:00:00 2001 From: huangweilong <13622993145@163.com> Date: Sat, 21 Dec 2024 16:13:53 +0800 Subject: [PATCH 5/5] fix ci --- .../javaagent/build.gradle.kts | 2 ++ .../errorprone/annotations/DoNotMock.java | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 instrumentation/nacos-client-2.0.0/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java diff --git a/instrumentation/nacos-client-2.0.0/javaagent/build.gradle.kts b/instrumentation/nacos-client-2.0.0/javaagent/build.gradle.kts index a141dd6c72e1..e1efb9a4a01f 100644 --- a/instrumentation/nacos-client-2.0.0/javaagent/build.gradle.kts +++ b/instrumentation/nacos-client-2.0.0/javaagent/build.gradle.kts @@ -15,6 +15,8 @@ muzzle { dependencies { library("com.alibaba.nacos:nacos-client:2.0.0") testImplementation("javax.annotation:javax.annotation-api:1.3.2") + + latestDepTestLibrary("com.alibaba.nacos:nacos-client:2.0.4+") } tasks.withType().configureEach { diff --git a/instrumentation/nacos-client-2.0.0/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java b/instrumentation/nacos-client-2.0.0/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java new file mode 100644 index 000000000000..6ffdaf0a6a19 --- /dev/null +++ b/instrumentation/nacos-client-2.0.0/javaagent/src/test/java/com/alibaba/nacos/shaded/com/google/errorprone/annotations/DoNotMock.java @@ -0,0 +1,20 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package com.alibaba.nacos.shaded.com.google.errorprone.annotations; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Target(TYPE) +@Retention(RUNTIME) +@Documented +public @interface DoNotMock { + String value(); +}