From 1b8cc1b8431e71a9dc65c41d37001a16792680fb Mon Sep 17 00:00:00 2001 From: Harsh Modi Date: Tue, 12 Sep 2023 09:03:51 -0400 Subject: [PATCH] trace parts of osbs-client calls expecially tasks with http requests Signed-off-by: Harsh Modi --- osbs/api.py | 8 +++++- osbs/cli/main.py | 62 ++++++++++++++++++++++++++++------------------ osbs/constants.py | 2 ++ osbs/tekton.py | 8 +++++- osbs/utils/otel.py | 5 +++- 5 files changed, 58 insertions(+), 27 deletions(-) diff --git a/osbs/api.py b/osbs/api.py index f615e6d0..c4fe5471 100644 --- a/osbs/api.py +++ b/osbs/api.py @@ -27,7 +27,7 @@ from osbs.utils.labels import Labels # import utils in this way, so that we can mock standalone functions with flexmock from osbs import utils -from osbs.utils.otel import get_current_traceparent +from osbs.utils.otel import get_current_traceparent, init_otel def _load_pipeline_from_template(pipeline_run_path, substitutions): @@ -255,6 +255,8 @@ def _get_binary_container_pipeline_data(self, *, buildtime_limit, user_params, @osbsapi def create_binary_container_build(self, **kwargs): + init_otel(otel_url=self.os_conf.get_otel_url(), + traceparent=kwargs.get('traceparent', None)) return self.create_binary_container_pipeline_run(**kwargs) @osbsapi @@ -305,6 +307,7 @@ def create_binary_container_pipeline_run(self, # if the traceparent is not updated then child call will # be linked to the parent of current call traceparent = get_current_traceparent() + logger.info("updating traceparent in kwargs") kwargs.update({ 'traceparent': traceparent }) @@ -361,6 +364,8 @@ def _get_source_container_pipeline_data(self, *, user_params, pipeline_run_name) @osbsapi def create_source_container_build(self, **kwargs): + init_otel(otel_url=self.os_conf.get_otel_url(), + traceparent=kwargs.get('traceparent', None)) return self.create_source_container_pipeline_run(**kwargs) @osbsapi @@ -387,6 +392,7 @@ def create_source_container_pipeline_run(self, # if the traceparent is not updated then child call will # be linked to the parent of current call traceparent = get_current_traceparent() + logger.info("updating traceparent in kwargs") kwargs.update({ 'traceparent': traceparent }) diff --git a/osbs/cli/main.py b/osbs/cli/main.py index d3f8ce28..8da30991 100644 --- a/osbs/cli/main.py +++ b/osbs/cli/main.py @@ -14,6 +14,10 @@ import sys import argparse + +from opentelemetry.sdk.environment_variables import OTEL_SERVICE_NAME +from otel_extensions import instrumented, get_tracer + from osbs import set_logging from osbs.api import OSBS from osbs.conf import Configuration @@ -22,6 +26,7 @@ from osbs.exceptions import (OsbsNetworkException, OsbsException, OsbsAuthException, OsbsResponseException) from osbs.utils import UserWarningsStore +from osbs.utils.otel import init_otel logger = logging.getLogger('osbs') @@ -139,21 +144,24 @@ def cmd_build(args): if osbs.os_conf.get_flatpak(): build_kwargs['flatpak'] = True - pipeline_run = osbs.create_binary_container_pipeline_run(**build_kwargs) + span_name = 'binary_build' + tracer = get_tracer(module_name=span_name, service_name=OTEL_SERVICE_NAME) + with tracer.start_as_current_span(span_name): + pipeline_run = osbs.create_binary_container_pipeline_run(**build_kwargs) - print_output(pipeline_run, export_metadata_file=args.export_metadata_file) + print_output(pipeline_run, export_metadata_file=args.export_metadata_file) - return_val = -1 + return_val = -1 - if pipeline_run.has_succeeded(): - return_val = 0 - cleanup_used_resources = osbs.os_conf.get_cleanup_used_resources() - if cleanup_used_resources and pipeline_run.data is not None: - try: - logger.info("pipeline run removed: %s", pipeline_run.remove_pipeline_run()) - except OsbsResponseException: - logger.error("failed to remove pipeline run %s", pipeline_run.pipeline_run_name) - raise + if pipeline_run.has_succeeded(): + return_val = 0 + cleanup_used_resources = osbs.os_conf.get_cleanup_used_resources() + if cleanup_used_resources and pipeline_run.data is not None: + try: + logger.info("pipeline run removed: %s", pipeline_run.remove_pipeline_run()) + except OsbsResponseException: + logger.error("failed to remove pipeline run %s", pipeline_run.pipeline_run_name) + raise return return_val @@ -181,21 +189,24 @@ def cmd_build_source_container(args): if args.traceparent: build_kwargs['traceparent'] = args.traceparent - pipeline_run = osbs.create_source_container_pipeline_run(**build_kwargs) + span_name = 'source_build' + tracer = get_tracer(module_name=span_name, service_name=OTEL_SERVICE_NAME) + with tracer.start_as_current_span(span_name): + pipeline_run = osbs.create_source_container_pipeline_run(**build_kwargs) - print_output(pipeline_run, export_metadata_file=args.export_metadata_file) + print_output(pipeline_run, export_metadata_file=args.export_metadata_file) - return_val = -1 + return_val = -1 - if pipeline_run.has_succeeded(): - return_val = 0 - cleanup_used_resources = osbs.os_conf.get_cleanup_used_resources() - if cleanup_used_resources and pipeline_run.data is not None: - try: - logger.info("pipeline run removed: %s", pipeline_run.remove_pipeline_run()) - except OsbsResponseException: - logger.error("failed to remove pipeline run %s", pipeline_run.pipeline_run_name) - raise + if pipeline_run.has_succeeded(): + return_val = 0 + cleanup_used_resources = osbs.os_conf.get_cleanup_used_resources() + if cleanup_used_resources and pipeline_run.data is not None: + try: + logger.info("pipeline run removed: %s", pipeline_run.remove_pipeline_run()) + except OsbsResponseException: + logger.error("failed to remove pipeline run %s", pipeline_run.pipeline_run_name) + raise return return_val @@ -426,6 +437,9 @@ def main(): set_logging(level=logging.INFO) return_value = -1 + + init_otel(otel_url=os_conf.get_otel_url(), + traceparent=args.traceparent) try: return_value = args.func(args) except AttributeError: diff --git a/osbs/constants.py b/osbs/constants.py index 18e003f4..caec35d4 100644 --- a/osbs/constants.py +++ b/osbs/constants.py @@ -74,6 +74,8 @@ # number of seconds to wait, before retrying on openshift not found OS_NOT_FOUND_MAX_WAIT = 1 +OTEL_SERVICE_NAME = "osbs" + ISOLATED_RELEASE_FORMAT = re.compile(r'^\d+\.\d+(\..+)?$') RELEASE_LABEL_FORMAT = re.compile(r"""^\d+ # First character must be a digit ([._]? # allow separators between groups diff --git a/osbs/tekton.py b/osbs/tekton.py index 56a85776..e6680217 100644 --- a/osbs/tekton.py +++ b/osbs/tekton.py @@ -14,10 +14,11 @@ from typing import Dict, List, Tuple, Callable, Any from datetime import datetime +from otel_extensions import instrumented from osbs.exceptions import OsbsResponseException, OsbsAuthException, OsbsException from osbs.constants import (DEFAULT_NAMESPACE, SERVICEACCOUNT_SECRET, SERVICEACCOUNT_TOKEN, - SERVICEACCOUNT_CACRT) + SERVICEACCOUNT_CACRT, OTEL_SERVICE_NAME) from osbs.osbs_http import HttpSession from osbs.kerberos_ccache import kerberos_ccache_init from osbs.utils import retry_on_conflict @@ -422,6 +423,7 @@ def pipeline_run_url(self): ) return self._pipeline_run_url + @instrumented(service_name=OTEL_SERVICE_NAME) def start_pipeline_run(self): if not self.input_data: raise OsbsException("No input data provided for pipeline run to start") @@ -445,6 +447,7 @@ def start_pipeline_run(self): ) return response.json() + @instrumented(service_name=OTEL_SERVICE_NAME) def remove_pipeline_run(self): url = self.os.build_url( self.api_path, @@ -458,6 +461,7 @@ def remove_pipeline_run(self): return response.json() @retry_on_conflict + @instrumented(service_name=OTEL_SERVICE_NAME) def cancel_pipeline_run(self): data = copy.deepcopy(self.minimal_data) data['spec']['status'] = 'CancelledRunFinally' @@ -479,6 +483,7 @@ def cancel_pipeline_run(self): raise OsbsException(exc_msg) return response_json + @instrumented(service_name=OTEL_SERVICE_NAME) def get_info(self, wait=False): if wait: self.wait_for_start() @@ -731,6 +736,7 @@ def load_result(result: Dict[str, str]) -> Tuple[str, Any]: name: value for name, value in map(load_result, pipeline_results) if value is not None } + @instrumented(service_name=OTEL_SERVICE_NAME) def wait_for_start(self): """ https://tekton.dev/docs/pipelines/pipelineruns/#monitoring-execution-status diff --git a/osbs/utils/otel.py b/osbs/utils/otel.py index 17915e6a..868710cf 100644 --- a/osbs/utils/otel.py +++ b/osbs/utils/otel.py @@ -20,6 +20,7 @@ def init_otel(otel_url: Optional[str], traceparent: Optional[str]): + logger.info("Initializing otel with traceparent %s", traceparent) span_exporter = '' otel_protocol = 'http/protobuf' if not otel_url: @@ -36,10 +37,12 @@ def init_otel(otel_url: Optional[str], traceparent: Optional[str]): ) init_telemetry_provider(otel_options) RequestsInstrumentor().instrument() + logger.info("Initialization complete") -def get_current_traceparent(**kwargs): +def get_current_traceparent(): tracecontext = trace.get_current_span().get_span_context() traceparent = (f'00-{format_trace_id(tracecontext.trace_id)}-' f'{format_span_id(tracecontext.span_id)}-01') + logger.info("current traceparent is %s", traceparent) return traceparent