Skip to content

Commit 3752548

Browse files
eyalkorenSylvainJugejackshirazi
authored
Only remove domain when FQDN is expected (#2286)
* Only remove domain when FQDN is expected * ensure that host name is properly normalized * add PR to changelog Co-authored-by: Sylvain Juge <[email protected]> Co-authored-by: jackshirazi <[email protected]>
1 parent 2f5ede2 commit 3752548

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ endif::[]
2626
[float]
2727
===== Bug fixes
2828
* Fixing missing Micrometer metrics in Spring boot due to premature initialization - {pull}2255[#2255]
29+
* Fixing hostname trimming of FQDN too aggressive - {pull}2286[#2286]
2930
3031
[[release-notes-1.x]]
3132
=== Java Agent version 1.x

apm-agent-core/src/main/java/co/elastic/apm/agent/impl/metadata/SystemInfo.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ static String discoverHostname(boolean isWindows, long timeoutMillis) {
139139
}
140140
if (hostname == null || hostname.isEmpty()) {
141141
logger.warn("Unable to discover hostname, set log_level to debug for more details");
142-
} else {
143-
hostname = removeDomain(hostname);
144142
}
145143
return hostname;
146144
}
@@ -151,6 +149,9 @@ static String fallbackHostnameDiscovery(boolean isWindows) {
151149
if (hostname == null || hostname.isEmpty()) {
152150
try {
153151
hostname = InetAddress.getLocalHost().getHostName();
152+
if (hostname != null) {
153+
hostname = removeDomain(hostname);
154+
}
154155
} catch (Exception e) {
155156
logger.warn("Last fallback for hostname discovery of localhost failed", e);
156157
}
@@ -194,7 +195,7 @@ private static String executeHostnameDiscoveryCommand(List<String> cmd, long tim
194195
String hostname = null;
195196
ProcessExecutionUtil.CommandOutput commandOutput = ProcessExecutionUtil.executeCommand(cmd, timeoutMillis);
196197
if (commandOutput.exitedNormally()) {
197-
hostname = commandOutput.getOutput().toString();
198+
hostname = commandOutput.getOutput().toString().trim();
198199
if (logger.isDebugEnabled()) {
199200
logger.debug("hostname obtained by executing command {}: {}", cmdAsString(cmd), hostname);
200201
}

apm-agent-core/src/test/java/co/elastic/apm/agent/impl/metadata/SystemInfoTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ public class SystemInfoTest extends CustomEnvVariables {
3838
isWindows = SystemInfo.isWindows(systemInfo.getPlatform());
3939
}
4040

41-
4241
@Test
4342
void testHostnameDiscoveryThroughCommand() {
4443
String hostname = SystemInfo.discoverHostnameThroughCommand(isWindows, 300);
45-
assertThat(hostname).isNotNull();
44+
assertThat(hostname).isNotNull().isNotEmpty();
45+
assertThat(hostname)
46+
.describedAs("hostname command output should be normalized")
47+
.isEqualTo(hostname.trim());
4648
}
4749

4850
@Test
@@ -61,7 +63,7 @@ void testHostnameDiscoveryThroughEnv() {
6163

6264
@Test
6365
void testHostnameDiscoveryFallbackThroughInetAddress() throws UnknownHostException {
64-
String expectedHostname = InetAddress.getLocalHost().getHostName();
66+
String expectedHostname = SystemInfo.removeDomain(InetAddress.getLocalHost().getHostName());
6567

6668
Map<String, String> customEnvVariables = new HashMap<>();
6769
if (isWindows) {

0 commit comments

Comments
 (0)