osiegmar/logback-gelf

Got empty hostname .

guxingke opened this issue · 3 comments

    private String buildHostname() {
        try {
            return InetAddress.getLocalHost().getCanonicalHostName();
        } catch (final UnknownHostException e) {
            return UUID.randomUUID().toString();
        }
    }

GelfUdpChunker#buildHostname

sometimes, it return empty string but uuid. cause the graylog sever missing log.

the other client works well .
Moocar

    /**
     * Retrieves the local host's hostname. If found, the fully qualified domain name (FQDN) will be returned,
     * otherwise it will fallback to the unqualified domain name. E.g prefer guerrero.moocar.me over guerrero.
     */
    public static String getLocalHostName() throws SocketException, UnknownHostException {
        try {
            final String canonicalHostName = InetAddress.getLocalHost().getCanonicalHostName();
            if (isFQDN(canonicalHostName)) {
                return canonicalHostName;
            } else {
                return InetAddress.getLocalHost().getHostName();
            }
        } catch (UnknownHostException e) {
            NetworkInterface networkInterface = NetworkInterface.getNetworkInterfaces().nextElement();
            if (networkInterface == null)
                throw e;
            InetAddress ipAddress = networkInterface.getInetAddresses().nextElement();
            if (ipAddress == null)
                throw e;
            return ipAddress.getHostAddress();
        }
    }

can use this way to get hostname, It's a pleasure.

Thanks for bringing this up!

It's very interesting, because according to the JavaDocs this should never happen, rather the method getCanonicalHostName() should return:

the fully qualified domain name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address

Before changing any code, I'd like to understand the cause of this problem. Can you share some additional information about your operating system, java vendor and version and how your systems host name is configured?

Thanks for your reply

Test Code

import java.net.InetAddress;
import java.util.*;

public class Test {
    public static void main(String[] args) throws Exception {
        InetAddress ia = InetAddress.getLocalHost();

        String name = ia.getCanonicalHostName();
        System.out.println(name);

        String hostname= ia.getHostName();
        System.out.println(hostname);
    }
}

Dev

java Test
image

uname -a
Darwin gxk.mini 18.2.0 Darwin Kernel Version 18.2.0: Thu Dec 20 20:46:53 PST 2018; root:xnu-4903.241.1~1/RELEASE_X86_64 x86_64

java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

hostname
gxk.mini

ping `hostname` -c 1
PING gxk.mini (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.029 ms

--- gxk.mini ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.029/0.029/0.029/0.000 ms

Prod

java Test
image

uname -a
Linux wp-app-2-51.duitang.cn 4.13.10-1.el7.elrepo.x86_64 #1 SMP Fri Oct 27 09:07:34 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux

java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

hostname
wp-app-2-51.duitang.cn

ping `hostname` -c 1
PING wp-app-2-51.duitang.cn (10.1.2.51) 56(84) bytes of data.
64 bytes from 10.1.2.51: icmp_seq=1 ttl=64 time=0.033 ms

--- wp-app-2-51.duitang.cn ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.033/0.033/0.033/0.000 ms

Note

  1. The Dev env works well. The Prod env get empty.
  2. the name ip mapping defined in the /etc/hosts
  3. no outer dns can resolve the hostname.

that's all, thanks, i don't want to dig into temporary.
if you want more info , i will provide.

I've checked the code of java.net.InetAddress#getCanonicalHostName and I think that the only reason why this method could return an empty string (undocumented!) instead of a hostname or ip address is because of some nameserver misconfiguration or because of a JDK bug. Although, I couldn't find any related bug reports or discussions on the internet about this behavior. In Moocars implementation this is handled (by chance?) because the library is checking if the hostname contains . and is not an IP address (to detect if it is fully qualified). But that implementation does not support IPv6 and prefers java.net.InetAddress#getHostName() instead of the ip address that might be returned by java.net.InetAddress#getCanonicalHostName which might be ambiguous.

I fixed this issue by 187f2f9 and released 2.1.1.