-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Retrieve dubbo server.address/server.port according to latest SemConv #17244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
bd9f76c
Add registry address capture for Dubbo
steverao 5a2ec62
Merge branch 'main' into dubbo-client-lb
steverao b390c63
Apply spotless
steverao bcd8629
Refactor registry address capturing
steverao e974692
Add integration tests for Dubbo registry mode and enhance registry ad…
steverao 2646ed4
Fix failing test
steverao e2c19d0
Optimize code
steverao 379318f
Address review comments
steverao 60dfe35
Merge branch 'main' into dubbo-client-lb
steverao 26387cc
feat: add service peer name handling in registry tests
steverao 42bdec0
Address review comments
steverao b11d3e7
Address review comments
steverao 83c1830
Merge remote-tracking branch 'upstream/main' into dubbo-client-lb
trask 0800ea0
Simlifications (#13)
trask c01ecef
Fix Dubbo cluster wrapper across join signatures
trask 1202b5c
revert a bit more
trask 8479808
refactor: remove unnecessary newline in RegistryCapturingClusterWrapper
steverao 7979c05
Update instrumentation/apache-dubbo-2.7/library-autoconfigure/src/mai…
steverao dfc781d
Update instrumentation/apache-dubbo-2.7/testing/build.gradle.kts
steverao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...etry/javaagent/instrumentation/apachedubbo/v2_7/RegistryCapturingClusterWrapperProxy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7; | ||
|
|
||
| import io.opentelemetry.instrumentation.apachedubbo.v2_7.internal.RegistryCapturingClusterWrapper; | ||
| import org.apache.dubbo.rpc.Invoker; | ||
| import org.apache.dubbo.rpc.cluster.Cluster; | ||
| import org.apache.dubbo.rpc.cluster.Directory; | ||
|
|
||
| public final class RegistryCapturingClusterWrapperProxy implements Cluster { | ||
|
|
||
| private final RegistryCapturingClusterWrapper delegate; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public RegistryCapturingClusterWrapperProxy(Cluster cluster) { | ||
| this.delegate = new RegistryCapturingClusterWrapper(cluster); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> Invoker<T> join(Directory<T> directory) { | ||
| return delegate.join(directory); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public <T> Invoker<T> join(Directory<T> directory, boolean buildFilterChain) { | ||
| return delegate.join(directory, buildFilterChain); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
...rc/main/resources/apache-dubbo-2.7/META-INF/services/org.apache.dubbo.rpc.cluster.Cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7.RegistryCapturingClusterWrapperProxy |
22 changes: 22 additions & 0 deletions
22
...a/io/opentelemetry/javaagent/instrumentation/apachedubbo/v2_7/DubboAgentRegistryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.apachedubbo.v2_7; | ||
|
|
||
| import io.opentelemetry.instrumentation.apachedubbo.v2_7.AbstractDubboRegistryTest; | ||
| import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
| import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| class DubboAgentRegistryTest extends AbstractDubboRegistryTest { | ||
|
|
||
| @RegisterExtension | ||
| static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
|
||
| @Override | ||
| protected InstrumentationExtension testing() { | ||
| return testing; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
...in/java/io/opentelemetry/instrumentation/apachedubbo/v2_7/internal/DubboRegistryUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.apachedubbo.v2_7.internal; | ||
|
|
||
| import java.lang.invoke.MethodHandle; | ||
| import java.lang.invoke.MethodHandles; | ||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.Method; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import javax.annotation.Nullable; | ||
| import org.apache.dubbo.common.URL; | ||
| import org.apache.dubbo.rpc.Invoker; | ||
| import org.apache.dubbo.rpc.RpcInvocation; | ||
| import org.apache.dubbo.rpc.cluster.Directory; | ||
|
|
||
| /** | ||
| * This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
| * any time. | ||
| */ | ||
| public final class DubboRegistryUtil { | ||
|
|
||
| private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); | ||
|
|
||
| private static final Map<Class<?>, Optional<MethodHandle>> DIRECTORY_ACCESSOR_CACHE = | ||
| new ConcurrentHashMap<>(); | ||
| private static final Map<Class<?>, Optional<MethodHandle>> REGISTRY_ACCESSOR_CACHE = | ||
| new ConcurrentHashMap<>(); | ||
| private static final Map<Class<?>, Optional<MethodHandle>> URL_ACCESSOR_CACHE = | ||
| new ConcurrentHashMap<>(); | ||
|
steverao marked this conversation as resolved.
Outdated
|
||
|
|
||
| private static final ThreadLocal<String> CAPTURED_REGISTRY_ADDRESS = new ThreadLocal<>(); | ||
|
|
||
| /** | ||
| * Used by {@link RegistryCapturingInvoker} while the cluster delegate runs (including into | ||
| * consumer protocol filters). | ||
| */ | ||
| static void pushCapturedRegistryAddress(String address) { | ||
| CAPTURED_REGISTRY_ADDRESS.set(address); | ||
| } | ||
|
|
||
| public static void clearCapturedRegistryAddress() { | ||
| CAPTURED_REGISTRY_ADDRESS.remove(); | ||
| } | ||
|
steverao marked this conversation as resolved.
|
||
|
|
||
| @Nullable | ||
| public static String extractRegistryAddress(RpcInvocation invocation) { | ||
| String captured = CAPTURED_REGISTRY_ADDRESS.get(); | ||
| if (captured != null) { | ||
| return captured; | ||
| } | ||
|
|
||
| Invoker<?> invoker = invocation.getInvoker(); | ||
| if (invoker == null) { | ||
| return null; | ||
| } | ||
|
|
||
| Directory<?> directory = getDirectory(invoker); | ||
| if (directory != null) { | ||
| String address = tryExtractRegistryAddressFromDirectory(directory); | ||
| if (address != null) { | ||
| return address; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves {@code protocol://host:port} from a registry-backed directory (for example {@code | ||
| * RegistryDirectory}), using {@code getRegistry()} when present and otherwise the {@code | ||
| * registry} field. Called once per consumer refer when {@link RegistryCapturingClusterWrapper} | ||
| * wraps the cluster invoker. | ||
| */ | ||
| @Nullable | ||
| public static String tryExtractRegistryAddressFromDirectory(Directory<?> directory) { | ||
| return extractRegistryAddressFromDirectory(directory); | ||
| } | ||
|
steverao marked this conversation as resolved.
Outdated
|
||
|
|
||
| public static String buildServiceTarget(URL url) { | ||
| String interfaceName = url.getServiceInterface(); | ||
| if (interfaceName == null || interfaceName.isEmpty()) { | ||
| interfaceName = url.getPath(); | ||
| } | ||
| if (interfaceName == null || interfaceName.isEmpty()) { | ||
| return ""; | ||
| } | ||
|
|
||
| String version = url.getParameter("version"); | ||
| String group = url.getParameter("group"); | ||
| boolean hasVersion = version != null && !version.isEmpty(); | ||
| boolean hasGroup = group != null && !group.isEmpty(); | ||
|
|
||
| if (!hasVersion && !hasGroup) { | ||
| return interfaceName; | ||
| } | ||
|
|
||
| StringBuilder sb = new StringBuilder(interfaceName); | ||
| sb.append(':'); | ||
| if (hasVersion) { | ||
| sb.append(version); | ||
| } | ||
| if (hasGroup) { | ||
| sb.append(':').append(group); | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| @Nullable | ||
| private static Directory<?> getDirectory(Invoker<?> invoker) { | ||
| MethodHandle mh = | ||
| findAccessor(invoker.getClass(), "getDirectory", "directory", DIRECTORY_ACCESSOR_CACHE); | ||
| if (mh == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| Object obj = mh.invoke(invoker); | ||
| return obj instanceof Directory ? (Directory<?>) obj : null; | ||
| } catch (Throwable t) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| private static String extractRegistryAddressFromDirectory(Directory<?> directory) { | ||
| MethodHandle getRegistry = | ||
| findAccessor(directory.getClass(), "getRegistry", "registry", REGISTRY_ACCESSOR_CACHE); | ||
| if (getRegistry == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| Object registry = getRegistry.invoke(directory); | ||
| if (registry == null) { | ||
| return null; | ||
| } | ||
| MethodHandle getUrl = findAccessor(registry.getClass(), "getUrl", null, URL_ACCESSOR_CACHE); | ||
| if (getUrl == null) { | ||
| return null; | ||
| } | ||
| Object urlObj = getUrl.invoke(registry); | ||
| if (!(urlObj instanceof URL)) { | ||
| return null; | ||
| } | ||
| URL url = (URL) urlObj; | ||
| return url.getProtocol() + "://" + url.getAddress(); | ||
| } catch (Throwable t) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| private static MethodHandle findAccessor( | ||
| Class<?> clazz, | ||
| String methodName, | ||
| @Nullable String fieldName, | ||
| Map<Class<?>, Optional<MethodHandle>> cache) { | ||
| return cache | ||
| .computeIfAbsent( | ||
| clazz, | ||
| cls -> { | ||
| MethodHandle mh = resolveMethod(cls, methodName); | ||
| if (mh != null) { | ||
| return Optional.of(mh); | ||
| } | ||
| if (fieldName != null) { | ||
| mh = resolveField(cls, fieldName); | ||
| if (mh != null) { | ||
| return Optional.of(mh); | ||
| } | ||
| } | ||
| return Optional.empty(); | ||
| }) | ||
| .orElse(null); | ||
| } | ||
|
|
||
| @Nullable | ||
| private static MethodHandle resolveMethod(Class<?> clazz, String name) { | ||
| try { | ||
| Method m = clazz.getMethod(name); | ||
| m.setAccessible(true); | ||
| return LOOKUP.unreflect(m); | ||
| } catch (NoSuchMethodException | IllegalAccessException e) { | ||
| // ignore | ||
|
steverao marked this conversation as resolved.
Outdated
|
||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Nullable | ||
| private static MethodHandle resolveField(Class<?> clazz, String name) { | ||
| for (Class<?> c = clazz; c != null && c != Object.class; c = c.getSuperclass()) { | ||
| try { | ||
| Field f = c.getDeclaredField(name); | ||
| f.setAccessible(true); | ||
| return LOOKUP.unreflectGetter(f); | ||
| } catch (NoSuchFieldException | IllegalAccessException e) { | ||
| // ignore | ||
|
steverao marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private DubboRegistryUtil() {} | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.