Skip to content

Commit

Permalink
Unify hostname detection in config and telemetry
Browse files Browse the repository at this point in the history
* In telemetry, used the hostname discovered in Config initialization.
* In Config, try `/proc/sys/kernel/hostname` and `/etc/hostname` before
  the `hostname` command.
  • Loading branch information
smola committed Dec 1, 2022
1 parent b31c1d9 commit 3aa334b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
2 changes: 1 addition & 1 deletion internal-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ excludedClassesCoverage += [
// a stub
"datadog.trace.bootstrap.instrumentation.api.ProfilingContextIntegration.NoOp"
]
excludedClassesBranchCoverage = ['datadog.trace.api.ProductActivationConfig',]
excludedClassesBranchCoverage = ['datadog.trace.api.ProductActivationConfig', 'datadog.trace.api.Config']

compileTestJava.dependsOn 'generateTestClassNameTries'

Expand Down
22 changes: 21 additions & 1 deletion internal-api/src/main/java/datadog/trace/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -2435,7 +2437,7 @@ private static Set<PropagationStyle> convertStringSetToPropagationStyleSet(
}

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

// Try environment variable. This works in almost all environments
Expand All @@ -2450,6 +2452,24 @@ private static String initHostName() {
return possibleHostname.trim();
}

// 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
}
if (possibleHostname != null && !possibleHostname.trim().isEmpty()) {
log.debug("Determined hostname from file {}", hostNameFile);
return possibleHostname.trim();
}
}

// Try hostname command
try (final BufferedReader reader =
new BufferedReader(
Expand Down
26 changes: 26 additions & 0 deletions internal-api/src/test/groovy/datadog/trace/api/ConfigTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,32 @@ class ConfigTest extends DDSpecification {
"1" | null | ProductActivation.FULLY_ENABLED
}

def "hostname discovery with environment variables"() {
setup:
final expectedHostname = "myhostname"
environmentVariables.set("HOSTNAME", expectedHostname)
environmentVariables.set("COMPUTERNAME", expectedHostname)

when:
def hostname = Config.initHostName()

then:
hostname == expectedHostname
}

def "hostname discovery without environment variables"() {
setup:
environmentVariables.set("HOSTNAME", "")
environmentVariables.set("COMPUTERNAME", "")

when:
def hostname = Config.initHostName()

then:
hostname != null
!hostname.trim().isEmpty()
}

static class ClassThrowsExceptionForValueOfMethod {
static ClassThrowsExceptionForValueOfMethod valueOf(String ignored) {
throw new Throwable()
Expand Down
35 changes: 2 additions & 33 deletions telemetry/src/main/java/datadog/telemetry/HostInfo.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package datadog.telemetry;

import datadog.trace.api.Config;
import datadog.trace.api.Platform;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.AccessControlException;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -24,8 +22,6 @@ public class HostInfo {
private static final Path ETC_HOSTNAME = FileSystems.getDefault().getPath("/etc/hostname");

private static final Path PROC_VERSION = FileSystems.getDefault().getPath("/proc/version");
private static final List<Path> HOSTNAME_FILES = Arrays.asList(PROC_HOSTNAME, ETC_HOSTNAME);

private static final Logger log = LoggerFactory.getLogger(RequestBuilder.class);

private static String hostname;
Expand All @@ -38,30 +34,7 @@ public static String getHostname() {
if (hostname != null) {
return hostname;
}
String hostname = null;
for (Path file : HOSTNAME_FILES) {
hostname = tryReadFile(file);
if (null != hostname) {
break;
}
}

if (hostname == null) {
try {
hostname = getHostNameFromLocalHost();
} catch (UnknownHostException e) {
// purposefully left empty
}
}

if (hostname != null) {
hostname = hostname.trim();
} else {
log.warn("Could not determine hostname");
hostname = "";
}

HostInfo.hostname = hostname;
HostInfo.hostname = Config.get().getHostName();
return hostname;
}

Expand Down Expand Up @@ -115,10 +88,6 @@ public static String getKernelVersion() {
return kernelVersion;
}

private static String getHostNameFromLocalHost() throws UnknownHostException {
return InetAddress.getLocalHost().getHostName();
}

private static String tryReadFile(Path file) {
String content = null;
if (Files.isRegularFile(file)) {
Expand Down

0 comments on commit 3aa334b

Please sign in to comment.