Skip to content
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

Unify hostname detection in config and telemetry #4352

Merged
merged 2 commits into from
Dec 1, 2022
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
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
24 changes: 23 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 @@ -256,6 +256,7 @@
import datadog.trace.bootstrap.config.provider.CapturedEnvironmentConfigSource;
import datadog.trace.bootstrap.config.provider.ConfigProvider;
import datadog.trace.bootstrap.config.provider.SystemPropertiesConfigSource;
import datadog.trace.util.Strings;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -267,7 +268,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 +2438,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 +2453,25 @@ 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);
smola marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (Throwable t) {
// Ignore
}
possibleHostname = Strings.trim(possibleHostname);
if (!possibleHostname.isEmpty()) {
log.debug("Determined hostname from file {}", hostNameFile);
return possibleHostname;
}
}

// Try hostname command
try (final BufferedReader reader =
new BufferedReader(
Expand Down
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