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

Start sending Execution Mode in the Telemetry #1657

Merged
merged 4 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions azurelinuxagent/common/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ def get_distro():
AGENT_PKG_PATTERN = re.compile(AGENT_PATTERN+"\.zip")
AGENT_DIR_PATTERN = re.compile(".*/{0}".format(AGENT_PATTERN))

# The execution mode of the VM - IAAS or PAAS. Linux VMs are only executed in IAAS mode.
AGENT_EXECUTION_MODE = "IAAS"
vrdmr marked this conversation as resolved.
Show resolved Hide resolved

EXT_HANDLER_PATTERN = b".*/WALinuxAgent-(\d+.\d+.\d+[.\d+]*).*-run-exthandlers"
EXT_HANDLER_REGEX = re.compile(EXT_HANDLER_PATTERN)

Expand Down
17 changes: 10 additions & 7 deletions azurelinuxagent/ga/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from azurelinuxagent.common.utils.restutil import IOErrorCounter
from azurelinuxagent.common.utils.textutil import parse_doc, findall, find, getattrib, hash_strings
from azurelinuxagent.common.version import DISTRO_NAME, DISTRO_VERSION, \
DISTRO_CODE_NAME, AGENT_NAME, CURRENT_AGENT, CURRENT_VERSION
DISTRO_CODE_NAME, AGENT_NAME, CURRENT_AGENT, CURRENT_VERSION, AGENT_EXECUTION_MODE


def parse_event(data_str):
Expand Down Expand Up @@ -167,8 +167,8 @@ def init_sysinfo(self):
DISTRO_CODE_NAME,
platform.release())
self.sysinfo.append(TelemetryEventParam("OSVersion", osversion))
self.sysinfo.append(
TelemetryEventParam("GAVersion", CURRENT_AGENT))
self.sysinfo.append(TelemetryEventParam("GAVersion", CURRENT_AGENT))
vrdmr marked this conversation as resolved.
Show resolved Hide resolved
self.sysinfo.append(TelemetryEventParam("ExecutionMode", AGENT_EXECUTION_MODE))

try:
ram = self.osutil.get_total_mem()
Expand Down Expand Up @@ -279,11 +279,14 @@ def daemon(self):

def add_sysinfo(self, event):
sysinfo_names = [v.name for v in self.sysinfo]
copy_param = []

for param in event.parameters:
if param.name in sysinfo_names:
logger.verbose("Remove existing event parameter: [{0}:{1}]", param.name, param.value)
event.parameters.remove(param)
event.parameters.extend(self.sysinfo)
if param.name not in sysinfo_names:
copy_param.append(param)

copy_param.extend(self.sysinfo)
event.parameters = copy_param

