From 1718c575ae73deb5f6a324da78bf7c5f24ab75d8 Mon Sep 17 00:00:00 2001 From: Jericho Tolentino <68654047+jericht@users.noreply.github.com> Date: Wed, 18 Oct 2023 16:14:02 -0500 Subject: [PATCH] feat: add host metrics logging (#45) Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com> --- src/deadline_worker_agent/metrics.py | 103 ++++++ src/deadline_worker_agent/startup/cli_args.py | 16 + src/deadline_worker_agent/startup/config.py | 19 ++ .../startup/config_file.py | 8 + .../startup/entrypoint.py | 24 +- src/deadline_worker_agent/startup/settings.py | 10 + src/deadline_worker_agent/worker.py | 25 +- test/unit/startup/test_config.py | 14 +- test/unit/startup/test_config_file.py | 2 + test/unit/startup/test_entrypoint.py | 6 + test/unit/startup/test_settings.py | 14 + test/unit/test_metrics.py | 321 ++++++++++++++++++ test/unit/test_worker.py | 2 + 13 files changed, 548 insertions(+), 16 deletions(-) create mode 100644 src/deadline_worker_agent/metrics.py create mode 100644 test/unit/test_metrics.py diff --git a/src/deadline_worker_agent/metrics.py b/src/deadline_worker_agent/metrics.py new file mode 100644 index 00000000..7ee660b6 --- /dev/null +++ b/src/deadline_worker_agent/metrics.py @@ -0,0 +1,103 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +from __future__ import annotations + +from logging import Logger +from threading import Timer +from typing import Any + +import os +import psutil + + +class HostMetricsLogger: + """Context manager that regularly logs host metrics""" + + logger: Logger + interval_s: float + _timer: Timer | None + _prev_network: Any | None + + def __init__(self, logger: Logger, interval_s: float) -> None: + assert interval_s > 0, "interval_s must be a positive number" + self._timer = None + self._prev_network = None + self.logger = logger + self.interval_s = interval_s + + def __enter__(self) -> HostMetricsLogger: + self.log_metrics() + return self + + def __exit__(self, type, value, traceback) -> None: + if self._timer: + self._timer.cancel() + self._timer = None + + def log_metrics(self): + """ + Queries information about the host machine and logs the information as a space-delimited + line of the form: