Skip to content

Commit

Permalink
Add vm arch to heartbeat telemetry (Azure#2818)
Browse files Browse the repository at this point in the history
* Add VM Arch to heartbeat telemetry

* Remove outdated vmsize heartbeat tesT

* Remove unused import

* Use platform to get vmarch

(cherry picked from commit 66e8b3d)
  • Loading branch information
maddieford committed Jun 6, 2023
1 parent 6a8e0d6 commit 0c13d35
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 47 deletions.
12 changes: 8 additions & 4 deletions azurelinuxagent/ga/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import glob
import json
import os
import platform
import re
import shutil
import signal
Expand Down Expand Up @@ -462,6 +463,9 @@ def _get_vm_size(self, protocol):

return self._vm_size

def _get_vm_arch(self):
return platform.machine()

def _check_daemon_running(self, debug):
# Check that the parent process (the agent's daemon) is still running
if not debug and self._is_orphaned:
Expand Down Expand Up @@ -1265,13 +1269,13 @@ def _send_heartbeat_telemetry(self, protocol):
if datetime.utcnow() >= (self._last_telemetry_heartbeat + UpdateHandler.TELEMETRY_HEARTBEAT_PERIOD):
dropped_packets = self.osutil.get_firewall_dropped_packets(protocol.get_endpoint())
auto_update_enabled = 1 if conf.get_autoupdate_enabled() else 0
# Include VMSize in the heartbeat message because the kusto table does not have
# a separate column for it (or architecture).
vmsize = self._get_vm_size(protocol)
# Include vm architecture in the heartbeat message because the kusto table does not have
# a separate column for it.
vmarch = self._get_vm_arch()

telemetry_msg = "{0};{1};{2};{3};{4};{5}".format(self._heartbeat_counter, self._heartbeat_id, dropped_packets,
self._heartbeat_update_goal_state_error_count,
auto_update_enabled, vmsize)
auto_update_enabled, vmarch)
debug_log_msg = "[DEBUG HeartbeatCounter: {0};HeartbeatId: {1};DroppedPackets: {2};" \
"UpdateGSErrors: {3};AutoUpdate: {4}]".format(self._heartbeat_counter,
self._heartbeat_id, dropped_packets,
Expand Down
43 changes: 0 additions & 43 deletions tests/ga/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from datetime import datetime, timedelta
from threading import current_thread
from azurelinuxagent.common.protocol.imds import ComputeInfo
from tests.common.osutil.test_default import TestOSUtil
import azurelinuxagent.common.osutil.default as osutil

Expand Down Expand Up @@ -2773,48 +2772,6 @@ def test_telemetry_heartbeat_creates_event(self, patch_add_event, patch_info, *_
self.assertTrue(any(call_args[0] == "[HEARTBEAT] Agent {0} is running as the goal state agent {1}"
for call_args in patch_info.call_args), "The heartbeat was not written to the agent's log")

@patch("azurelinuxagent.ga.update.add_event")
@patch("azurelinuxagent.common.protocol.imds.ImdsClient")
def test_telemetry_heartbeat_retries_failed_vm_size_fetch(self, mock_imds_factory, patch_add_event, *_):

def validate_single_heartbeat_event_matches_vm_size(vm_size):
heartbeat_event_kwargs = [
kwargs for _, kwargs in patch_add_event.call_args_list
if kwargs.get('op', None) == WALAEventOperation.HeartBeat
]

self.assertEqual(1, len(heartbeat_event_kwargs), "Expected exactly one HeartBeat event, got {0}"\
.format(heartbeat_event_kwargs))

telemetry_message = heartbeat_event_kwargs[0].get("message", "")
self.assertTrue(telemetry_message.endswith(vm_size),
"Expected HeartBeat message ('{0}') to end with the test vmSize value, {1}."\
.format(telemetry_message, vm_size))

with mock_wire_protocol(mockwiredata.DATA_FILE) as mock_protocol:
update_handler = get_update_handler()
update_handler.protocol_util.get_protocol = Mock(return_value=mock_protocol)

# Zero out the _vm_size parameter for test resiliency
update_handler._vm_size = None

mock_imds_client = mock_imds_factory.return_value = Mock()

# First force a vmSize retrieval failure
mock_imds_client.get_compute.side_effect = HttpError(msg="HTTP Test Failure")
update_handler._last_telemetry_heartbeat = datetime.utcnow() - timedelta(hours=1)
update_handler._send_heartbeat_telemetry(mock_protocol)

validate_single_heartbeat_event_matches_vm_size("unknown")
patch_add_event.reset_mock()

# Now provide a vmSize
mock_imds_client.get_compute = lambda: ComputeInfo(vmSize="TestVmSizeValue")
update_handler._last_telemetry_heartbeat = datetime.utcnow() - timedelta(hours=1)
update_handler._send_heartbeat_telemetry(mock_protocol)

validate_single_heartbeat_event_matches_vm_size("TestVmSizeValue")


class AgentMemoryCheckTestCase(AgentTestCase):

Expand Down

0 comments on commit 0c13d35

Please sign in to comment.