Skip to content

Introduce cache for peer.hostname lookup #8601

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 2 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import datadog.trace.api.Config;
import datadog.trace.api.DDTags;
import datadog.trace.api.Functions;
import datadog.trace.api.cache.DDCache;
import datadog.trace.api.cache.DDCaches;
import datadog.trace.api.cache.QualifiedClassNameCache;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
Expand Down Expand Up @@ -37,6 +39,8 @@ public String apply(Class<?> clazz) {
},
Functions.PrefixJoin.of("."));

private static final DDCache<String, String> HOSTNAME_CACHE = DDCaches.newFixedSizeCache(64);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dougqh do you have any thoughts on sizing this cache?


protected final boolean traceAnalyticsEnabled;
protected final Double traceAnalyticsSampleRate;

Expand Down Expand Up @@ -114,13 +118,14 @@ public AgentSpan onPeerConnection(final AgentSpan span, final InetAddress remote

public AgentSpan onPeerConnection(AgentSpan span, InetAddress remoteAddress, boolean resolved) {
if (remoteAddress != null) {
if (resolved) {
span.setTag(Tags.PEER_HOSTNAME, remoteAddress.getHostName());
String ip = remoteAddress.getHostAddress();
if (resolved && Config.get().isPeerHostNameEnabled()) {
span.setTag(Tags.PEER_HOSTNAME, hostName(remoteAddress, ip));
}
if (remoteAddress instanceof Inet4Address) {
span.setTag(Tags.PEER_HOST_IPV4, remoteAddress.getHostAddress());
span.setTag(Tags.PEER_HOST_IPV4, ip);
} else if (remoteAddress instanceof Inet6Address) {
span.setTag(Tags.PEER_HOST_IPV6, remoteAddress.getHostAddress());
span.setTag(Tags.PEER_HOST_IPV6, ip);
}
}
return span;
Expand Down Expand Up @@ -187,4 +192,11 @@ public CharSequence className(final Class<?> clazz) {
String simpleName = clazz.getSimpleName();
return simpleName.isEmpty() ? CLASS_NAMES.getClassName(clazz) : simpleName;
}

private static String hostName(InetAddress remoteAddress, String ip) {
if (null != ip) {
return HOSTNAME_CACHE.computeIfAbsent(ip, _ip -> remoteAddress.getHostName());
}
return remoteAddress.getHostName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,13 @@ public final class TracerConfig {
"trace.experimental.long-running.initial.flush.interval";
public static final String TRACE_LONG_RUNNING_FLUSH_INTERVAL =
"trace.experimental.long-running.flush.interval";

public static final String TRACE_PEER_HOSTNAME_ENABLED = "trace.peer.hostname.enabled";

public static final String TRACE_PEER_SERVICE_DEFAULTS_ENABLED =
"trace.peer.service.defaults.enabled";

public static final String TRACE_PEER_SERVICE_COMPONENT_OVERRIDES =
"trace.peer.service.component.overrides";

public static final String TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED =
"trace.remove.integration-service-names.enabled";

Expand Down
9 changes: 9 additions & 0 deletions internal-api/src/main/java/datadog/trace/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public static String getHostName() {
private final String prioritySamplingForce;
private final boolean traceResolverEnabled;
private final int spanAttributeSchemaVersion;
private final boolean peerHostNameEnabled;
private final boolean peerServiceDefaultsEnabled;
private final Map<String, String> peerServiceComponentOverrides;
private final boolean removeIntegrationServiceNamesEnabled;
Expand Down Expand Up @@ -827,6 +828,8 @@ private Config(final ConfigProvider configProvider, final InstrumenterConfig ins

spanAttributeSchemaVersion = schemaVersionFromConfig();

peerHostNameEnabled = configProvider.getBoolean(TRACE_PEER_HOSTNAME_ENABLED, true);

// following two only used in v0.
// in v1+ defaults are always calculated regardless this feature flag
peerServiceDefaultsEnabled =
Expand Down Expand Up @@ -2167,6 +2170,10 @@ public int getSpanAttributeSchemaVersion() {
return spanAttributeSchemaVersion;
}

public boolean isPeerHostNameEnabled() {
return peerHostNameEnabled;
}

public boolean isPeerServiceDefaultsEnabled() {
return peerServiceDefaultsEnabled;
}
Expand Down Expand Up @@ -4820,6 +4827,8 @@ public String toString() {
+ jaxRsExceptionAsErrorsEnabled
+ ", axisPromoteResourceName="
+ axisPromoteResourceName
+ ", peerHostNameEnabled="
+ peerHostNameEnabled
+ ", peerServiceDefaultsEnabled="
+ peerServiceDefaultsEnabled
+ ", peerServiceComponentOverrides="
Expand Down