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 <[email protected]>
  • Loading branch information
hjmodi committed Sep 12, 2023
1 parent bc023f8 commit 4bbdb9f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 27 deletions.
8 changes: 7 additions & 1 deletion osbs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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
Expand All @@ -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
})
Expand Down
62 changes: 38 additions & 24 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 instrumented, get_tracer

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 @@ -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


Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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:
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
8 changes: 7 additions & 1 deletion osbs/tekton.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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,
Expand All @@ -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'
Expand All @@ -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()
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(service_name=OTEL_SERVICE_NAME)
def wait_for_start(self):
"""
https://tekton.dev/docs/pipelines/pipelineruns/#monitoring-execution-status
Expand Down
7 changes: 6 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 @@ -35,11 +36,15 @@ def init_otel(otel_url: Optional[str], traceparent: Optional[str]):
OTEL_EXPORTER_OTLP_PROTOCOL=otel_protocol,
)
init_telemetry_provider(otel_options)
if 'TRACEPARENT' in os.environ:
del os.environ['TRACEPARENT']
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

0 comments on commit 4bbdb9f

Please sign in to comment.