def send_imds_heartbeat(self):
"""
Expand Down
110 changes: 100 additions & 10 deletions tests/ga/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

from azurelinuxagent.common.cgroup import CGroup
from azurelinuxagent.common.event import EventLogger
from azurelinuxagent.common.protocol.restapi import get_properties
from azurelinuxagent.common.protocol.imds import ComputeInfo, IMDS_IMAGE_ORIGIN_ENDORSED
from azurelinuxagent.common.protocol.restapi import get_properties, VMInfo
from azurelinuxagent.common.protocol.wire import WireProtocol
from azurelinuxagent.common.utils import restutil
from azurelinuxagent.common.version import AGENT_VERSION
from azurelinuxagent.ga.monitor import *
from nose.plugins.attrib import attr
from tests.common.test_cgroupstelemetry import make_new_cgroup, consume_cpu_time, consume_memory
Expand All @@ -45,17 +47,24 @@ def random_generator(size=6, chars=string.ascii_uppercase + string.digits + stri
return ''.join(random.choice(chars) for x in range(size))


def create_event_message(size,
def create_event_message(size=0,
name="DummyExtension",
op=WALAEventOperation.Unknown,
is_success=True,
duration=0,
version=CURRENT_VERSION,
is_internal=False,
evt_type="",
message="DummyMessage",
invalid_chars=False):
return get_event_message(name=size, op=op, is_success=is_success, duration=duration,
version=version, message=random_generator(size), evt_type=evt_type,

return get_event_message(name=size if size != 0 else name,
op=op,
is_success=is_success,
duration=duration,
version=version,
message=random_generator(size) if size != 0 else message,
evt_type=evt_type,
is_internal=is_internal)


Expand Down Expand Up @@ -97,16 +106,21 @@ def test_add_sysinfo(self, *args):
tenant_name = 'dummy_tenant'
role_name = 'dummy_role'
role_instance_name = 'dummy_role_instance'
execution_mode_value = "IAAS"

vm_name_param = "VMName"
tenant_name_param = "TenantName"
role_name_param = "RoleName"
role_instance_name_param = "RoleInstanceName"

sysinfo = [TelemetryEventParam(vm_name_param, vm_name),
TelemetryEventParam(tenant_name_param, tenant_name),
TelemetryEventParam(role_name_param, role_name),
TelemetryEventParam(role_instance_name_param, role_instance_name)]
execution_mode_param = "ExecutionMode"

sysinfo = [
TelemetryEventParam(role_instance_name_param, role_instance_name),
TelemetryEventParam(vm_name_param, vm_name),
TelemetryEventParam(execution_mode_param, execution_mode_value),
TelemetryEventParam(tenant_name_param, tenant_name),
TelemetryEventParam(role_name_param, role_name)
]
monitor_handler.sysinfo = sysinfo
monitor_handler.add_sysinfo(event)

Expand All @@ -127,8 +141,11 @@ def test_add_sysinfo(self, *args):
elif p.name == role_instance_name_param:
self.assertEqual(role_instance_name, p.value)
counter += 1
elif p.name == execution_mode_param:
self.assertEqual(execution_mode_value, p.value)
counter += 1

self.assertEqual(4, counter)
self.assertEqual(5, counter)

@patch("azurelinuxagent.ga.monitor.MonitorHandler.send_telemetry_heartbeat")
@patch("azurelinuxagent.ga.monitor.MonitorHandler.collect_and_send_events")
Expand Down Expand Up @@ -303,6 +320,79 @@ def _create_mock(self, test_data, mock_http_get, MockCryptUtil, *args):
monitor_handler.protocol_util.get_protocol = Mock(return_value=protocol)
return monitor_handler, protocol

@patch("azurelinuxagent.common.protocol.imds.ImdsClient.get_compute",
return_value=ComputeInfo(subscriptionId="DummySubId",
location="DummyVMLocation",
vmId="DummyVmId",
resourceGroupName="DummyRG",
publisher=""))
@patch("azurelinuxagent.common.protocol.wire.WireProtocol.get_vminfo",
return_value=VMInfo(subscriptionId="DummySubId",
vmName="DummyVMName",
containerId="DummyContainerId",
roleName="DummyRoleName",
roleInstanceName="DummyRoleInstanceName", tenantName="DummyTenant"))
@patch("platform.release", return_value="platform-release")
@patch("platform.system", return_value="Linux")
@patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.get_processor_cores", return_value=4)
@patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.get_total_mem", return_value=10000)
@patch("azurelinuxagent.common.protocol.wire.WireClient.send_event")
@patch("azurelinuxagent.common.conf.get_lib_dir")
def test_collect_and_send_events(self, mock_lib_dir, patch_send_event, patch_get_total_mem, patch_os_cores,
patch_platform_system, patch_platform_release, patch_get_vminfo, patch_get_compute,
*args):
mock_lib_dir.return_value = self.lib_dir

test_data = WireProtocolData(DATA_FILE)
monitor_handler, protocol = self._create_mock(test_data, *args)
monitor_handler.init_protocols()
monitor_handler.init_sysinfo()

# Replacing OSVersion to make it platform agnostic. We can't mock global constants (eg. DISTRO_NAME,
# DISTRO_VERSION, DISTRO_CODENAME), so to make them constant during the test-time, we need to replace the
# OSVersion field in the event object.
for i in monitor_handler.sysinfo:
if i.name == "OSVersion":
i.value = "{0}:{1}-{2}-{3}:{4}".format(platform.system(),
"DISTRO_NAME",
"DISTRO_VERSION",
"DISTRO_CODE_NAME",
platform.release())

self.event_logger.save_event(create_event_message(message="Message-Test"))
monitor_handler.collect_and_send_events()

# Validating the crafted message by the collect_and_sent_event call.
self.assertEqual(1, patch_send_event.call_count)
send_event_call_args = protocol.client.send_event.call_args[0]
sample_message = '<Event id="1">' \
'<![CDATA[<Param Name="Name" Value="DummyExtension" T="mt:wstr" />' \
'<Param Name="Version" Value="{0}" T="mt:wstr" />' \
'<Param Name="IsInternal" Value="False" T="mt:bool" />' \
'<Param Name="Operation" Value="Unknown" T="mt:wstr" />' \
'<Param Name="OperationSuccess" Value="True" T="mt:bool" />' \
'<Param Name="Message" Value="Message-Test" T="mt:wstr" />' \
'<Param Name="Duration" Value="0" T="mt:uint64" />' \
'<Param Name="ExtensionType" Value="" T="mt:wstr" />' \
'<Param Name="OSVersion" ' \
'Value="Linux:DISTRO_NAME-DISTRO_VERSION-DISTRO_CODE_NAME:platform-release" T="mt:wstr" />' \
'<Param Name="GAVersion" Value="WALinuxAgent-{0}" T="mt:wstr" />' \
'<Param Name="ExecutionMode" Value="IAAS" T="mt:wstr" />' \
'<Param Name="RAM" Value="10000" T="mt:uint64" />' \
'<Param Name="Processors" Value="4" T="mt:uint64" />' \
'<Param Name="VMName" Value="DummyVMName" T="mt:wstr" />' \
'<Param Name="TenantName" Value="DummyTenant" T="mt:wstr" />' \
'<Param Name="RoleName" Value="DummyRoleName" T="mt:wstr" />' \
'<Param Name="RoleInstanceName" Value="DummyRoleInstanceName" T="mt:wstr" />' \
'<Param Name="Location" Value="DummyVMLocation" T="mt:wstr" />' \
'<Param Name="SubscriptionId" Value="DummySubId" T="mt:wstr" />' \
'<Param Name="ResourceGroupName" Value="DummyRG" T="mt:wstr" />' \
'<Param Name="VMId" Value="DummyVmId" T="mt:wstr" />' \
'<Param Name="ImageOrigin" Value="1" T="mt:uint64" />]]>' \
'</Event>'.format(AGENT_VERSION)

self.assertEqual(sample_message, send_event_call_args[1])

@patch("azurelinuxagent.common.protocol.wire.WireClient.send_event")
@patch("azurelinuxagent.common.conf.get_lib_dir")
def test_collect_and_send_events_with_small_events(self, mock_lib_dir, patch_send_event, *args):
Expand Down