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: