Skip to content
Closed
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
109 changes: 75 additions & 34 deletions internal-api/src/main/java/datadog/trace/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -4191,62 +4191,103 @@ private static <T> Set<T> convertStringSetToSet(

/** Returns the detected hostname. First tries locally, then using DNS */
static String initHostName() {
String possibleHostname;

// Try environment variable. This works in almost all environments
if (isWindowsOS()) {
possibleHostname = getEnv("COMPUTERNAME");
} else {
possibleHostname = getEnv("HOSTNAME");
}

if (possibleHostname != null && !possibleHostname.isEmpty()) {
log.debug("Determined hostname from environment variable");
return possibleHostname.trim();
String possibleHostname = initHostNameFromEnv();
if (possibleHostname != null) {
return possibleHostname;
}

// Try hostname files
final String[] hostNameFiles = new String[] {"/proc/sys/kernel/hostname", "/etc/hostname"};
for (final String hostNameFile : hostNameFiles) {
try {
final Path hostNamePath = FileSystems.getDefault().getPath(hostNameFile);
if (Files.isRegularFile(hostNamePath)) {
byte[] bytes = Files.readAllBytes(hostNamePath);
possibleHostname = new String(bytes, StandardCharsets.ISO_8859_1);
}
} catch (Throwable t) {
// Ignore
}
possibleHostname = Strings.trim(possibleHostname);
if (!possibleHostname.isEmpty()) {
log.debug("Determined hostname from file {}", hostNameFile);
possibleHostname = initHostNameFromFile(hostNameFile);
if (possibleHostname != null) {
return possibleHostname;
}
}

// Try hostname command
possibleHostname = initHostNameFromCommand();
if (possibleHostname != null) {
return possibleHostname;
}

return initHostNameFromDNS();
}

private static String initHostNameFromEnv() {
String possibleHostname = null;
final String envVar = isWindowsOS() ? "COMPUTERNAME" : "HOSTNAME";
possibleHostname = getEnv(envVar);
if (possibleHostname == null) {
return null;
}
possibleHostname = possibleHostname.trim();
if (possibleHostname.isEmpty()) {
return null;
}
log.debug("Determined hostname from environment variable: {}", envVar);
return possibleHostname;
}

private static String initHostNameFromFile(final String fileName) {
String possibleHostname = null;
try {
final Path hostNamePath = FileSystems.getDefault().getPath(fileName);
if (Files.isRegularFile(hostNamePath)) {
byte[] bytes = Files.readAllBytes(hostNamePath);
possibleHostname = new String(bytes, StandardCharsets.ISO_8859_1);
}
} catch (Throwable t) {
return null;
}
if (possibleHostname == null) {
return null;
}
possibleHostname = possibleHostname.trim();
if (possibleHostname.isEmpty()) {
return null;
}
log.debug("Determined hostname from file {}", fileName);
return possibleHostname;
}

private static String initHostNameFromCommand() {
String possibleHostname = null;
try (final TraceScope scope = AgentTracer.get().muteTracing();
final BufferedReader reader =
new BufferedReader(
new InputStreamReader(Runtime.getRuntime().exec("hostname").getInputStream()))) {
possibleHostname = reader.readLine();
} catch (final Throwable ignore) {
// Ignore. Hostname command is not always available
return null;
}

if (possibleHostname != null && !possibleHostname.isEmpty()) {
log.debug("Determined hostname from hostname command");
return possibleHostname.trim();
if (possibleHostname == null) {
return null;
}
possibleHostname = possibleHostname.trim();
if (possibleHostname.isEmpty()) {
return null;
}
log.debug("Determined hostname from hostname command");
return possibleHostname;
}

// From DNS
private static String initHostNameFromDNS() {
String possibleHostname = null;
try {
return InetAddress.getLocalHost().getHostName();
possibleHostname = InetAddress.getLocalHost().getHostName();
} catch (final UnknownHostException e) {
// If we are not able to detect the hostname we do not throw an exception.
return null;
}

return null;
if (possibleHostname == null) {
return null;
}
possibleHostname = possibleHostname.trim();
if (possibleHostname.isEmpty()) {
return null;
}
log.debug("Determined hostname from DNS");
return possibleHostname;
}

private static boolean isWindowsOS() {
Expand Down
78 changes: 76 additions & 2 deletions telemetry/src/test/groovy/datadog/telemetry/HostInfoTest.groovy
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package datadog.telemetry

import datadog.trace.api.Config
import datadog.trace.api.Platform
import org.junit.Assume
import spock.lang.Shared
import spock.lang.Specification

class HostInfoTest extends Specification {
void 'getHostname'() {

@Shared
final boolean unameAvailable = 'uname -a'.execute().waitFor() == 0

@Shared
final String unameN = 'uname -n'.execute().text.trim()

void 'getHostname is not null or empty'() {
when:
final hostname = HostInfo.getHostname()

Expand All @@ -14,6 +23,71 @@ class HostInfoTest extends Specification {
!hostname.trim().isEmpty()
}

void 'getHostname matches uname -n'() {
Assume.assumeTrue(unameAvailable)

when:
final hostname = HostInfo.getHostname()

then:
hostname == unameN
}

void 'getHostname from env var matches uname -n'() {
Assume.assumeTrue(unameAvailable)

when:
final hostname = Config.initHostNameFromEnv()
Assume.assumeNotNull(hostname)

then:
hostname == unameN
}

void 'getHostname from /proc/sys/kernel/hostname matches uname -n'() {
Assume.assumeTrue(unameAvailable)

when:
final hostname = Config.initHostNameFromFile("/proc/sys/kernel/hostname")
Assume.assumeNotNull(hostname)

then:
hostname == unameN
}

void 'getHostname from /etc/hostname matches uname -n'() {
Assume.assumeTrue(unameAvailable)

when:
final hostname = Config.initHostNameFromFile("/etc/hostname")
Assume.assumeNotNull(hostname)

then:
hostname == unameN
}

void 'getHostname from hostname command matches uname -n'() {
Assume.assumeTrue(unameAvailable)

when:
final hostname = Config.initHostNameFromCommand()
Assume.assumeNotNull(hostname)

then:
hostname == unameN
}

void 'getHostname from DNS matches uname -n'() {
Assume.assumeTrue(unameAvailable)

when:
final hostname = Config.initHostNameFromDNS()
Assume.assumeNotNull(hostname)

then:
hostname == unameN
}

void 'getOsName'() {
when:
final osName = HostInfo.getOsName()
Expand All @@ -32,7 +106,7 @@ class HostInfoTest extends Specification {
}

void 'compare to uname'() {
Assume.assumeTrue('uname -a'.execute().waitFor() == 0)
Assume.assumeTrue(unameAvailable)

expect:
HostInfo.getHostname() == 'uname -n'.execute().text.trim()
Expand Down