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
Show file tree
Hide file tree
Changes from all 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
13 changes: 11 additions & 2 deletions datadog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
statsd_host=None, statsd_port=None, statsd_use_default_route=False,
statsd_socket_path=None, statsd_namespace=None, return_raw_response=False, **kwargs):
statsd_socket_path=None, statsd_namespace=None, return_raw_response=False,
hostname_from_config=True, **kwargs):
"""
Initialize and configure Datadog.api and Datadog.statsd modules

Expand All @@ -41,9 +42,13 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
:param app_key: Datadog application key
:type app_key: string

:param host_name: Set a specific hostname
:type host_name: string

:param proxies: Proxy to use to connect to Datadog API;
for example, 'proxies': {'http': "http:<user>:<pass>@<ip>:<port>/"}
:type proxies: dictionary mapping protocol to the URL of the proxy.

:param api_host: Datadog API endpoint
:type api_host: url

Expand Down Expand Up @@ -72,6 +77,9 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
:param return_raw_response: Whether or not to return the raw response object in addition \
to the decoded response content (default: False)
:type return_raw_response: boolean

:param hostname_from_config: Set the hostname from the Datadog agent config (agent 5). Will be deprecated
:type hostname_from_config: boolean
"""
# API configuration
api._api_key = api_key or api._api_key or os.environ.get('DATADOG_API_KEY', os.environ.get('DD_API_KEY'))
Expand All @@ -80,7 +88,8 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
api._application_key or
os.environ.get('DATADOG_APP_KEY', os.environ.get('DD_APP_KEY'))
)
api._host_name = host_name or api._host_name or get_hostname()
bkabrda marked this conversation as resolved.
Show resolved Hide resolved
api._hostname_from_config = hostname_from_config
api._host_name = host_name or api._host_name or get_hostname(hostname_from_config)
api._api_host = api_host or api._api_host or os.environ.get('DATADOG_HOST', 'https://api.datadoghq.com')

# Statsd configuration
Expand Down
1 change: 1 addition & 0 deletions datadog/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
_api_version = 'v1'
_api_host = None
_host_name = None
_hostname_from_config = True
_cacert = True

# HTTP(S) settings
Expand Down
16 changes: 10 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(hostname_from_config):
"""
Get the canonical host name this agent should identify as. This is
the authoritative source of the host name for the agent.
Expand All @@ -52,12 +52,16 @@ def get_hostname():

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

# Try to get GCE instance name
if hostname is None:
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def test_default_values(self):
self.assertIsNone(api._api_key)
self.assertIsNone(api._application_key)
self.assertEqual(api._api_host, "https://api.datadoghq.com")
self.assertEqual(api._host_name, util.hostname.get_hostname())
self.assertEqual(api._host_name, util.hostname.get_hostname(api._hostname_from_config))

def test_env_var_values(self):
with EnvVars(
Expand All @@ -169,7 +169,7 @@ def test_env_var_values(self):
self.assertEqual(api._api_key, "API_KEY_ENV")
self.assertEqual(api._application_key, "APP_KEY_ENV")
self.assertEqual(api._api_host, "HOST_ENV")
self.assertEqual(api._host_name, util.hostname.get_hostname())
self.assertEqual(api._host_name, util.hostname.get_hostname(api._hostname_from_config))

del os.environ["DATADOG_API_KEY"]
del os.environ["DATADOG_APP_KEY"]
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_precedence(self):
self.assertEqual(api._api_key, "API_KEY_ENV")
self.assertEqual(api._application_key, "APP_KEY_ENV")
self.assertEqual(api._api_host, "HOST_ENV")
self.assertEqual(api._host_name, util.hostname.get_hostname())
self.assertEqual(api._host_name, util.hostname.get_hostname(api._hostname_from_config))

# Initialize again to check given parameters take precedence over already set value and env vars
initialize(api_key="API_KEY", app_key="APP_KEY", api_host="HOST", host_name="HOSTNAME")
Expand Down