Skip to content

Commit 6399fee

Browse files
committed
Remove caching of hostname detection in config class
1 parent 8b2b83b commit 6399fee

1 file changed

Lines changed: 12 additions & 18 deletions

File tree

  • dd-trace-api/src/main/java/datadog/trace/api

dd-trace-api/src/main/java/datadog/trace/api/Config.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,10 @@ private Config(final Properties properties, final Config parent) {
322322

323323
/** @return A map of tags to be applied only to the local application root span. */
324324
public Map<String, String> getLocalRootSpanTags() {
325-
final Map<String, String> result = newHashMap(reportHostName ? 1 : 0);
326-
result.putAll(getRuntimeTags());
325+
final Map<String, String> runtimeTags = getRuntimeTags();
326+
final Map<String, String> result =
327+
newHashMap(reportHostName ? (runtimeTags.size() + 1) : runtimeTags.size());
328+
result.putAll(runtimeTags);
327329
if (reportHostName) {
328330
result.put(INTERNAL_HOST_NAME, getHostname());
329331
}
@@ -662,26 +664,18 @@ private static <V extends Enum<V>> Set<V> convertStringSetToEnumSet(
662664
return Collections.unmodifiableSet(result);
663665
}
664666

665-
// Fields used to cache detected hostName which is a time consuming operation.
666-
private String hostName = null;
667-
private boolean hostNameDetected = false;
668-
669667
/**
670-
* Returns the detected hostname. This operation is time consuming and the first time this method
671-
* is called will take some time. Hostname is cached for subsequent calls.
668+
* Returns the detected hostname. This operation is time consuming so if the usage changes and
669+
* this method will be called several times then we should implement some sort of caching.
672670
*/
673-
public String getHostname() {
674-
if (!this.hostNameDetected) {
675-
try {
676-
this.hostName = InetAddress.getLocalHost().getHostName();
677-
} catch (UnknownHostException e) {
678-
// If we are not able to detect the hostname we do not throw an exception.
679-
} finally {
680-
this.hostNameDetected = true;
681-
}
671+
private String getHostname() {
672+
try {
673+
return InetAddress.getLocalHost().getHostName();
674+
} catch (UnknownHostException e) {
675+
// If we are not able to detect the hostname we do not throw an exception.
682676
}
683677

684-
return this.hostName;
678+
return null;
685679
}
686680

687681
// This has to be placed after all other static fields to give them a chance to initialize

0 commit comments

Comments
 (0)