Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hostname lookup in debug and on/off setup #428

Merged
merged 7 commits into from
Oct 2, 2019
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions datadog/util/hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def is_valid_hostname(hostname):
return True


def get_hostname():
def get_hostname(from_config=True):
"""
Get the canonical host name this agent should identify as. This is
the authoritative source of the host name for the agent.
Expand All @@ -45,19 +45,25 @@ def get_hostname():
* agent config (datadog.conf, "hostname:")
* 'hostname -f' (on unix)
* socket.gethostname()

:param from_config: hostname lookup from the Datadog agent configuration file
:type from_config: bool
"""

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
if from_config:
config = get_config()
config_hostname = config.get('hostname')
if config_hostname and is_valid_hostname(config_hostname):
log.debug("Hostname lookup from agent configuration"
" will be deprecated in an upcoming version of datadogpy")
return config_hostname
except CfgNotFound:
log.info("No agent or invalid configuration file found")
log.debug("No agent or invalid configuration file found")

# Try to get GCE instance name
if hostname is None:
Expand Down