From 6dd96bf6d974441ec91ad03e6cf9a236580796ff Mon Sep 17 00:00:00 2001 From: David Bouchare Date: Fri, 16 Aug 2019 14:43:21 +0200 Subject: [PATCH 1/7] Hostname lookup from config to debug and on/off setup --- datadog/api/__init__.py | 3 +++ datadog/util/hostname.py | 12 +++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/datadog/api/__init__.py b/datadog/api/__init__.py index f33498ea0..a9e96b88a 100644 --- a/datadog/api/__init__.py +++ b/datadog/api/__init__.py @@ -16,6 +16,9 @@ _backoff_period = 300 _mute = True +# Agent config settings +config_lookup = False + # Resources from datadog.api.comments import Comment from datadog.api.dashboard_lists import DashboardList diff --git a/datadog/util/hostname.py b/datadog/util/hostname.py index 0c39670c4..5e00d9836 100644 --- a/datadog/util/hostname.py +++ b/datadog/util/hostname.py @@ -7,6 +7,7 @@ import types # datadog +from datadog.api import config_lookup from datadog.util.compat import url_lib, is_p3k, iteritems from datadog.util.config import get_config, get_os, CfgNotFound @@ -52,12 +53,13 @@ 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 config_lookup: + 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") + log.debug("No agent or invalid configuration file found") # Try to get GCE instance name if hostname is None: From 408bff9971bf3c1fb99d1d02979e5d15db47cded Mon Sep 17 00:00:00 2001 From: David Bouchare Date: Mon, 19 Aug 2019 11:23:30 +0200 Subject: [PATCH 2/7] agent config lookup true by default --- datadog/__init__.py | 3 +++ datadog/api/__init__.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/datadog/__init__.py b/datadog/__init__.py index bd0e83ff4..9c4ba2372 100644 --- a/datadog/__init__.py +++ b/datadog/__init__.py @@ -47,6 +47,9 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host=None, :param api_host: Datadog API endpoint :type api_host: url + :param config_lookup: Hostname resolution using the Datadog agent configuration file + :type config_lookup: boolean + :param statsd_host: Host of DogStatsd server or statsd daemon :type statsd_host: address diff --git a/datadog/api/__init__.py b/datadog/api/__init__.py index a9e96b88a..5902a1c36 100644 --- a/datadog/api/__init__.py +++ b/datadog/api/__init__.py @@ -17,7 +17,7 @@ _mute = True # Agent config settings -config_lookup = False +config_lookup = True # Resources from datadog.api.comments import Comment From c8a2b1847085f2bdebc970d6dfd404358279286f Mon Sep 17 00:00:00 2001 From: David Bouchare Date: Tue, 20 Aug 2019 10:18:04 +0200 Subject: [PATCH 3/7] Pass argument directly to get_hostname --- datadog/__init__.py | 3 --- datadog/api/__init__.py | 3 --- datadog/util/hostname.py | 4 ++-- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/datadog/__init__.py b/datadog/__init__.py index 9c4ba2372..bd0e83ff4 100644 --- a/datadog/__init__.py +++ b/datadog/__init__.py @@ -47,9 +47,6 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host=None, :param api_host: Datadog API endpoint :type api_host: url - :param config_lookup: Hostname resolution using the Datadog agent configuration file - :type config_lookup: boolean - :param statsd_host: Host of DogStatsd server or statsd daemon :type statsd_host: address diff --git a/datadog/api/__init__.py b/datadog/api/__init__.py index 5902a1c36..f33498ea0 100644 --- a/datadog/api/__init__.py +++ b/datadog/api/__init__.py @@ -16,9 +16,6 @@ _backoff_period = 300 _mute = True -# Agent config settings -config_lookup = True - # Resources from datadog.api.comments import Comment from datadog.api.dashboard_lists import DashboardList diff --git a/datadog/util/hostname.py b/datadog/util/hostname.py index 5e00d9836..dcd85ff0b 100644 --- a/datadog/util/hostname.py +++ b/datadog/util/hostname.py @@ -36,7 +36,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. @@ -53,7 +53,7 @@ def get_hostname(): # first, try the config try: - if config_lookup: + if from_config: config = get_config() config_hostname = config.get('hostname') if config_hostname and is_valid_hostname(config_hostname): From d8c4b9e8cebece44e0d8f9df9653b667b2bef24e Mon Sep 17 00:00:00 2001 From: David Bouchare Date: Tue, 20 Aug 2019 10:23:30 +0200 Subject: [PATCH 4/7] Remove import - not needed --- datadog/util/hostname.py | 1 - 1 file changed, 1 deletion(-) diff --git a/datadog/util/hostname.py b/datadog/util/hostname.py index dcd85ff0b..90154e70b 100644 --- a/datadog/util/hostname.py +++ b/datadog/util/hostname.py @@ -7,7 +7,6 @@ import types # datadog -from datadog.api import config_lookup from datadog.util.compat import url_lib, is_p3k, iteritems from datadog.util.config import get_config, get_os, CfgNotFound From c2de2ca6dfe055fb0770b9c7f62613ffc818dba9 Mon Sep 17 00:00:00 2001 From: David Bouchare Date: Fri, 23 Aug 2019 16:29:55 +0200 Subject: [PATCH 5/7] Add deprecation message --- datadog/util/hostname.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/datadog/util/hostname.py b/datadog/util/hostname.py index 90154e70b..3c9362761 100644 --- a/datadog/util/hostname.py +++ b/datadog/util/hostname.py @@ -45,6 +45,9 @@ def get_hostname(from_config=True): * 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 @@ -56,6 +59,8 @@ def get_hostname(from_config=True): 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.debug("No agent or invalid configuration file found") From 9206c05677935bfb9b1b85381df4caa5f0f4e2f2 Mon Sep 17 00:00:00 2001 From: David Bouchare Date: Fri, 27 Sep 2019 16:10:50 +0200 Subject: [PATCH 6/7] Address last review comment --- datadog/__init__.py | 15 +++++++++++++-- datadog/api/__init__.py | 1 + datadog/util/hostname.py | 21 +++++++++------------ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/datadog/__init__.py b/datadog/__init__.py index bd0e83ff4..f5ac925d0 100644 --- a/datadog/__init__.py +++ b/datadog/__init__.py @@ -29,7 +29,7 @@ logging.getLogger('datadog.threadstats').addHandler(NullHandler()) -def initialize(api_key=None, app_key=None, host_name=None, api_host=None, +def initialize(api_key=None, app_key=None, host_name=None, hostname_from_config=True, 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): """ @@ -41,9 +41,16 @@ 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 hostname_from_config: Set the hostname from the Datadog agent config (agent 5). Will be deprecated + :type hostname_from_config: boolean + :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 @@ -80,7 +87,11 @@ 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 + if api._hostname_from_config: + api._host_name = host_name or api._host_name or get_hostname() + else: + api._host_name = host_name or api._host_name 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 3c9362761..78e3003a8 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(from_config=True): +def get_hostname(): """ Get the canonical host name this agent should identify as. This is the authoritative source of the host name for the agent. @@ -45,9 +45,6 @@ def get_hostname(from_config=True): * 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 @@ -55,15 +52,15 @@ def get_hostname(from_config=True): # first, try the config try: - 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 + 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.debug("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: From 0d7528eedee93a2c97cf6705338a075f5fed03d2 Mon Sep 17 00:00:00 2001 From: David Bouchare Date: Mon, 30 Sep 2019 18:17:36 +0200 Subject: [PATCH 7/7] Address review feedback --- datadog/__init__.py | 16 +++++++--------- datadog/util/hostname.py | 17 +++++++++-------- tests/unit/api/test_api.py | 6 +++--- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/datadog/__init__.py b/datadog/__init__.py index f5ac925d0..f78f3fd48 100644 --- a/datadog/__init__.py +++ b/datadog/__init__.py @@ -29,9 +29,10 @@ logging.getLogger('datadog.threadstats').addHandler(NullHandler()) -def initialize(api_key=None, app_key=None, host_name=None, hostname_from_config=True, api_host=None, +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 @@ -44,9 +45,6 @@ def initialize(api_key=None, app_key=None, host_name=None, hostname_from_config= :param host_name: Set a specific hostname :type host_name: string - :param hostname_from_config: Set the hostname from the Datadog agent config (agent 5). Will be deprecated - :type hostname_from_config: boolean - :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. @@ -79,6 +77,9 @@ def initialize(api_key=None, app_key=None, host_name=None, hostname_from_config= :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')) @@ -88,10 +89,7 @@ def initialize(api_key=None, app_key=None, host_name=None, hostname_from_config= os.environ.get('DATADOG_APP_KEY', os.environ.get('DD_APP_KEY')) ) api._hostname_from_config = hostname_from_config - if api._hostname_from_config: - api._host_name = host_name or api._host_name or get_hostname() - else: - api._host_name = host_name or api._host_name + 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/util/hostname.py b/datadog/util/hostname.py index 78e3003a8..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,13 +52,14 @@ 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): - 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 + 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.warning("No agent or invalid configuration file found") 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")