From 9f9a96defa5248f6f389335223086b9447fa69db Mon Sep 17 00:00:00 2001 From: Harsh Modi Date: Wed, 6 Sep 2023 11:19:09 -0400 Subject: [PATCH] instrument binary build task to get specific execution information Signed-off-by: Harsh Modi --- .../tasks/binary_container_build.py | 48 ++++++++++++------- tests/tasks/test_binary_container_build.py | 2 +- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/atomic_reactor/tasks/binary_container_build.py b/atomic_reactor/tasks/binary_container_build.py index b623fb833..97af18191 100644 --- a/atomic_reactor/tasks/binary_container_build.py +++ b/atomic_reactor/tasks/binary_container_build.py @@ -17,11 +17,12 @@ from json import JSONDecodeError from osbs.utils import ImageName +from otel_extensions import instrumented, get_tracer from atomic_reactor import dirs from atomic_reactor import util -from atomic_reactor.constants import REMOTE_HOST_MAX_RETRIES, REMOTE_HOST_RETRY_INTERVAL - +from atomic_reactor.constants import (REMOTE_HOST_MAX_RETRIES, REMOTE_HOST_RETRY_INTERVAL, + OTEL_SERVICE_NAME) from atomic_reactor.tasks.common import Task, TaskParams from atomic_reactor.utils import retries from atomic_reactor.utils import remote_host @@ -103,23 +104,31 @@ def execute(self) -> Any: podman_remote = PodmanRemote.setup_for( remote_resource, registries_authfile=get_authfile_path(config.registry) ) + module_name = self.task_name + '_' + platform + tracer = get_tracer(module_name=module_name, service_name=OTEL_SERVICE_NAME) + with tracer.start_as_current_span("build_container") as span: + span.set_attribute('git_ref', self._params.user_params.get('git_ref')) + span.set_attribute('git_uri', self._params.user_params.get('git_uri')) + span.set_attribute('git_commit', + self._params.source.provider_params.get('git_commit', None)) + span.set_attribute('koji_task_id', + self._params.user_params.get('koji_task_id', None)) + # log the image+host for auditing purposes + logger.info("Building image=%s on host=%s", dest_tag, remote_resource.host.hostname) + + output_lines = podman_remote.build_container( + build_dir=build_dir, + build_args=self.workflow_data.buildargs, + dest_tag=dest_tag, + flatpak=flatpak, + memory_limit=config.remote_hosts.get("memory_limit"), + podman_capabilities=config.remote_hosts.get("podman_capabilities") + ) + for line in output_lines: + logger.info(line.rstrip()) + build_log_file.write(line) - # log the image+host for auditing purposes - logger.info("Building image=%s on host=%s", dest_tag, remote_resource.host.hostname) - - output_lines = podman_remote.build_container( - build_dir=build_dir, - build_args=self.workflow_data.buildargs, - dest_tag=dest_tag, - flatpak=flatpak, - memory_limit=config.remote_hosts.get("memory_limit"), - podman_capabilities=config.remote_hosts.get("podman_capabilities") - ) - for line in output_lines: - logger.info(line.rstrip()) - build_log_file.write(line) - - logger.info("Build finished successfully! Pushing image to %s", dest_tag) + logger.info("Build finished successfully! Pushing image to %s", dest_tag) image_size_limit = config.image_size_limit['binary_image'] image_size = podman_remote.get_image_size(dest_tag) @@ -135,6 +144,7 @@ def execute(self) -> Any: return remote_resource.host.hostname + @instrumented def acquire_remote_resource(self, remote_hosts_config: dict) -> remote_host.LockedResource: """Lock a build slot on a remote host.""" logger.info("Acquiring a build slot on a remote host") @@ -310,6 +320,7 @@ def build_container( else: yield last_line + @instrumented def get_image_size(self, dest_tag: ImageName) -> int: inspect_cmd = [*self._podman_remote_cmd, 'image', 'inspect', str(dest_tag)] try: @@ -328,6 +339,7 @@ def get_image_size(self, dest_tag: ImageName) -> int: f"{str(dest_tag)}") from e return image_size + @instrumented def push_container(self, dest_tag: ImageName, *, insecure: bool = False) -> None: """Push the built container (named dest_tag) to the registry (as dest_tag). diff --git a/tests/tasks/test_binary_container_build.py b/tests/tasks/test_binary_container_build.py index 9f64857a6..4ced4a4cd 100644 --- a/tests/tasks/test_binary_container_build.py +++ b/tests/tasks/test_binary_container_build.py @@ -116,7 +116,7 @@ def base_task_params(build_dir: Path, context_dir: Path) -> Dict[str, Any]: "config_file": CONFIG_PATH, "namespace": NAMESPACE, "pipeline_run_name": PIPELINE_RUN_NAME, - "user_params": {}, + "user_params": {'git_uri': 'http://example.com/test.git'}, "task_result": None, }