use java library to grab hostname
I ran into this issue due to lack of hostname command on the container.
2025-04-01 11:21:56,560 [elastic-apm-metadata-0] INFO co.elastic.apm.agent.impl.metadata.SystemInfo - Failed to execute command "hostname -f" with exit code -1
2025-04-01 11:21:56,560 [elastic-apm-metadata-0] DEBUG co.elastic.apm.agent.impl.metadata.SystemInfo - Command execution error
java.io.IOException: Cannot run program "hostname": error=2, No such file or directory
https://github.com/elastic/apm-agent-java/blob/93f28fec583847f53b205307eb00f44c3ef05d5f/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/metadata/SystemInfo.java#L223
It would be beneficial to abandon calling this command and do so in java to avoid such error as well as a small performance improvement.
String hostname = InetAddress.getLocalHost().getHostName();
Hi,
I don't recall exactly but using hostname allows us to capture a value consistent cross-languages, and this Java API tends to not be 100% consistent, for example it might return short host name or fully-qualified named depending on the system, which prevents consistency.
What you could do as a work-around is to set the HOSTNAME environment variable, or hostname configuration option to provide a value explicitly.
Right, https://github.com/elastic/apm-agent-java/blob/93f28fec583847f53b205307eb00f44c3ef05d5f/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/metadata/SystemInfo.java#L168-L185 will be called as fallback in that case.
Yes, in addition to that I forgot about the /etc/hostname file which can also be used if you are not on Windows.