Skip to content

Commit

Permalink
Retrieve latest container id from cache when sending telemetry events (
Browse files Browse the repository at this point in the history
  • Loading branch information
pgombar authored Sep 19, 2019
1 parent 0105f5f commit 9687794
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
4 changes: 4 additions & 0 deletions azurelinuxagent/common/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import azurelinuxagent.common.logger as logger
from azurelinuxagent.common.exception import EventError
from azurelinuxagent.common.future import ustr
from azurelinuxagent.common.protocol.wire import CONTAINER_ID_ENV_VARIABLE
from azurelinuxagent.common.protocol.restapi import TelemetryEventParam, \
TelemetryEvent, \
get_properties
Expand Down Expand Up @@ -94,6 +95,7 @@ class WALAEventOperation:
WALAEventOperation.UnInstall,
]


class EventStatus(object):
EVENT_STATUS_FILE = "event_status.json"

Expand Down Expand Up @@ -277,6 +279,8 @@ def _add_event(self, duration, evt_type, is_internal, is_success, message, name,
event.parameters.append(TelemetryEventParam('Message', message))
event.parameters.append(TelemetryEventParam('Duration', duration))
event.parameters.append(TelemetryEventParam('ExtensionType', evt_type))
event.parameters.append(TelemetryEventParam('ContainerId',
os.environ.get(CONTAINER_ID_ENV_VARIABLE, "UNINITIALIZED")))

data = get_properties(event)
try:
Expand Down
4 changes: 3 additions & 1 deletion azurelinuxagent/common/protocol/wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
AGENTS_MANIFEST_FILE_NAME = "{0}.{1}.agentsManifest"
TRANSPORT_CERT_FILE_NAME = "TransportCert.pem"
TRANSPORT_PRV_FILE_NAME = "TransportPrivate.pem"
# Store the last retrieved container id as an environment variable to be shared between threads for telemetry purposes
CONTAINER_ID_ENV_VARIABLE = "AZURE_GUEST_AGENT_CONTAINER_ID"

PROTOCOL_VERSION = "2012-11-30"
ENDPOINT_FINE_NAME = "WireServer"
Expand Down Expand Up @@ -112,7 +114,6 @@ def get_vminfo(self):
vminfo.tenantName = hosting_env.deployment_name
vminfo.roleName = hosting_env.role_name
vminfo.roleInstanceName = goal_state.role_instance_id
vminfo.containerId = goal_state.container_id
return vminfo

def get_certs(self):
Expand Down Expand Up @@ -1352,6 +1353,7 @@ def parse(self, xml_text):
self.role_config_name = findtext(role_config, "ConfigName")
container = find(xml_doc, "Container")
self.container_id = findtext(container, "ContainerId")
os.environ[CONTAINER_ID_ENV_VARIABLE] = self.container_id
self.remote_access_uri = findtext(container, "RemoteAccessInfo")
lbprobe_ports = find(xml_doc, "LBProbePorts")
self.load_balancer_probe_port = findtext(lbprobe_ports, "Port")
Expand Down
2 changes: 0 additions & 2 deletions azurelinuxagent/ga/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ def init_sysinfo(self):
vminfo.roleName))
self.sysinfo.append(TelemetryEventParam("RoleInstanceName",
vminfo.roleInstanceName))
self.sysinfo.append(TelemetryEventParam("ContainerId",
vminfo.containerId))
except ProtocolError as e:
logger.warn("Failed to get system info: {0}", ustr(e))

Expand Down
52 changes: 51 additions & 1 deletion tests/common/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from azurelinuxagent.common.exception import EventError
from azurelinuxagent.common.future import ustr
from azurelinuxagent.common.utils.extensionprocessutil import read_output
from azurelinuxagent.common.protocol.wire import GoalState
from azurelinuxagent.common.version import CURRENT_VERSION
from azurelinuxagent.ga.monitor import MonitorHandler

Expand All @@ -34,6 +35,55 @@


class TestEvent(AgentTestCase):
def test_add_event_should_read_container_id_from_process_environment(self):
tmp_file = os.path.join(self.tmp_dir, "tmp_file")

def patch_save_event(json_data):
fileutil.write_file(tmp_file, json_data)

