From b8c3ee48ebb0e348e74b6a73cc5b7ada35bdfceb Mon Sep 17 00:00:00 2001 From: HadhemiDD <43783545+HadhemiDD@users.noreply.github.com> Date: Tue, 20 Jun 2023 16:37:36 +0200 Subject: [PATCH] Disable server info and version collection when collect_server_info is false (#14610) * if collect_server_info is set to false disable server info and version collection * the collect metadata in check.py has to check for the collect_server_info before attempting to collect server info, even when the base url is well formated * add testing to see if metadata is collected when collect_server_info is false * add testing to see if metadata is collected when collect_server_info is false * fix typo * fix typo * commit * commit * commit * commit typo * fix check.py --- envoy/datadog_checks/envoy/check.py | 7 +++++++ envoy/tests/test_unit.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/envoy/datadog_checks/envoy/check.py b/envoy/datadog_checks/envoy/check.py index 6dc782892525c..67876bc283c5d 100644 --- a/envoy/datadog_checks/envoy/check.py +++ b/envoy/datadog_checks/envoy/check.py @@ -101,6 +101,8 @@ def __init__(self, name, init_config, instances): super().__init__(name, init_config, instances) self.check_initializations.append(self.configure_additional_transformers) openmetrics_endpoint = self.instance.get('openmetrics_endpoint') + self.collect_server_info = self.instance.get('collect_server_info', True) + self.base_url = None try: parts = urlparse(openmetrics_endpoint) @@ -148,7 +150,12 @@ def _collect_metadata(self): if not self.base_url: self.log.debug("Skipping server info collection due to malformed url: %s", self.base_url) return + # From http://domain/thing/stats to http://domain/thing/server_info + if not self.collect_server_info: + self.log.debug("Skipping server info collection as it is disabled, collect_server_info") + return + server_info_url = urljoin(self.base_url, 'server_info') raw_version = _get_server_info(server_info_url, self.log, self.http) diff --git a/envoy/tests/test_unit.py b/envoy/tests/test_unit.py index 7b0418323c922..b3c525ce57468 100644 --- a/envoy/tests/test_unit.py +++ b/envoy/tests/test_unit.py @@ -84,3 +84,17 @@ def test_collect_metadata_with_invalid_base_url( c._collect_metadata() datadog_agent.assert_metadata_count(0) c.log.debug.assert_called_with('Skipping server info collection due to malformed url: %s', b'') + + +@requires_py3 +def test_collect_metadata_with_disabled_collect_server_info( + datadog_agent, fixture_path, mock_http_response, check, default_instance +): + default_instance["collect_server_info"] = False + c = check(default_instance) + c.check_id = 'test:123' + c.log = mock.MagicMock() + + c._collect_metadata() + datadog_agent.assert_metadata_count(0) + c.log.debug.assert_called_with('Skipping server info collection as it is disabled, collect_server_info')