From 1f893d9376932fefbfea2362ac513ca43931fb4f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 19 Apr 2019 14:52:23 -0700 Subject: [PATCH] Log warning on slow host resolution Update `StartupInfoLogger` so that if the `InetAddress` call takes more than 200ms a warning is logged. Closes gh-7087 --- .../boot/StartupInfoLogger.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java index 42081e3b58..6c83eabc5c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java @@ -21,6 +21,7 @@ import java.net.InetAddress; import java.util.concurrent.Callable; import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.boot.system.ApplicationHome; import org.springframework.boot.system.ApplicationPid; @@ -39,6 +40,10 @@ import org.springframework.util.StringUtils; */ class StartupInfoLogger { + private static final Log logger = LogFactory.getLog(StartupInfoLogger.class); + + private static final long HOST_NAME_RESOLVE_THRESHOLD = 200; + private final Class sourceClass; StartupInfoLogger(Class sourceClass) { @@ -105,7 +110,20 @@ class StartupInfoLogger { } private void appendOn(StringBuilder message) { + long startTime = System.currentTimeMillis(); append(message, "on ", () -> InetAddress.getLocalHost().getHostName()); + long resolveTime = System.currentTimeMillis() - startTime; + if (resolveTime > HOST_NAME_RESOLVE_THRESHOLD && logger.isWarnEnabled()) { + StringBuilder warning = new StringBuilder(); + warning.append("InetAddress.getLocalHost().getHostName() took "); + warning.append(resolveTime); + warning.append(" milliseconds to respond."); + warning.append(" Please verify your network configuration"); + if (System.getProperty("os.name").toLowerCase().contains("mac")) { + warning.append(" (macOS machines may need to add entries to /etc/hosts)"); + } + logger.warn(warning.append(".")); + } } private void appendPid(StringBuilder message) {