monitor: JSONProgressMonitor class

New monitor type that emits a JSON object for each log message.

Unlike other monitors:
- The constructor takes a build manifest as argument to initialise the
  Pipeline and Stage counts for the Progress part of the report.
- It doesn't print a 'result' at the end of the build.
- The log() method that prints a log message supports specifying an
  origin to override the default that's set by the constructor.

Although the Logline supports reporting errors separately, this isn't
used yet.

Signed-off-by: Achilleas Koutsou <achilleas@koutsou.net>
This commit is contained in:
Achilleas Koutsou 2021-10-05 17:40:31 +02:00 committed by Ondřej Budai
parent 512933ddd4
commit da4044bc7b

View file

@ -289,6 +289,43 @@ class LogMonitor(BaseMonitor):
self.out.write(message)
class JSONProgressMonitor(BaseMonitor):
"""Monitor that prints the log output of modules wrapped in a JSON object with context and progress metadata"""
def __init__(self, fd: int, manifest: osbuild.Manifest):
super().__init__(fd)
self._ctx_ids: Set[str] = set()
self._progress = Progress("pipelines", len(manifest.pipelines))
self._context = Context(origin="org.osbuild")
def result(self, result):
pass
def begin(self, pipeline: osbuild.Pipeline):
self._context.pipeline(pipeline)
self._progress.sub_progress(Progress("stages", len(pipeline.stages)))
self._progress.incr()
def stage(self, stage: osbuild.Stage):
self._context.stage(stage)
self._progress.incr(depth=1)
def assembler(self, assembler):
self.module(assembler)
def module(self, module):
self.stage(module)
def log(self, message, origin: Optional[str] = None):
oo = self._context.origin
if origin is not None:
self._context.origin = origin
line = LogLine(message=message, context=self._context, progress=self._progress)
json.dump(line.as_dict(), self.out)
self.out.write("\n")
self._context.origin = oo
def make(name, fd):
module = sys.modules[__name__]
monitor = getattr(module, name, None)