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 a754793
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
15 changes: 14 additions & 1 deletion 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

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 @@ -415,6 +420,8 @@ def main():
logger.error("Configuration error: %s", ex.message)
return -1

init_otel(otel_url=os_conf.get_otel_url(),
traceparent=args.traceparent)
is_verbose = os_conf.get_verbosity()

if args.quiet:
Expand All @@ -427,7 +434,13 @@ def main():

return_value = -1
try:
return_value = args.func(args)
span_name = "binary_build_pipeline"
if getattr(args, 'func', None) is cmd_build_source_container:
span_name = "source_build_pipeline"
tracer = get_tracer(module_name=span_name, service_name=OTEL_SERVICE_NAME)

with tracer.start_as_current_span(span_name):
return_value = args.func(args)
except AttributeError:
if hasattr(args, 'func'):
raise
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
4 changes: 3 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,10 @@ 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')
Expand Down

0 comments on commit a754793

Please sign in to comment.