Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send telemetry for environment variables used per extension call #2536

Merged
merged 3 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions azurelinuxagent/ga/exthandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1902,12 +1902,15 @@ def launch_command(self, cmd, cmd_name=None, timeout=300, extension_error_code=E
if supported_features:
env[ExtCommandEnvVariable.ExtensionSupportedFeatures] = json.dumps(supported_features)

ext_name = self.get_extension_full_name(extension)
try:
# Some extensions erroneously begin cmd with a slash; don't interpret those
# as root-relative. (Issue #1170)
command_full_path = os.path.join(base_dir, cmd.lstrip(os.path.sep))
self.logger.info("Executing command: {0} with environment variables: {1}".format(command_full_path,
json.dumps(env)))
log_msg = "Executing command: {0} with environment variables: {1}".format(command_full_path,
json.dumps(env))
self.logger.info(log_msg)
self.report_event(name=ext_name, message=log_msg, log_event=False)

# Add the os environment variables before executing command
env.update(os.environ)
Expand All @@ -1928,7 +1931,6 @@ def launch_command(self, cmd, cmd_name=None, timeout=300, extension_error_code=E
code=extension_error_code)

duration = elapsed_milliseconds(begin_utc)
ext_name = self.get_extension_full_name(extension)
log_msg = "Command: {0}\n{1}".format(cmd, "\n".join(
[line for line in process_output.split('\n') if line != ""]))
self.logger.info(log_msg)
Expand Down
16 changes: 8 additions & 8 deletions tests/ga/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2336,8 +2336,8 @@ def test_ext_sequence_no_should_be_set_from_within_extension(self, *args):
exthandlers_handler.report_ext_handlers_status()

for _, kwargs in mock_report_event.call_args_list:
# The output is of the format - 'testfile.sh\n[stdout]ConfigSequenceNumber=N\n[stderr]'
if test_file_name not in kwargs['message']:
# The output is of the format - 'Command: testfile.sh -{Operation} \n[stdout]ConfigSequenceNumber=N\n[stderr]'
if ("Command: " + test_file_name) not in kwargs['message']:
continue
self.assertIn("{0}={1}".format(ExtCommandEnvVariable.ExtensionSeqNumber, expected_seq_no),
kwargs['message'])
Expand Down Expand Up @@ -2405,13 +2405,13 @@ def test_correct_exit_code_should_be_set_on_uninstall_cmd_failure(self, *args):
with patch.object(ExtHandlerInstance, 'report_event') as mock_report_event:
exthandlers_handler.run()
exthandlers_handler.report_ext_handlers_status()
exthandlers_handler.report_ext_handlers_status()

_, disable_kwargs = mock_report_event.call_args_list[1] # pylint: disable=unused-variable
_, update_kwargs = mock_report_event.call_args_list[2]
_, uninstall_kwargs = mock_report_event.call_args_list[3] # pylint: disable=unused-variable
_, install_kwargs = mock_report_event.call_args_list[4]
_, enable_kwargs = mock_report_event.call_args_list[5]
update_kwargs = next(kwargs for _, kwargs in mock_report_event.call_args_list if
"Command: testfile.sh -update" in kwargs['message'])
install_kwargs = next(kwargs for _, kwargs in mock_report_event.call_args_list if
"Command: testfile.sh -install" in kwargs['message'])
enable_kwargs = next(kwargs for _, kwargs in mock_report_event.call_args_list if
"Command: testfile.sh -enable" in kwargs['message'])

self.assertIn("%s=%s" % (ExtCommandEnvVariable.DisableReturnCode, exit_code), update_kwargs['message'])
self.assertIn("%s=%s" % (ExtCommandEnvVariable.UninstallReturnCode, exit_code), install_kwargs['message'])
Expand Down
11 changes: 8 additions & 3 deletions tests/ga/test_multi_config_extension.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import json
import os.path
import re
import subprocess
import uuid

Expand Down Expand Up @@ -887,15 +888,19 @@ def test_it_should_ignore_disable_errors_for_multi_config_extensions(self):
(sc_ext, ExtensionCommandNames.ENABLE)
)

reported_events = [kwargs for _, kwargs in patch_report_event.call_args_list if
re.search("Executing command: (.+) with environment variables: ",
kwargs['message']) is None]

self.assertTrue(all(
fail_code in kwargs['message'] for args, kwargs in patch_report_event.call_args_list if
fail_code in kwargs['message'] for kwargs in reported_events if
kwargs['name'] == first_ext.name), "Error not reported")
self.assertTrue(all(
fail_code in kwargs['message'] for args, kwargs in patch_report_event.call_args_list if
fail_code in kwargs['message'] for kwargs in reported_events if
kwargs['name'] == second_ext.name), "Error not reported")
# Make sure fail code is not reported for any other extension
self.assertFalse(all(
fail_code in kwargs['message'] for args, kwargs in patch_report_event.call_args_list if
fail_code in kwargs['message'] for kwargs in reported_events if
kwargs['name'] == third_ext.name), "Error not reported")

def test_it_should_report_transitioning_if_status_file_not_found(self):
Expand Down