Set the hostname reported by the Java agent

Changing your hostname settings, particularly the FQDN settings, could disable your server's network access. Test any changes thoroughly before deploying them to a production environment.

Problem

You can't view links to your Linux hosts from your Java app's APM Overview page.

Solution

Configure the hostname settings so that Servers for Linux agent and the Java agent return the exact same name string. If possible, do so by editing the fully qualified domain name (FQDN) settings. The Java and Servers agents both read their hostname from the operating system's FQDN settings, so setting the hostname there ensures both agents share a single hostname.

The New Relic user interface uses the hostname to link an app to its host server. Changing the hostname will cause a different name to be reported for the app server than what is reported by any monitored apps. If the app link is currently working, changing the hostname may break the host-server links.

Example: Editing your FQDN settings

This example configures the server to use the following FQDN values:

  • IP address: 1.2.3.4
  • FQDN: myhost.example.com
  • Short hostname: myhost

Most Linux distributions store hostname settings in two files. The two most common filenames and paths are:

  • /etc/hosts
  • /etc/sysconfig/network (may not exist in your Linux distribution)

Perform the following steps to configure the hostname that should be in effect at startup,

  1. Append the IP address, FQDN, and short hostname to the existing data in /etc/hosts. The appended line is highlighted.

    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    1.2.3.4 myhost.example.com myhost

    These changes take effect the next time the server restarts.

  2. If /etc/sysconfig/network is present: Append the FQDN to the existing data in /etc/sysconfig/network. The appended line contains the highlighted hostname:

    NETWORKING=yes
    HOSTNAME=myhost.example.com

    These changes take effect the next time the server restarts.

  3. Use the command line to set the current hostname, so the changes take effect without restarting the server:
    sudo hostname myhost.example.com
  4. Restart the Java agent and Servers agent to register the new hostname.
Example: Scripting hostname settings on AWS Elastic Beanstalk

You can also configure the hostname to be a FQDN at startup using a shell script. In this example, cloud-init passes a shell script to the Amazon Machine Image (AMI) at startup, using the variable UserData. A cloud-init script runs before the Java agent and Servers agent start, so the agents inherit the hostname settings from the cloud-init script.

This example script configures all hostname settings. Depending on your Linux distribution, the echo command to /etc/sysconfig/network may not be required.

#!/bin/sh

# Set the hostname so the Linux Server Monitor
# and Java agent see the same names.

SHORT_HOSTNAME=`hostname -s`
IP=`hostname -I`
DOMAIN="example.com"
HOSTNAME="${SHORT_HOSTNAME}.${DOMAIN}"

hostname "${HOSTNAME}"
echo "${IP} ${HOSTNAME}" >> /etc/hosts
echo "HOSTNAME=${HOSTNAME}" >> /etc/sysconfig/network

Cause

New Relic links the app and the server together by string matching the hostnames reported by the Java agent and Servers for Linux agent. If these two hostname strings are not identical, New Relic can't connect the app and server in the APM Overview page.

There are several reasons this might occur. The JVM might be reporting the IP address and not the hostname. Or the JVM might report an IPv6 address while the Linux Server Monitor reports an IPv4 address.

For more help

Join the discussion about Java monitoring in the New Relic Online Technical Community! The Technical Community is a public platform to discuss and troubleshoot your New Relic toolset.

If you need additional help, get support at support.newrelic.com.