mirror of
https://github.com/yavook/kiwi-simple-metrics.git
synced 2024-12-22 12:33:01 +00:00
main blocking function: run_metrics(*_metrics)
This commit is contained in:
parent
cac6129282
commit
a6ef9c0c2b
2 changed files with 18 additions and 10 deletions
|
@ -6,14 +6,11 @@ from . import metrics
|
|||
from .settings import SETTINGS
|
||||
|
||||
|
||||
def handle_report() -> None:
|
||||
def run_metrics(*_metrics: metrics.Metric) -> None:
|
||||
reports = (metric() for metric in _metrics)
|
||||
|
||||
# create single report from metrics
|
||||
report = metrics.Report.summary(
|
||||
metrics.cpu(),
|
||||
metrics.memory(),
|
||||
metrics.disk(),
|
||||
metrics.external(),
|
||||
)
|
||||
report = metrics.Report.summary(*reports)
|
||||
|
||||
# maybe print this to stdout
|
||||
if SETTINGS.log.enabled:
|
||||
|
@ -23,18 +20,24 @@ def handle_report() -> None:
|
|||
report.push_webhook()
|
||||
|
||||
|
||||
async def run_metrics() -> None:
|
||||
async def async_main() -> None:
|
||||
loop = asyncio.get_running_loop()
|
||||
|
||||
while True:
|
||||
await asyncio.gather(
|
||||
asyncio.sleep(SETTINGS.interval),
|
||||
loop.run_in_executor(None, handle_report),
|
||||
loop.run_in_executor(
|
||||
None, run_metrics,
|
||||
metrics.cpu,
|
||||
metrics.memory,
|
||||
metrics.disk,
|
||||
metrics.external,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
asyncio.run(run_metrics())
|
||||
asyncio.run(async_main())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -1,11 +1,16 @@
|
|||
from typing import Callable, TypeAlias
|
||||
|
||||
from ._report import Report
|
||||
from .cpu import cpu
|
||||
from .disk import disk
|
||||
from .external import external
|
||||
from .memory import memory
|
||||
|
||||
Metric: TypeAlias = Callable[[], Report | None]
|
||||
|
||||
__all__ = [
|
||||
"Report",
|
||||
"Metric",
|
||||
|
||||
"cpu",
|
||||
"disk",
|
||||
|
|
Loading…
Reference in a new issue