Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New logger to dump a JSON file with all attributes of a Monitor & ISO dates #1404

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/loggers/json_full.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
json_full - write a full JSON status file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Writes the status of monitors to a JSON file.
This file includes all serializable data from a monitor instance and all dates are in plain ISO format.

.. confval:: filename

:type: string
:required: true

the filename to write to
55 changes: 54 additions & 1 deletion simplemonitor/Loggers/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import arrow
from jinja2 import Environment, FileSystemLoader, select_autoescape

import jsonpickle

from ..Monitors.monitor import Monitor
from ..util import (
copy_if_different,
Expand All @@ -29,7 +31,7 @@
from .logger import Logger, register

if TYPE_CHECKING:
import datetime
pass


@register
Expand Down Expand Up @@ -544,3 +546,54 @@ def process_batch(self) -> None:

def describe(self) -> str:
return "Writing JSON file to {0}".format(self.filename)


@register
class JsonLoggerFull(Logger):
"""Write monitor status to a full JSON file."""

logger_type = "json_full"
filename = "" # type: str
supports_batch = True

def __init__(self, config_options: Optional[dict] = None) -> None:
if config_options is None:
config_options = {}
super().__init__(config_options)
self.filename = self.get_config_option(
"filename", required=True, allow_empty=False
)

def save_result2(self, name: str, monitor: Monitor) -> None:
if self.batch_data is None:
self.batch_data = {}
result = MonitorResult()
for k, v in vars(monitor).items():
# remove built-in attributes
if k.startswith("__"):
continue
# remove attributes that are not JSON serializable
if not isinstance(v, (str, int, float, bool, dict, list)):
continue
# handle arrow dates
if isinstance(v, arrow.Arrow):
v = v.isoformat()
result.__setattr__(k, v)
# transform some attributes per the original JSON logger
if hasattr(monitor, "was_skipped") and monitor.was_skipped:
result.status = "Skipped"
elif monitor.virtual_fail_count() <= 0:
result.status = "OK"
self.batch_data[name] = result

def process_batch(self) -> None:
payload = MonitorJsonPayload()
payload.generated = arrow.now().isoformat()
if self.batch_data is not None:
payload.monitors = self.batch_data
with open(self.filename, "w") as f:
f.write(jsonpickle.encode(payload, unpicklable=False))
self.batch_data = {}

def describe(self) -> str:
return "Writing full JSON file to {0}".format(self.filename)
Loading