From 05f4e0139f915221eb0392279dee01099378922f Mon Sep 17 00:00:00 2001 From: narrieta Date: Fri, 29 Oct 2021 11:20:46 -0700 Subject: [PATCH 1/3] Improve error message for vmSettings errors --- azurelinuxagent/common/protocol/wire.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/azurelinuxagent/common/protocol/wire.py b/azurelinuxagent/common/protocol/wire.py index 5cbef106aa..1161aaed4d 100644 --- a/azurelinuxagent/common/protocol/wire.py +++ b/azurelinuxagent/common/protocol/wire.py @@ -889,6 +889,8 @@ def get_vm_settings(): response_content = self.decode_config(response.read()) return response_etag, response_content + except HttpError: + raise except ProtocolError: raise except Exception as exception: @@ -1508,11 +1510,12 @@ def __init__(self, operation): self._operation = operation self._error_count = 0 self._next_period = datetime.now() + _ErrorReporter._Period + self._log_message_prefix = "[{0}] [Informational only, the Agent will continue normal operation]".format(self._operation) def report_error(self, error): self._error_count += 1 if self._error_count <= _ErrorReporter._MaxErrors: - logger.info("[{0}] {1}", self._operation, error) + logger.info("{0} {1}", self._log_message_prefix, error) add_event(op=self._operation, message=error, is_success=False, log_event=False) def report_summary(self): @@ -1520,6 +1523,6 @@ def report_summary(self): self._next_period = datetime.now() + _ErrorReporter._Period if self._error_count > 0: message = "{0} errors in the last period".format(self._error_count) - logger.info("[{0}] {1}", self._operation, message) + logger.info("{0} {1}", self._log_message_prefix, message) add_event(op=self._operation, message=message, is_success=False, log_event=False) self._error_count = 0 From eaaec06b14cf7a8f288e66ed84d8b7db202b3cae Mon Sep 17 00:00:00 2001 From: narrieta Date: Fri, 29 Oct 2021 11:52:52 -0700 Subject: [PATCH 2/3] Add etag and correlation id --- azurelinuxagent/common/protocol/wire.py | 4 ++-- tests/protocol/test_wire.py | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/azurelinuxagent/common/protocol/wire.py b/azurelinuxagent/common/protocol/wire.py index 1161aaed4d..ab073e942e 100644 --- a/azurelinuxagent/common/protocol/wire.py +++ b/azurelinuxagent/common/protocol/wire.py @@ -889,10 +889,10 @@ def get_vm_settings(): response_content = self.decode_config(response.read()) return response_etag, response_content - except HttpError: - raise except ProtocolError: raise + except HttpError as http_error: + raise Exception("{0} [correlation ID: {1} eTag: {2}]".format(ustr(http_error), correlation_id, etag)) except Exception as exception: error = textutil.format_exception(exception) # since this is a generic error, we format the exception to include the traceback raise Exception("Error fetching vmSettings [correlation ID: {0} eTag: {1}]: {2}".format(correlation_id, etag, error)) diff --git a/tests/protocol/test_wire.py b/tests/protocol/test_wire.py index 6055a2f5da..7291a3430b 100644 --- a/tests/protocol/test_wire.py +++ b/tests/protocol/test_wire.py @@ -1272,21 +1272,21 @@ def assert_no_exception(test_case, test_function, expected_error): def test_error_in_http_request(test_case, mock_response, expected_error): def do_mock_request(): with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: - def http_get_handler(url, *_, **__): - if self.is_host_plugin_vm_settings_request(url): - if isinstance(mock_response, Exception): - raise mock_response - return mock_response - return None - protocol.set_http_handlers(http_get_handler=http_get_handler) + protocol.do_not_mock = lambda method, url: method == "GET" and self.is_host_plugin_vm_settings_request(url) - protocol.client.update_goal_state() + def http_get_vm_settings(_method, _host, _relative_url, **kwargs): + if isinstance(mock_response, Exception): + raise mock_response + return mock_response + + with patch("azurelinuxagent.common.utils.restutil._http_request", side_effect=http_get_vm_settings): + protocol.client.update_goal_state() assert_no_exception(test_case, do_mock_request, expected_error) # # We test errors different kind of errors; none of them should make update_protocol raise an exception, but all of them should be reported # - test_error_in_http_request("Internal error in the HostGAPlugin", MockHttpResponse(httpclient.BAD_GATEWAY), "[Internal error in HostGAPlugin]") # HostGAPlugin uses 502 for internal errors + test_error_in_http_request("Internal error in the HostGAPlugin", MockHttpResponse(httpclient.BAD_GATEWAY), "Status Code 502") # HostGAPlugin uses 502 for internal errors test_error_in_http_request("Arbitrary error in the request (BAD_REQUEST)", MockHttpResponse(httpclient.BAD_REQUEST), "[HTTP Failed] [400: None]") test_error_in_http_request("ProtocolError during the request", ProtocolError("GENERIC PROTOCOL ERROR"), "GENERIC PROTOCOL ERROR") test_error_in_http_request("Generic error in the request", Exception("GENERIC REQUEST ERROR"), "GENERIC REQUEST ERROR") From 026eb1d24edac636d8a9be135a864b7a03ad7eb9 Mon Sep 17 00:00:00 2001 From: narrieta Date: Fri, 29 Oct 2021 13:05:03 -0700 Subject: [PATCH 3/3] pylint warning --- tests/protocol/test_wire.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/protocol/test_wire.py b/tests/protocol/test_wire.py index 7291a3430b..2004214029 100644 --- a/tests/protocol/test_wire.py +++ b/tests/protocol/test_wire.py @@ -1274,7 +1274,7 @@ def do_mock_request(): with mock_wire_protocol(mockwiredata.DATA_FILE_VM_SETTINGS) as protocol: protocol.do_not_mock = lambda method, url: method == "GET" and self.is_host_plugin_vm_settings_request(url) - def http_get_vm_settings(_method, _host, _relative_url, **kwargs): + def http_get_vm_settings(_method, _host, _relative_url, **_): if isinstance(mock_response, Exception): raise mock_response return mock_response