From 0aa0ca9f58dbbc9880f4f6bb6fb07a66b70ac88f 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 | 2 ++ osbs/cli/main.py | 10 ++++++++++ osbs/constants.py | 2 ++ osbs/tekton.py | 6 ++++++ osbs/utils/otel.py | 5 ++++- 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/osbs/api.py b/osbs/api.py index f615e6d0..63cfb238 100644 --- a/osbs/api.py +++ b/osbs/api.py @@ -305,6 +305,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 }) @@ -387,6 +388,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..52d1c346 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 get_tracer, instrumented + 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') @@ -103,6 +108,7 @@ def print_output(pipeline_run, export_metadata_file=None): logger.error("Error during canceling pipeline run %s: %s", pipeline_run_name, repr(ex)) +@instrumented def cmd_build(args): if args.instance is None: conf_section = DEFAULT_CONF_BINARY_SECTION @@ -157,6 +163,7 @@ def cmd_build(args): return return_val +@instrumented def cmd_build_source_container(args): if args.instance is None: conf_section = DEFAULT_CONF_SOURCE_SECTION @@ -426,6 +433,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..683f7b30 100644 --- a/osbs/tekton.py +++ b/osbs/tekton.py @@ -14,6 +14,7 @@ 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, @@ -422,6 +423,7 @@ def pipeline_run_url(self): ) return self._pipeline_run_url + @instrumented 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 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 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 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 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..c5e5f2d1 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,9 +37,11 @@ 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(): + logger.info("updating to 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')