with patch("azurelinuxagent.common.event.EventLogger.save_event", side_effect=patch_save_event):
# No container id is set
os.environ.pop(event.CONTAINER_ID_ENV_VARIABLE, None)
event.add_event(name='dummy_name')
data = fileutil.read_file(tmp_file)
self.assertTrue('{"name": "ContainerId", "value": "UNINITIALIZED"}' in data or
'{"value": "UNINITIALIZED", "name": "ContainerId"}' in data)

# Container id is set as an environment variable explicitly
os.environ[event.CONTAINER_ID_ENV_VARIABLE] = '424242'
event.add_event(name='dummy_name')
data = fileutil.read_file(tmp_file)
self.assertTrue('{{"name": "ContainerId", "value": "{0}"}}'.format(
os.environ[event.CONTAINER_ID_ENV_VARIABLE]) in data or
'{{"value": "{0}", "name": "ContainerId"}}'.format(
os.environ[event.CONTAINER_ID_ENV_VARIABLE]) in data)

# Container id is set as an environment variable when parsing the goal state
xml_text = load_data("wire/goal_state.xml")
goal_state = GoalState(xml_text)

container_id = goal_state.container_id
event.add_event(name='dummy_name')
data = fileutil.read_file(tmp_file)
self.assertTrue('{{"name": "ContainerId", "value": "{0}"}}'.format(container_id) in data or
'{{"value": "{0}", "name": "ContainerId"}}'.format(container_id), data)

# Container id is updated as the goal state changes, both in telemetry event and in environment variables
new_container_id = "z6d5526c-5ac2-4200-b6e2-56f2b70c5ab2"
xml_text = load_data("wire/goal_state.xml")
xml_text_updated = xml_text.replace("c6d5526c-5ac2-4200-b6e2-56f2b70c5ab2", new_container_id)
goal_state = GoalState(xml_text_updated)

event.add_event(name='dummy_name')
data = fileutil.read_file(tmp_file)

# Assert both the environment variable and telemetry event got updated
self.assertEquals(os.environ[event.CONTAINER_ID_ENV_VARIABLE], new_container_id)
self.assertTrue('{{"name": "ContainerId", "value": "{0}"}}'.format(new_container_id) in data or
'{{"value": "{0}", "name": "ContainerId"}}'.format(new_container_id), data)

os.environ.pop(event.CONTAINER_ID_ENV_VARIABLE)

def test_event_status_event_marked(self):
es = event.__event_status__

Expand Down Expand Up @@ -239,7 +289,7 @@ def test_save_event_message_with_non_ascii_characters(self):
event_str = MonitorHandler.collect_event(os.path.join(self.tmp_dir, tld_file))
event_json = json.loads(event_str)

self.assertEqual(len(event_json["parameters"]), 8)
self.assertEqual(len(event_json["parameters"]), 9)

for i in event_json["parameters"]:
if i["name"] == "Name":
Expand Down
1 change: 1 addition & 0 deletions tests/ga/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from azurelinuxagent.ga.exthandlers import *
from azurelinuxagent.common.protocol.wire import WireProtocol, InVMArtifactsProfile

# Mock sleep to reduce test execution time
SLEEP = time.sleep


Expand Down
10 changes: 2 additions & 8 deletions tests/ga/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,16 @@ def test_add_sysinfo(self, *args):
tenant_name = 'dummy_tenant'
role_name = 'dummy_role'
role_instance_name = 'dummy_role_instance'
container_id = 'dummy_container_id'

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

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),
TelemetryEventParam(container_id_param, container_id)]
TelemetryEventParam(role_instance_name_param, role_instance_name)]
monitor_handler.sysinfo = sysinfo
monitor_handler.add_sysinfo(event)

Expand All @@ -130,11 +127,8 @@ 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 == container_id_param:
self.assertEqual(container_id, p.value)
counter += 1

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

@patch("azurelinuxagent.ga.monitor.MonitorHandler.send_telemetry_heartbeat")
@patch("azurelinuxagent.ga.monitor.MonitorHandler.collect_and_send_events")
Expand Down

0 comments on commit 9687794

Please sign in to comment.