diff --git a/datadog/__init__.py b/datadog/__init__.py index bd0e83ff4..f78f3fd48 100644 --- a/datadog/__init__.py +++ b/datadog/__init__.py @@ -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 @@ -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::@:/"} :type proxies: dictionary mapping protocol to the URL of the proxy. + :param api_host: Datadog API endpoint :type api_host: url @@ -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')) @@ -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() + 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 diff --git a/datadog/api/__init__.py b/datadog/api/__init__.py index f33498ea0..31ca0bc5b 100644 --- a/datadog/api/__init__.py +++ b/datadog/api/__init__.py @@ -6,6 +6,7 @@ _api_version = 'v1' _api_host = None _host_name = None +_hostname_from_config = True _cacert = True # HTTP(S) settings diff --git a/datadog/util/hostname.py b/datadog/util/hostname.py index 0c39670c4..bb9e7478e 100644 --- a/datadog/util/hostname.py +++ b/datadog/util/hostname.py @@ -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. @@ -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: diff --git a/tests/unit/api/test_api.py b/tests/unit/api/test_api.py index c4ffd1431..7792c9349 100644 --- a/tests/unit/api/test_api.py +++ b/tests/unit/api/test_api.py @@ -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( @@ -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"] @@ -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")