Skip to content

Commit

Permalink
trace parts of osbs-client calls
Browse files Browse the repository at this point in the history
expecially tasks with http requests

Signed-off-by: Harsh Modi <hmodi@redhat.com>
  • Loading branch information
hjmodi committed Sep 12, 2023
1 parent bc023f8 commit 0aa0ca9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions osbs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down Expand Up @@ -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
})
Expand Down
10 changes: 10 additions & 0 deletions osbs/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions osbs/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions osbs/tekton.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand All @@ -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,
Expand All @@ -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'
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion osbs/utils/otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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')
Expand Down

0 comments on commit 0aa0ca9

Please sign in to comment.