-
Notifications
You must be signed in to change notification settings - Fork 315
Use resolved address for peer.hostname when available without hitting the cache #8915
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2b66505
Use resolved address for peer.hostname when available without hitting…
amarziali e45ff39
Update dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/boot…
amarziali 5bcf241
try to fork the test
amarziali c9b7da4
Open java.net for hostnameresolver tests
amarziali fef3bcd
Do not mock sealed classes with java 21
amarziali 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
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
68 changes: 68 additions & 0 deletions
68
...trap/src/main/java/datadog/trace/bootstrap/instrumentation/java/net/HostNameResolver.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,68 @@ | ||
| package datadog.trace.bootstrap.instrumentation.java.net; | ||
|
|
||
| import datadog.trace.api.cache.DDCache; | ||
| import datadog.trace.api.cache.DDCaches; | ||
| import datadog.trace.util.MethodHandles; | ||
| import java.lang.invoke.MethodHandle; | ||
| import java.net.InetAddress; | ||
|
|
||
| public final class HostNameResolver { | ||
| private static final MethodHandle HOLDER_GET; | ||
| private static final MethodHandle HOSTNAME_GET; | ||
|
|
||
| private static final DDCache<String, String> HOSTNAME_CACHE = DDCaches.newFixedSizeCache(64); | ||
|
|
||
| static { | ||
| MethodHandle holderTmp = null, hostnameTmp = null; | ||
| try { | ||
| final ClassLoader cl = HostNameResolver.class.getClassLoader(); | ||
| final MethodHandles methodHandles = new MethodHandles(cl); | ||
|
|
||
| final Class<?> holderClass = | ||
| Class.forName("java.net.InetAddress$InetAddressHolder", false, cl); | ||
| holderTmp = methodHandles.method(InetAddress.class, "holder"); | ||
| if (holderTmp != null) { | ||
| hostnameTmp = methodHandles.method(holderClass, "getHostName"); | ||
| } | ||
| } catch (Throwable ignored) { | ||
| holderTmp = null; | ||
| } finally { | ||
| if (holderTmp != null && hostnameTmp != null) { | ||
| HOLDER_GET = holderTmp; | ||
| HOSTNAME_GET = hostnameTmp; | ||
| } else { | ||
| HOLDER_GET = null; | ||
| HOSTNAME_GET = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private HostNameResolver() {} | ||
|
|
||
| static String getAlreadyResolvedHostName(InetAddress address) { | ||
| if (HOLDER_GET == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| final Object holder = HOLDER_GET.invoke(address); | ||
| return (String) HOSTNAME_GET.invoke(holder); | ||
| } catch (final Throwable ignored) { | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static String fromCache(InetAddress remoteAddress, String ip) { | ||
| if (null != ip) { | ||
| return HOSTNAME_CACHE.computeIfAbsent(ip, _ip -> remoteAddress.getHostName()); | ||
| } | ||
| return remoteAddress.getHostName(); | ||
| } | ||
|
|
||
| public static String hostName(InetAddress address, String ip) { | ||
| final String alreadyResolved = getAlreadyResolvedHostName(address); | ||
| if (alreadyResolved != null) { | ||
| return alreadyResolved; | ||
| } | ||
| return fromCache(address, ip); | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
...groovy/datadog/trace/bootstrap/instrumentation/java/net/HostNameResolverForkedTest.groovy
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,43 @@ | ||
| package datadog.trace.bootstrap.instrumentation.java.net | ||
|
|
||
| import datadog.trace.test.util.DDSpecification | ||
|
|
||
| class HostNameResolverForkedTest extends DDSpecification { | ||
| def "should directly get the hostname for already resolved address #address"() { | ||
| given: | ||
| def host = HostNameResolver.getAlreadyResolvedHostName(address) | ||
| expect: | ||
| host == expected | ||
| where: | ||
| address | expected | ||
| new Inet4Address("test", InetAddress.getLocalHost().getAddress()) | "test" | ||
| new Inet6Address("test", InetAddress.getLocalHost().getAddress()) | "test" | ||
| } | ||
|
|
||
| def "should return null when directly get the address for unresolved #address"() { | ||
| given: | ||
| def host = HostNameResolver.getAlreadyResolvedHostName(address) | ||
| expect: | ||
| host == null | ||
| where: | ||
| address | _ | ||
| InetAddress.getByAddress(InetAddress.getLocalHost().getAddress()) | _ | ||
| new Inet6Address(null, InetAddress.getLocalHost().getAddress()) | _ | ||
| } | ||
|
|
||
| def "should use the cache for unresolved addresses"() { | ||
| given: | ||
| def inet1 = new Inet4Address(null, InetAddress.getLocalHost().getAddress()) | ||
| def inet2 = new Inet4Address(null, 0) // this will fail if a resolution will happen | ||
| when: | ||
| def address = new InetSocketAddress(inet1, 666) | ||
| def host = HostNameResolver.hostName(address.getAddress(), "127.0.0.1") | ||
| then: | ||
| host != null | ||
| when: | ||
| address = new InetSocketAddress(inet2, 666) | ||
| def host2 = HostNameResolver.hostName(address.getAddress(), "127.0.0.1") | ||
| then: | ||
| host == host2 | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor suggestion, so the returns are aligned :)