Merge pull request #927 from DataDog/better-hostname-detection

Better hostname detection
This commit is contained in:
Laplie Anderson 2019-07-22 16:05:11 -04:00 committed by GitHub
commit e19d402926
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 6 deletions

View File

@ -1,9 +1,11 @@
package datadog.trace.api; package datadog.trace.api;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Arrays; import java.util.Arrays;
@ -885,7 +887,7 @@ public class Config {
* exist or if it is in a wrong format. * exist or if it is in a wrong format.
*/ */
private static Properties loadConfigurationFile() { private static Properties loadConfigurationFile() {
Properties properties = new Properties(); final Properties properties = new Properties();
// Reading from system property first and from env after // Reading from system property first and from env after
String configurationFilePath = String configurationFilePath =
@ -922,11 +924,38 @@ public class Config {
return properties; return properties;
} }
/** /** Returns the detected hostname. First tries locally, then using DNS */
* Returns the detected hostname. This operation is time consuming so if the usage changes and
* this method will be called several times then we should implement some sort of caching.
*/
private String getHostName() { private String getHostName() {
String possibleHostname = null;
// Try environment variable. This works in almost all environments
if (System.getProperty("os.name").startsWith("Windows")) {
possibleHostname = System.getenv("COMPUTERNAME");
} else {
possibleHostname = System.getenv("HOSTNAME");
}
if (possibleHostname != null && !possibleHostname.isEmpty()) {
log.debug("Determined hostname from environment variable");
return possibleHostname.trim();
}
// Try hostname command
try {
final Process process = Runtime.getRuntime().exec("hostname");
final BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
possibleHostname = reader.readLine();
} catch (final Exception e) {
// Ignore. Hostname command is not always available
}
if (possibleHostname != null && !possibleHostname.isEmpty()) {
log.debug("Determined hostname from hostname command");
return possibleHostname.trim();
}
// From DNS
try { try {
return InetAddress.getLocalHost().getHostName(); return InetAddress.getLocalHost().getHostName();
} catch (final UnknownHostException e) { } catch (final UnknownHostException e) {

View File

@ -724,7 +724,7 @@ class ConfigTest extends Specification {
def config = Config.get(properties) def config = Config.get(properties)
then: then:
config.localRootSpanTags.get('_dd.hostname') == InetAddress.localHost.hostName config.localRootSpanTags.containsKey('_dd.hostname')
} }
def "verify fallback to properties file"() { def "verify fallback to properties file"() {