From a1c933133a63b1c062aac2cdab567d85b41ac0fa Mon Sep 17 00:00:00 2001 From: Alexandre Yang Date: Tue, 19 May 2020 16:36:46 +0200 Subject: [PATCH] Better metadata error handling (#6685) --- envoy/datadog_checks/envoy/envoy.py | 10 +++++----- envoy/tests/test_envoy.py | 11 ++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/envoy/datadog_checks/envoy/envoy.py b/envoy/datadog_checks/envoy/envoy.py index fa31011edd30b..54f989097537f 100644 --- a/envoy/datadog_checks/envoy/envoy.py +++ b/envoy/datadog_checks/envoy/envoy.py @@ -161,12 +161,12 @@ def _collect_metadata(self, stats_url): # } raw_version = response.json()["version"].split('/')[1] except requests.exceptions.Timeout: - msg = 'Envoy endpoint `{}` timed out after {} seconds'.format(server_info_url, self.http.options['timeout']) - self.log.info(msg) + self.log.warning( + 'Envoy endpoint `%s` timed out after %s seconds', server_info_url, self.http.options['timeout'] + ) return - except (requests.exceptions.RequestException, requests.exceptions.ConnectionError): - msg = 'Error accessing Envoy endpoint `{}`'.format(server_info_url) - self.log.info(msg) + except Exception as e: + self.log.warning('Error collecting Envoy version with url=`%s`. Error: %s', server_info_url, str(e)) return self.set_metadata('version', raw_version) diff --git a/envoy/tests/test_envoy.py b/envoy/tests/test_envoy.py index c5c1ac9bedfd9..17877a5128df3 100644 --- a/envoy/tests/test_envoy.py +++ b/envoy/tests/test_envoy.py @@ -127,13 +127,22 @@ def test_metadata(datadog_agent): instance = INSTANCES['main'] check = Envoy(CHECK_NAME, {}, [instance]) check.check_id = 'test:123' + check.log = mock.MagicMock() with mock.patch('requests.get', side_effect=requests.exceptions.Timeout()): check._collect_metadata(instance['stats_url']) datadog_agent.assert_metadata_count(0) - with mock.patch('requests.get', side_effect=requests.exceptions.RequestException): + check.log.warning.assert_called_with( + 'Envoy endpoint `%s` timed out after %s seconds', 'http://localhost:8001/server_info', (10.0, 10.0) + ) + with mock.patch('requests.get', side_effect=requests.exceptions.RequestException('Req Exception')): check._collect_metadata(instance['stats_url']) datadog_agent.assert_metadata_count(0) + check.log.warning.assert_called_with( + 'Error collecting Envoy version with url=`%s`. Error: %s', + 'http://localhost:8001/server_info', + 'Req Exception', + ) with mock.patch('requests.get', return_value=response('multiple_services')): check._collect_metadata(instance['stats_url'])