control logging to stdout

This commit is contained in:
Jörn-Michael Miehe 2023-08-31 23:09:50 +00:00
parent 8f6a7a37fd
commit af9d3eb502
4 changed files with 20 additions and 2 deletions

1
.vscode/launch.json vendored
View file

@ -11,6 +11,7 @@
"module": "kiwi_simple_metrics.main",
"env": {
"METRIC__INTERVAL": "5",
"METRIC__QUIET": "False",
"METRIC__DISK__PATHS": "[\"/var\", \"/\", \"/dev\"]",
},
"justMyCode": true

View file

@ -10,11 +10,14 @@ async def run_metrics() -> None:
while True:
interval = asyncio.sleep(SETTINGS.interval)
print(metrics.Report.concat(
report = metrics.Report.concat(
metrics.cpu(),
metrics.memory(),
metrics.disk(),
))
)
if not SETTINGS.quiet:
print(report)
await interval

View file

@ -34,6 +34,14 @@ class Report:
result: str
failed: bool = False
def __str__(self) -> str:
state = "OK" if not self.failed else "FAIL"
return SETTINGS.report_stdout.format(
state=state,
result=self.result,
)
@classmethod
def concat(cls, *_reports: Any) -> Self:
reports = [

View file

@ -91,6 +91,12 @@ class Settings(BaseSettings):
# time between gathering reports
interval: float = 600
# if False, prints reports to stdout
quiet: bool = True
# how to format reports to stdout
report_stdout: str = "[{state}] {result}"
# separates metrics and values in reports
separator: str = ", "