# Buggy code
def send_cgroup_telemetry(self):
    if self.last_cgroup_telemetry is None:
        self.last_cgroup_telemetry = datetime.datetime.utcnow()

    if datetime.datetime.utcnow() >= (
        self.last_telemetry_heartbeat + MonitorHandler.CGROUP_TELEMETRY_PERIOD
    ):
        try:
            for cgroup_name, metrics in CGroupsTelemetry.collect_all_tracked().items():
                for metric_group, metric_name, value in metrics:
                    if value > 0:
                        report_metric(metric_group, metric_name, cgroup_name, value)
        except Exception as e:
            logger.warn(
                "Failed to collect performance metrics: {0} [{1}]",
                e,
                traceback.format_exc(),
            )

        # Look for extension cgroups we're not already tracking and track them
        try:
            CGroupsTelemetry.update_tracked(self.protocol.client.get_current_handlers())
        except Exception as e:
            logger.warn(
                "Monitor: updating tracked extensions raised {0}: {1}",
                e,
                traceback.format_exc(),
            )

        self.last_cgroup_telemetry = datetime.datetime.utcnow()


# Traceback (azurelinuxagent.common.cgroups.CGroupsException)
2018/07/31 11:41:06.400633 ERROR ExtHandler monitor: Exception tracking wrapper and agent: 'Hierarchy memory is not mounted' [Traceback (most recent call last):
File "bin/WALinuxAgent-2.2.30-py2.7.egg/azurelinuxagent/ga/monitor.py", line 397, in init_cgroups
CGroupsTelemetry.track_cgroup(CGroups.for_extension(""))
File "bin/WALinuxAgent-2.2.30-py2.7.egg/azurelinuxagent/common/cgroups.py", line 360, in for_extension
return CGroups(name, CGroups._construct_custom_path_for_hierarchy)
File "bin/WALinuxAgent-2.2.30-py2.7.egg/azurelinuxagent/common/cgroups.py", line 401, in __init__
raise CGroupsException("Hierarchy {0} is not mounted".format(hierarchy))
azurelinuxagent.common.cgroups.CGroupsException: 'Hierarchy memory is not mounted'
]

# Fixed code
def send_cgroup_telemetry(self):
    if self.last_cgroup_telemetry is None:
        self.last_cgroup_telemetry = datetime.datetime.utcnow()

    if datetime.datetime.utcnow() >= (
        self.last_telemetry_heartbeat + MonitorHandler.CGROUP_TELEMETRY_PERIOD
    ):
        try:
            for cgroup_name, metrics in CGroupsTelemetry.collect_all_tracked().items():
                for metric_group, metric_name, value in metrics:
                    if value > 0:
                        report_metric(metric_group, metric_name, cgroup_name, value)
        except Exception as e:
            logger.warn(
                "Monitor: failed to collect cgroups performance metrics: {0}", ustr(e)
            )
            logger.verbose(traceback.format_exc())

        # Look for extension cgroups we're not already tracking and track them
        try:
            CGroupsTelemetry.update_tracked(self.protocol.client.get_current_handlers())
        except Exception as e:
            logger.warn(
                "Monitor: failed to update cgroups tracked extensions: {0}", ustr(e)
            )
            logger.verbose(traceback.format_exc())

        self.last_cgroup_telemetry = datetime.datetime.utcnow()