def get_hostname():

    hostname = None
    config = None

    # first, try the config
    try:
        config = get_config()
        config_hostname = config.get('hostname')
        if config_hostname and is_valid_hostname(config_hostname):
            return config_hostname
    except CfgNotFound:
        log.info("No agent or invalid configuration file found")

    # Try to get GCE instance name
    if hostname is None:
        gce_hostname = GCE.get_hostname(config)
        if gce_hostname is not None:
            if is_valid_hostname(gce_hostname):
                return gce_hostname
    # then move on to os-specific detection
    if hostname is None:
        def _get_hostname_unix():
            try:
                # try fqdn
                p = subprocess.Popen(['/bin/hostname', '-f'], stdout=subprocess.PIPE)
                out, err = p.communicate()
                if p.returncode == 0:
                    if is_p3k():
                        return out.decode("utf-8").strip()
                    else:
                        return out.strip()
            except Exception:
                return None

        os_name = get_os()
        if os_name in ['mac', 'freebsd', 'linux', 'solaris']:
            unix_hostname = _get_hostname_unix()
            if unix_hostname and is_valid_hostname(unix_hostname):
                hostname = unix_hostname

    # if we have an ec2 default hostname, see if there's an instance-id available
    if hostname is not None and True in [hostname.lower().startswith(p) for p in [u'ip-', u'domu']]:
        instanceid = EC2.get_instance_id(config)
        if instanceid:
            hostname = instanceid

    # fall back on socket.gethostname(), socket.getfqdn() is too unreliable
    if hostname is None:
        try:
            socket_hostname = socket.gethostname()
        except socket.error:
            socket_hostname = None
        if socket_hostname and is_valid_hostname(socket_hostname):
            hostname = socket_hostname

    if hostname is None:
        log.critical("Unable to reliably determine host name. You can define one"
                     " in datadog.conf or in your hosts file")
        raise Exception("Unable to reliably determine host name. You can define"
                        " one in datadog.conf or in your hosts file")
    else:
        return hostname
Get the canonical host name this agent should identify as. This is
dataset/ETHPy150Open DataDog/datadogpy/datadog/util/hostname.py/get_hostname