From 37808d7f539fb1bcb9de09d84d078ffb11078f32 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Sat, 21 Dec 2024 15:13:58 +0800 Subject: [PATCH 1/5] Fix wrong dubbo trace caused by using rpcContext.isProviderSide() --- .../v2_7/DubboInstrumentationModule.java | 6 ++- ...er.java => OpenTelemetryClientFilter.java} | 8 ++-- .../v2_7/OpenTelemetryServerFilter.java | 39 +++++++++++++++++++ .../META-INF/org.apache.dubbo.rpc.Filter | 3 +- .../apachedubbo/v2_7/DubboTelemetry.java | 12 ++++-- ...er.java => OpenTelemetryClientFilter.java} | 8 ++-- .../v2_7/OpenTelemetryServerFilter.java | 28 +++++++++++++ .../apachedubbo/v2_7/TracingFilter.java | 17 +++----- .../services/org.apache.dubbo.rpc.Filter | 3 +- 9 files changed, 99 insertions(+), 25 deletions(-) rename instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/{OpenTelemetryFilter.java => OpenTelemetryClientFilter.java} (88%) create mode 100644 instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java rename instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/{OpenTelemetryFilter.java => OpenTelemetryClientFilter.java} (80%) create mode 100644 instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java diff --git a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboInstrumentationModule.java b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboInstrumentationModule.java index c5f0b5641e94..b353997ea93b 100644 --- a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboInstrumentationModule.java +++ b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboInstrumentationModule.java @@ -44,7 +44,11 @@ public ElementMatcher.Junction classLoaderMatcher() { public void injectClasses(ClassInjector injector) { injector .proxyBuilder( - "io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7.OpenTelemetryFilter") + "io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7.OpenTelemetryClientFilter") + .inject(InjectionMode.CLASS_ONLY); + injector + .proxyBuilder( + "io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7.OpenTelemetryServerFilter") .inject(InjectionMode.CLASS_ONLY); } diff --git a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryFilter.java b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java similarity index 88% rename from instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryFilter.java rename to instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java index 550f469ad9df..5680dd8860bf 100644 --- a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryFilter.java +++ b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java @@ -16,12 +16,12 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Result; -@Activate(group = {"consumer", "provider"}) -public class OpenTelemetryFilter implements Filter { +@Activate(group = {"consumer"}) +public class OpenTelemetryClientFilter implements Filter { private final Filter delegate; - public OpenTelemetryFilter() { + public OpenTelemetryClientFilter() { delegate = DubboTelemetry.builder(GlobalOpenTelemetry.get()) .addAttributesExtractor( @@ -29,7 +29,7 @@ public OpenTelemetryFilter() { new DubboClientNetworkAttributesGetter(), AgentCommonConfig.get().getPeerServiceResolver())) .build() - .newFilter(); + .newClientFilter(); } @Override diff --git a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java new file mode 100644 index 000000000000..0ac0fc17c836 --- /dev/null +++ b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java @@ -0,0 +1,39 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.instrumentation.apachedubbo.v2_7.DubboTelemetry; +import io.opentelemetry.instrumentation.apachedubbo.v2_7.internal.DubboClientNetworkAttributesGetter; +import io.opentelemetry.instrumentation.api.incubator.semconv.net.PeerServiceAttributesExtractor; +import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig; +import org.apache.dubbo.common.extension.Activate; +import org.apache.dubbo.rpc.Filter; +import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.Result; + +@Activate(group = {"provider"}) +public class OpenTelemetryServerFilter implements Filter { + + private final Filter delegate; + + public OpenTelemetryServerFilter() { + delegate = + DubboTelemetry.builder(GlobalOpenTelemetry.get()) + .addAttributesExtractor( + PeerServiceAttributesExtractor.create( + new DubboClientNetworkAttributesGetter(), + AgentCommonConfig.get().getPeerServiceResolver())) + .build() + .newServerFilter(); + } + + @Override + public Result invoke(Invoker invoker, Invocation invocation) { + return delegate.invoke(invoker, invocation); + } +} diff --git a/instrumentation/apache-dubbo-2.7/javaagent/src/main/resources/apache-dubbo-2.7/META-INF/org.apache.dubbo.rpc.Filter b/instrumentation/apache-dubbo-2.7/javaagent/src/main/resources/apache-dubbo-2.7/META-INF/org.apache.dubbo.rpc.Filter index c4d39b0b344f..f76248783a71 100644 --- a/instrumentation/apache-dubbo-2.7/javaagent/src/main/resources/apache-dubbo-2.7/META-INF/org.apache.dubbo.rpc.Filter +++ b/instrumentation/apache-dubbo-2.7/javaagent/src/main/resources/apache-dubbo-2.7/META-INF/org.apache.dubbo.rpc.Filter @@ -1 +1,2 @@ -io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7.OpenTelemetryFilter \ No newline at end of file +io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7.OpenTelemetryClientFilter +io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7.OpenTelemetryServerFilter diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java index 982ab442d27f..a59b81d83c5c 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java @@ -26,6 +26,7 @@ public static DubboTelemetryBuilder builder(OpenTelemetry openTelemetry) { } private final Instrumenter serverInstrumenter; + private final Instrumenter clientInstrumenter; DubboTelemetry( @@ -35,8 +36,13 @@ public static DubboTelemetryBuilder builder(OpenTelemetry openTelemetry) { this.clientInstrumenter = clientInstrumenter; } - /** Returns a new Dubbo {@link Filter} that traces Dubbo RPC invocations. */ - public Filter newFilter() { - return new TracingFilter(serverInstrumenter, clientInstrumenter); + /** Returns a new Dubbo client {@link Filter} that traces Dubbo RPC invocations. */ + public Filter newClientFilter() { + return new TracingFilter(clientInstrumenter, true); + } + + /** Returns a new Dubbo server {@link Filter} that traces Dubbo RPC invocations. */ + public Filter newServerFilter() { + return new TracingFilter(serverInstrumenter, false); } } diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/OpenTelemetryFilter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java similarity index 80% rename from instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/OpenTelemetryFilter.java rename to instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java index 8cf99f56d89a..49f480429c88 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/OpenTelemetryFilter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java @@ -12,13 +12,13 @@ import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Result; -@Activate(group = {"consumer", "provider"}) -public final class OpenTelemetryFilter implements Filter { +@Activate(group = {"consumer"}) +public final class OpenTelemetryClientFilter implements Filter { private final Filter delegate; - public OpenTelemetryFilter() { - delegate = DubboTelemetry.create(GlobalOpenTelemetry.get()).newFilter(); + public OpenTelemetryClientFilter() { + delegate = DubboTelemetry.create(GlobalOpenTelemetry.get()).newClientFilter(); } @Override diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java new file mode 100644 index 000000000000..06e8fd402395 --- /dev/null +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java @@ -0,0 +1,28 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.apachedubbo.v2_7; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import org.apache.dubbo.common.extension.Activate; +import org.apache.dubbo.rpc.Filter; +import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.Result; + +@Activate(group = {"provider"}) +public final class OpenTelemetryServerFilter implements Filter { + + private final Filter delegate; + + public OpenTelemetryServerFilter() { + delegate = DubboTelemetry.create(GlobalOpenTelemetry.get()).newServerFilter(); + } + + @Override + public Result invoke(Invoker invoker, Invocation invocation) { + return delegate.invoke(invoker, invocation); + } +} diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java index d3d239096bb1..36fe35c678cc 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java @@ -18,14 +18,12 @@ final class TracingFilter implements Filter { - private final Instrumenter serverInstrumenter; - private final Instrumenter clientInstrumenter; + private final Instrumenter instrumenter; + private final boolean isClient; - TracingFilter( - Instrumenter serverInstrumenter, - Instrumenter clientInstrumenter) { - this.serverInstrumenter = serverInstrumenter; - this.clientInstrumenter = clientInstrumenter; + TracingFilter(Instrumenter instrumenter, boolean isClient) { + this.instrumenter = instrumenter; + this.isClient = isClient; } @Override @@ -40,9 +38,6 @@ public Result invoke(Invoker invoker, Invocation invocation) { return invoker.invoke(invocation); } - boolean isServer = rpcContext.isProviderSide(); - Instrumenter instrumenter = - isServer ? serverInstrumenter : clientInstrumenter; Context parentContext = Context.current(); DubboRequest request = DubboRequest.create((RpcInvocation) invocation, rpcContext); @@ -55,7 +50,7 @@ public Result invoke(Invoker invoker, Invocation invocation) { boolean isSynchronous = true; try (Scope ignored = context.makeCurrent()) { result = invoker.invoke(invocation); - if (!isServer) { + if (isClient) { CompletableFuture future = rpcContext.getCompletableFuture(); if (future != null) { isSynchronous = false; diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/resources/META-INF/services/org.apache.dubbo.rpc.Filter b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/resources/META-INF/services/org.apache.dubbo.rpc.Filter index 42297d5caab4..250516c0dc0b 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/resources/META-INF/services/org.apache.dubbo.rpc.Filter +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/resources/META-INF/services/org.apache.dubbo.rpc.Filter @@ -1 +1,2 @@ -io.opentelemetry.instrumentation.apachedubbo.v2_7.OpenTelemetryFilter \ No newline at end of file +io.opentelemetry.instrumentation.apachedubbo.v2_7.OpenTelemetryClientFilter +io.opentelemetry.instrumentation.apachedubbo.v2_7.OpenTelemetryServerFilter From 92583fcba51a58a326d0e073e7ef91dd71fe4734 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Mon, 23 Dec 2024 23:44:43 +0800 Subject: [PATCH 2/5] Address comment problems --- .../apachedubbo/v2_7/OpenTelemetryClientFilter.java | 2 +- .../apachedubbo/v2_7/OpenTelemetryServerFilter.java | 2 +- .../instrumentation/apachedubbo/v2_7/DubboTelemetry.java | 7 +++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java index 5680dd8860bf..d2dc6fb3cb46 100644 --- a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java +++ b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java @@ -17,7 +17,7 @@ import org.apache.dubbo.rpc.Result; @Activate(group = {"consumer"}) -public class OpenTelemetryClientFilter implements Filter { +public final class OpenTelemetryClientFilter implements Filter { private final Filter delegate; diff --git a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java index 0ac0fc17c836..a37be1fa7d2b 100644 --- a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java +++ b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java @@ -17,7 +17,7 @@ import org.apache.dubbo.rpc.Result; @Activate(group = {"provider"}) -public class OpenTelemetryServerFilter implements Filter { +public final class OpenTelemetryServerFilter implements Filter { private final Filter delegate; diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java index a59b81d83c5c..5319f496b4a6 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java @@ -13,6 +13,9 @@ /** Entrypoint for instrumenting Apache Dubbo servers and clients. */ public final class DubboTelemetry { + private final Instrumenter serverInstrumenter; + private final Instrumenter clientInstrumenter; + /** Returns a new {@link DubboTelemetry} configured with the given {@link OpenTelemetry}. */ public static DubboTelemetry create(OpenTelemetry openTelemetry) { return builder(openTelemetry).build(); @@ -25,10 +28,6 @@ public static DubboTelemetryBuilder builder(OpenTelemetry openTelemetry) { return new DubboTelemetryBuilder(openTelemetry); } - private final Instrumenter serverInstrumenter; - - private final Instrumenter clientInstrumenter; - DubboTelemetry( Instrumenter serverInstrumenter, Instrumenter clientInstrumenter) { From f5fafc049b46b52e17ba12197e238cb84402f4fd Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Wed, 25 Dec 2024 20:19:07 +0800 Subject: [PATCH 3/5] Keep old api and mark it as deprecated --- .../apachedubbo/v2_7/DubboTelemetry.java | 14 +++++++++-- .../apachedubbo/v2_7/TracingFilter.java | 23 ++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java index 5319f496b4a6..948d4a9eabbe 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java @@ -35,13 +35,23 @@ public static DubboTelemetryBuilder builder(OpenTelemetry openTelemetry) { this.clientInstrumenter = clientInstrumenter; } + /** + * Returns a new Dubbo {@link Filter} that traces Dubbo RPC invocations. + * + *

Use {@link #newClientFilter} and {@link #newServerFilter} instead. + */ + @Deprecated + public Filter newFilter() { + return new TracingFilter(serverInstrumenter, clientInstrumenter); + } + /** Returns a new Dubbo client {@link Filter} that traces Dubbo RPC invocations. */ public Filter newClientFilter() { - return new TracingFilter(clientInstrumenter, true); + return new TracingFilter(clientInstrumenter, null); } /** Returns a new Dubbo server {@link Filter} that traces Dubbo RPC invocations. */ public Filter newServerFilter() { - return new TracingFilter(serverInstrumenter, false); + return new TracingFilter(null, serverInstrumenter); } } diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java index 36fe35c678cc..366970e50d5a 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java @@ -18,12 +18,14 @@ final class TracingFilter implements Filter { - private final Instrumenter instrumenter; - private final boolean isClient; + private final Instrumenter clientInstrumenter; + private final Instrumenter serverInstrumenter; - TracingFilter(Instrumenter instrumenter, boolean isClient) { - this.instrumenter = instrumenter; - this.isClient = isClient; + TracingFilter( + Instrumenter clientInstrumenter, + Instrumenter serverInstrumenter) { + this.clientInstrumenter = clientInstrumenter; + this.serverInstrumenter = serverInstrumenter; } @Override @@ -38,6 +40,15 @@ public Result invoke(Invoker invoker, Invocation invocation) { return invoker.invoke(invocation); } + Instrumenter instrumenter; + boolean isServer = rpcContext.isProviderSide(); + if (this.clientInstrumenter != null && this.serverInstrumenter != null) { + instrumenter = isServer ? serverInstrumenter : clientInstrumenter; + } else { + instrumenter = + this.clientInstrumenter != null ? this.clientInstrumenter : this.serverInstrumenter; + } + Context parentContext = Context.current(); DubboRequest request = DubboRequest.create((RpcInvocation) invocation, rpcContext); @@ -50,7 +61,7 @@ public Result invoke(Invoker invoker, Invocation invocation) { boolean isSynchronous = true; try (Scope ignored = context.makeCurrent()) { result = invoker.invoke(invocation); - if (isClient) { + if (instrumenter == this.clientInstrumenter) { CompletableFuture future = rpcContext.getCompletableFuture(); if (future != null) { isSynchronous = false; From 93f2ffb8e813344fde56060ba3f7f2f50772c149 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Wed, 25 Dec 2024 20:40:44 +0800 Subject: [PATCH 4/5] Optimize codes --- .../instrumentation/apachedubbo/v2_7/TracingFilter.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java index 366970e50d5a..40e27fc7967f 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java @@ -41,12 +41,10 @@ public Result invoke(Invoker invoker, Invocation invocation) { } Instrumenter instrumenter; - boolean isServer = rpcContext.isProviderSide(); - if (this.clientInstrumenter != null && this.serverInstrumenter != null) { - instrumenter = isServer ? serverInstrumenter : clientInstrumenter; + if (clientInstrumenter != null && serverInstrumenter != null) { + instrumenter = rpcContext.isProviderSide() ? serverInstrumenter : clientInstrumenter; } else { - instrumenter = - this.clientInstrumenter != null ? this.clientInstrumenter : this.serverInstrumenter; + instrumenter = clientInstrumenter != null ? clientInstrumenter : serverInstrumenter; } Context parentContext = Context.current(); From 4607f4a950d9dae321a5e727ba771acc819fcd4b Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Mon, 30 Dec 2024 15:49:50 +0200 Subject: [PATCH 5/5] Rework dubbo insrumentation (#7) --- .../apachedubbo/v2_7/DubboSingletons.java | 32 +++++++++ .../v2_7/OpenTelemetryClientFilter.java | 14 +--- .../v2_7/OpenTelemetryServerFilter.java | 14 +--- .../apachedubbo/v2_7/DubboTelemetry.java | 8 +-- .../apachedubbo/v2_7/TracingFilter.java | 70 ++++++++++++++----- 5 files changed, 92 insertions(+), 46 deletions(-) create mode 100644 instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboSingletons.java diff --git a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboSingletons.java b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboSingletons.java new file mode 100644 index 000000000000..034d739dc82c --- /dev/null +++ b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboSingletons.java @@ -0,0 +1,32 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.instrumentation.apachedubbo.v2_7.DubboTelemetry; +import io.opentelemetry.instrumentation.apachedubbo.v2_7.internal.DubboClientNetworkAttributesGetter; +import io.opentelemetry.instrumentation.api.incubator.semconv.net.PeerServiceAttributesExtractor; +import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig; +import org.apache.dubbo.rpc.Filter; + +public final class DubboSingletons { + public static final Filter CLIENT_FILTER; + public static final Filter SERVER_FILTER; + + static { + DubboTelemetry telemetry = + DubboTelemetry.builder(GlobalOpenTelemetry.get()) + .addAttributesExtractor( + PeerServiceAttributesExtractor.create( + new DubboClientNetworkAttributesGetter(), + AgentCommonConfig.get().getPeerServiceResolver())) + .build(); + CLIENT_FILTER = telemetry.newClientFilter(); + SERVER_FILTER = telemetry.newServerFilter(); + } + + private DubboSingletons() {} +} diff --git a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java index d2dc6fb3cb46..03e5ce6a2183 100644 --- a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java +++ b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryClientFilter.java @@ -5,11 +5,6 @@ package io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7; -import io.opentelemetry.api.GlobalOpenTelemetry; -import io.opentelemetry.instrumentation.apachedubbo.v2_7.DubboTelemetry; -import io.opentelemetry.instrumentation.apachedubbo.v2_7.internal.DubboClientNetworkAttributesGetter; -import io.opentelemetry.instrumentation.api.incubator.semconv.net.PeerServiceAttributesExtractor; -import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig; import org.apache.dubbo.common.extension.Activate; import org.apache.dubbo.rpc.Filter; import org.apache.dubbo.rpc.Invocation; @@ -22,14 +17,7 @@ public final class OpenTelemetryClientFilter implements Filter { private final Filter delegate; public OpenTelemetryClientFilter() { - delegate = - DubboTelemetry.builder(GlobalOpenTelemetry.get()) - .addAttributesExtractor( - PeerServiceAttributesExtractor.create( - new DubboClientNetworkAttributesGetter(), - AgentCommonConfig.get().getPeerServiceResolver())) - .build() - .newClientFilter(); + delegate = DubboSingletons.CLIENT_FILTER; } @Override diff --git a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java index a37be1fa7d2b..f9d7065815c5 100644 --- a/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java +++ b/instrumentation/apache-dubbo-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/OpenTelemetryServerFilter.java @@ -5,11 +5,6 @@ package io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7; -import io.opentelemetry.api.GlobalOpenTelemetry; -import io.opentelemetry.instrumentation.apachedubbo.v2_7.DubboTelemetry; -import io.opentelemetry.instrumentation.apachedubbo.v2_7.internal.DubboClientNetworkAttributesGetter; -import io.opentelemetry.instrumentation.api.incubator.semconv.net.PeerServiceAttributesExtractor; -import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig; import org.apache.dubbo.common.extension.Activate; import org.apache.dubbo.rpc.Filter; import org.apache.dubbo.rpc.Invocation; @@ -22,14 +17,7 @@ public final class OpenTelemetryServerFilter implements Filter { private final Filter delegate; public OpenTelemetryServerFilter() { - delegate = - DubboTelemetry.builder(GlobalOpenTelemetry.get()) - .addAttributesExtractor( - PeerServiceAttributesExtractor.create( - new DubboClientNetworkAttributesGetter(), - AgentCommonConfig.get().getPeerServiceResolver())) - .build() - .newServerFilter(); + delegate = DubboSingletons.SERVER_FILTER; } @Override diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java index 948d4a9eabbe..a44489af3463 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/DubboTelemetry.java @@ -38,20 +38,20 @@ public static DubboTelemetryBuilder builder(OpenTelemetry openTelemetry) { /** * Returns a new Dubbo {@link Filter} that traces Dubbo RPC invocations. * - *

Use {@link #newClientFilter} and {@link #newServerFilter} instead. + * @deprecated Use {@link #newClientFilter} and {@link #newServerFilter} instead. */ @Deprecated public Filter newFilter() { - return new TracingFilter(serverInstrumenter, clientInstrumenter); + return TracingFilter.newFilter(serverInstrumenter, clientInstrumenter); } /** Returns a new Dubbo client {@link Filter} that traces Dubbo RPC invocations. */ public Filter newClientFilter() { - return new TracingFilter(clientInstrumenter, null); + return TracingFilter.newClientFilter(clientInstrumenter); } /** Returns a new Dubbo server {@link Filter} that traces Dubbo RPC invocations. */ public Filter newServerFilter() { - return new TracingFilter(null, serverInstrumenter); + return TracingFilter.newServerFilter(serverInstrumenter); } } diff --git a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java index 40e27fc7967f..feced63c0bf0 100644 --- a/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java +++ b/instrumentation/apache-dubbo-2.7/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/TracingFilter.java @@ -18,14 +18,52 @@ final class TracingFilter implements Filter { - private final Instrumenter clientInstrumenter; - private final Instrumenter serverInstrumenter; - - TracingFilter( - Instrumenter clientInstrumenter, - Instrumenter serverInstrumenter) { - this.clientInstrumenter = clientInstrumenter; - this.serverInstrumenter = serverInstrumenter; + private final InstrumenterSupplier instrumenterSupplier; + + private TracingFilter(InstrumenterSupplier instrumenterSupplier) { + this.instrumenterSupplier = instrumenterSupplier; + } + + static TracingFilter newClientFilter(Instrumenter clientInstrumenter) { + return newFilter(clientInstrumenter, true); + } + + static TracingFilter newServerFilter(Instrumenter serverInstrumenter) { + return newFilter(serverInstrumenter, false); + } + + private static TracingFilter newFilter( + Instrumenter instrumenter, boolean isClientSide) { + return new TracingFilter( + new InstrumenterSupplier() { + + @Override + public Instrumenter get(RpcContext rpcContext) { + return instrumenter; + } + + @Override + public boolean isClientSide(RpcContext rpcContext) { + return isClientSide; + } + }); + } + + static TracingFilter newFilter( + Instrumenter serverInstrumenter, + Instrumenter clientInstrumenter) { + return new TracingFilter( + new InstrumenterSupplier() { + @Override + public Instrumenter get(RpcContext rpcContext) { + return rpcContext.isConsumerSide() ? clientInstrumenter : serverInstrumenter; + } + + @Override + public boolean isClientSide(RpcContext rpcContext) { + return rpcContext.isConsumerSide(); + } + }); } @Override @@ -40,13 +78,7 @@ public Result invoke(Invoker invoker, Invocation invocation) { return invoker.invoke(invocation); } - Instrumenter instrumenter; - if (clientInstrumenter != null && serverInstrumenter != null) { - instrumenter = rpcContext.isProviderSide() ? serverInstrumenter : clientInstrumenter; - } else { - instrumenter = clientInstrumenter != null ? clientInstrumenter : serverInstrumenter; - } - + Instrumenter instrumenter = instrumenterSupplier.get(rpcContext); Context parentContext = Context.current(); DubboRequest request = DubboRequest.create((RpcInvocation) invocation, rpcContext); @@ -59,7 +91,7 @@ public Result invoke(Invoker invoker, Invocation invocation) { boolean isSynchronous = true; try (Scope ignored = context.makeCurrent()) { result = invoker.invoke(invocation); - if (instrumenter == this.clientInstrumenter) { + if (instrumenterSupplier.isClientSide(rpcContext)) { CompletableFuture future = rpcContext.getCompletableFuture(); if (future != null) { isSynchronous = false; @@ -76,4 +108,10 @@ public Result invoke(Invoker invoker, Invocation invocation) { } return result; } + + private interface InstrumenterSupplier { + Instrumenter get(RpcContext rpcContext); + + boolean isClientSide(RpcContext rpcContext); + } }