-
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
Open
steverao
wants to merge
17
commits into
open-telemetry:main
Choose a base branch
from
steverao:dubbo-client-lb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 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 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 |
27 changes: 27 additions & 0 deletions
27
...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,27 @@ | ||
| /* | ||
| * 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; | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean hasServicePeerName() { | ||
| return true; | ||
| } | ||
| } |
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
199 changes: 199 additions & 0 deletions
199
...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,199 @@ | ||
| /* | ||
| * 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.Optional; | ||
| 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 ClassValue<Optional<MethodHandle>> DIRECTORY_ACCESSOR = | ||
| createAccessor("getDirectory", "directory"); | ||
| private static final ClassValue<Optional<MethodHandle>> REGISTRY_ACCESSOR = | ||
| createAccessor("getRegistry", "registry"); | ||
| private static final ClassValue<Optional<MethodHandle>> URL_ACCESSOR = | ||
| createAccessor("getUrl", null); | ||
|
|
||
| private static final ThreadLocal<String> CAPTURED_REGISTRY_ADDRESS = new ThreadLocal<>(); | ||
|
|
||
| /** | ||
| * Used by {@link RegistryCapturingInvoker} while the cluster delegate runs (including into | ||
| * consumer protocol filters). Returns the previous value so callers can restore it. | ||
| */ | ||
| @Nullable | ||
| static String pushCapturedRegistryAddress(String address) { | ||
| String previous = CAPTURED_REGISTRY_ADDRESS.get(); | ||
| CAPTURED_REGISTRY_ADDRESS.set(address); | ||
| return previous; | ||
| } | ||
|
|
||
| static void restoreCapturedRegistryAddress(@Nullable String previous) { | ||
| if (previous == null) { | ||
| CAPTURED_REGISTRY_ADDRESS.remove(); | ||
| } else { | ||
| CAPTURED_REGISTRY_ADDRESS.set(previous); | ||
| } | ||
| } | ||
|
|
||
| @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; | ||
| } | ||
|
|
||
| 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 = DIRECTORY_ACCESSOR.get(invoker.getClass()).orElse(null); | ||
| if (mh == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| Object obj = mh.invoke(invoker); | ||
| return obj instanceof Directory ? (Directory<?>) obj : null; | ||
| } catch (Throwable t) { | ||
| 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 | ||
| static String tryExtractRegistryAddressFromDirectory(Directory<?> directory) { | ||
| MethodHandle getRegistry = REGISTRY_ACCESSOR.get(directory.getClass()).orElse(null); | ||
| if (getRegistry == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| Object registry = getRegistry.invoke(directory); | ||
| if (registry == null) { | ||
| return null; | ||
| } | ||
| MethodHandle getUrl = URL_ACCESSOR.get(registry.getClass()).orElse(null); | ||
| 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; | ||
| } | ||
| } | ||
|
|
||
| private static ClassValue<Optional<MethodHandle>> createAccessor( | ||
| String methodName, @Nullable String fieldName) { | ||
| return new ClassValue<Optional<MethodHandle>>() { | ||
| @Override | ||
| protected Optional<MethodHandle> computeValue(Class<?> type) { | ||
| MethodHandle mh = resolveMethod(type, methodName); | ||
| if (mh != null) { | ||
| return Optional.of(mh); | ||
| } | ||
| if (fieldName != null) { | ||
| mh = resolveField(type, fieldName); | ||
| if (mh != null) { | ||
| return Optional.of(mh); | ||
| } | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Nullable | ||
| private static MethodHandle resolveMethod(Class<?> clazz, String name) { | ||
| try { | ||
| Method m = clazz.getMethod(name); | ||
| return LOOKUP.unreflect(m); | ||
| } catch (NoSuchMethodException | IllegalAccessException ignored) { | ||
| // ignore | ||
| } | ||
| 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 ignored) { | ||
| // ignore | ||
| } | ||
| } | ||
| 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.